Click here to Skip to main content
15,886,873 members
Home / Discussions / C#
   

C#

 
GeneralRe: BinaryWriter/BinaryReader Portability Pin
Andrew Shapira24-Sep-03 11:12
Andrew Shapira24-Sep-03 11:12 
Generalsurvey on books Pin
nevhile.net23-Sep-03 14:52
nevhile.net23-Sep-03 14:52 
GeneralRe: survey on books Pin
David Stone23-Sep-03 16:07
sitebuilderDavid Stone23-Sep-03 16:07 
GeneralRe: survey on books Pin
James T. Johnson23-Sep-03 17:17
James T. Johnson23-Sep-03 17:17 
GeneralRe: survey on books Pin
Andrew Shapira23-Sep-03 18:40
Andrew Shapira23-Sep-03 18:40 
GeneralRe: survey on books Pin
David Stone23-Sep-03 19:16
sitebuilderDavid Stone23-Sep-03 19:16 
GeneralRe: survey on books Pin
Bog26-Sep-03 8:42
Bog26-Sep-03 8:42 
GeneralC# Crypto Interop with WIN32 Pin
cory_baker23-Sep-03 12:09
cory_baker23-Sep-03 12:09 
Hi all!

I am trying to encrypt data with C# and decrypt the same data using VC++ 6, both using a password. I believe that I am very close on this one but am currently stuck and am receiving error 2148073477 on the VC++ CryptDecrypt() call. My keys generated by C# and VC++ are the same, so that seems to be okay.

My C# code is as follows:

// C# Begin *********************************
string s = "Hello";
RC2CryptoServiceProvider sp;
MemoryStream ms;
byte[] toEncrypt;
byte[] fromEncrypt;
byte[] IV = new byte[8];
byte[] key;


sp = new RC2CryptoServiceProvider();
sp.Mode = CipherMode.CBC;
sp.KeySize = 40;
sp.EffectiveKeySize = 40;

ms = new MemoryStream();

// Create the key
PasswordDeriveBytes pdb = new PasswordDeriveBytes( "ABCD-1234-XXXX-0000", IV );
key = pdb.CryptDeriveKey( "RC2", "MD5", 40, IV );
sp.Key = key;

// Create the crypto stream
CryptoStream cs = new CryptoStream( ms, sp.CreateEncryptor(), CryptoStreamMode.Write );

// Encrypt the data
toEncrypt = Encoding.UTF8.GetBytes( s );
cs.Write( toEncrypt, 0, toEncrypt.Length );
cs.FlushFinalBlock();
ms.Position = 0;
fromEncrypt = new byte[ms.Length];
ms.Read( fromEncrypt, 0, (int)ms.Length );

// Cleanup
cs.Close();
ms.Close();

// C# End *********************************

My VC is as follows:

The VC code contains a byte array that I populate after running the C# code and stepping through.
It contains a call to CryptEncrypt() and CryptDecrypt() to prove that encryption is working with the generated key.
I also exported the key to ensure that it was identical to the C# key.

// VC Begin *********************************
HCRYPTPROV hCryptProv;
HCRYPTKEY hKey;
HCRYPTHASH hHash;
CHAR szPassword[] = "ABCD-1234-XXXX-0000";
CHAR szMessage[512] = "";
DWORD dwLength;
BOOL b;
BYTE* pbKeyBlob;
BYTE bTest[] = {0x1e, 0x03, 0x91, 0xbe, 0xef, 0x2b, 0xdf, 0x91};

strcpy( szMessage, "Hello" );

b = CryptAcquireContext( &hCryptProv, NULL, MS_DEF_PROV, PROV_RSA_FULL, 0);
b = CryptCreateHash( hCryptProv, CALG_MD5, 0, 0, &hHash);

dwLength = strlen( szPassword );
b = CryptHashData( hHash, (BYTE*)szPassword, dwLength, 0);
b = CryptDeriveKey( hCryptProv, CALG_RC2, hHash, CRYPT_EXPORTABLE, &hKey);

dwLength = strlen( szMessage );
b = CryptEncrypt( hKey, 0, TRUE, 0, (BYTE*)szMessage, &dwLength, 512 );
b = CryptDecrypt( hKey, 0, TRUE, 0, (BYTE*)szMessage, &dwLength );

// The call that FAILS!
dwLength = 8;
b = CryptDecrypt( hKey, 0, TRUE, 0, bTest, &dwLength );
DWORD dw = GetLastError();

b = CryptExportKey( hKey, NULL, PLAINTEXTKEYBLOB, 0, NULL, &dwLength);
pbKeyBlob = (BYTE*)malloc(dwLength);
b = CryptExportKey( hKey, NULL, PLAINTEXTKEYBLOB, 0, pbKeyBlob, &dwLength);
free( pbKeyBlob );

b = CryptDestroyHash( hHash );
b = CryptDestroyKey( hKey );
b = CryptReleaseContext( hCryptProv, 0 );

// VC End *********************************


Is there something I'm missing?

Thanks
Cory Baker

QuestionHow-To Create Dynamic Webservice Detection? Pin
DudleyDoorite23-Sep-03 11:20
DudleyDoorite23-Sep-03 11:20 
QuestionC# Question: static int amountopen = (0 - 500); ??? Pin
Chua Wen Ching23-Sep-03 10:59
Chua Wen Ching23-Sep-03 10:59 
AnswerRe: C# Question: static int amountopen = (0 - 500); ??? Pin
Alvaro Mendez23-Sep-03 11:13
Alvaro Mendez23-Sep-03 11:13 
GeneralRe: C# Question: static int amountopen = (0 - 500); ??? Pin
Chua Wen Ching23-Sep-03 11:21
Chua Wen Ching23-Sep-03 11:21 
GeneralRe: C# Question: static int amountopen = (0 - 500); ??? Pin
Alvaro Mendez23-Sep-03 11:48
Alvaro Mendez23-Sep-03 11:48 
GeneralRe: C# Question: static int amountopen = (0 - 500); ??? Pin
Chua Wen Ching23-Sep-03 22:53
Chua Wen Ching23-Sep-03 22:53 
GeneralDefault type access modifier Pin
Rohde23-Sep-03 10:27
Rohde23-Sep-03 10:27 
GeneralRe: Default type access modifier Pin
Alvaro Mendez23-Sep-03 11:03
Alvaro Mendez23-Sep-03 11:03 
GeneralRe: Default type access modifier Pin
Blake Coverett23-Sep-03 16:15
Blake Coverett23-Sep-03 16:15 
GeneralRe: Default type access modifier Pin
Alvaro Mendez23-Sep-03 18:30
Alvaro Mendez23-Sep-03 18:30 
GeneralRe: Default type access modifier Pin
Blake Coverett23-Sep-03 21:20
Blake Coverett23-Sep-03 21:20 
GeneralRe: Default type access modifier Pin
Rohde23-Sep-03 23:03
Rohde23-Sep-03 23:03 
GeneralReading/Writing a CMYK Bitmap Pin
Matt Philmon23-Sep-03 10:08
Matt Philmon23-Sep-03 10:08 
GeneralRe: Reading/Writing a CMYK Bitmap Pin
Blake Coverett23-Sep-03 16:19
Blake Coverett23-Sep-03 16:19 
GeneralSending mail with HTML link in it Pin
Tartampion23-Sep-03 9:35
Tartampion23-Sep-03 9:35 
GeneralRe: Sending mail with HTML link in it Pin
Marshall Rosenstein23-Sep-03 9:44
Marshall Rosenstein23-Sep-03 9:44 
GeneralRe: Sending mail with HTML link in it Pin
Tartampion23-Sep-03 13:03
Tartampion23-Sep-03 13:03 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.