using System;
using System.Collections.Generic;
using System.Text;
using System.Security.Cryptography;
using System.IO;
namespace strEncrypt
{
public class clsEncrypt
{
public string EncryptString(string input)
{
try
{
System.Text.UnicodeEncoding encoding = new UnicodeEncoding();
System.Security.Cryptography.SHA1CryptoServiceProvider sha1 = new SHA1CryptoServiceProvider();
byte[] byteVal = encoding.GetBytes(input);
byte[] byteencryptedPwd = sha1.ComputeHash(byteVal);
sha1.Clear();
string pwd = Convert.ToBase64String (byteencryptedPwd);
try
{
StreamWriter sw = new StreamWriter(@"C:\Password.txt");
sw.WriteLine(pwd);
sw.Close();
}
catch
{
//Apart from returning the encrypted string, I am also writing the same to C:\Password.txt.
}
return pwd;
}
catch
{
return "EXCEPTION";
}
}
}
}
---------------------------------------------------------------------------------
Now we will get a C#.NET dll and now let me explain you how to consume this in C++ application.
---------------------------------------------------------------------------------
Create a new project from Visual studio :
Project Types : Visual C++ -> Win32 -> Win32 Console application. Click on OK after entering the file name.
Now we will get Win32 Applicatio Wizard. Click on Next.
Select 'Console Application' and click on finish.
---------------------------------------------------------------------------------
Copy the dll into our C++ project folder.
Open 'Property Pages' of the project. From Common Properties -> References , click on 'Add new reference' and add the C#.NET dll reference.
From the same window 'Configuration Properties' ->
General :
Character Set : Not set
Common Language Runtime support - select 'Common Language Runtime Support (/clr)'
C/C++ : Debug Information format : Disabled
Warning Level : Level1
Detect 64 bit portability issues : No
---------------------------------------------------------------------------------
code for .cpp file
---------------------------------------------------------------------------------
#include <iostream>
#using <mscorlib.dll>
#using <strEncryptCS.dll>
using namespace System;
using namespace System::Collections;
using namespace System::Collections::Generic;
using namespace System::Security::Cryptography;
using namespace std;
using namespace strEncrypt;
System::String^ CPPEncryptString(System::String^);
void main(int argc, char *argv[])
{
System::String^ input;
System::String^ output;
Console::WriteLine("Enter String:");
input=Console::ReadLine();
output = CPPEncryptString(input);
Console::WriteLine(output);
Console::Read();
}
System::String^ CPPEncryptString(System::String^ input)
{
clsEncrypt a;
return a.EncryptString(input);
}
---------------------------------------------------------------------------------
Build this application. You will get an exe.
This is how we can access & use functions C#.NET dll in C++.
No comments:
Post a Comment