Click here to Skip to main content
15,895,656 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
GeneralRe: DES Encryption Pin
Richard MacCutchan16-Dec-11 7:40
mveRichard MacCutchan16-Dec-11 7:40 
GeneralRe: DES Encryption Pin
Randor 16-Dec-11 7:59
professional Randor 16-Dec-11 7:59 
GeneralRe: DES Encryption Pin
jkirkerx16-Dec-11 8:12
professionaljkirkerx16-Dec-11 8:12 
GeneralRe: DES Encryption Pin
jkirkerx16-Dec-11 9:00
professionaljkirkerx16-Dec-11 9:00 
GeneralRe: DES Encryption Pin
Randor 16-Dec-11 9:41
professional Randor 16-Dec-11 9:41 
GeneralRe: DES Encryption Pin
jkirkerx16-Dec-11 10:31
professionaljkirkerx16-Dec-11 10:31 
GeneralRe: DES Encryption Pin
Randor 16-Dec-11 11:04
professional Randor 16-Dec-11 11:04 
GeneralRe: DES Encryption Pin
jkirkerx16-Dec-11 12:06
professionaljkirkerx16-Dec-11 12:06 
I went back to what I had before I posted, and changed CALG_DES to CALG_MD5. I don't get an error, , I'm just struggling in making my buffer that passes in/out in CryptEncrypt.

I get a value back that's sort or weird. It works, but I think the size of my buffer is too large.

R㏾΂ò.¢Ó`Ï­<oÝV|63¾ˆWãÔZ÷(šœ(è~Ó¥¶ãòõ¬"oÌtðaÏMûÏþƒÝÌXðýýýý««««««««îþîþîþîþîþîþ

I think the end in bold is a leak somewhere, or the buffer being too large

I know I need to CryptEncrypt once to get the buffer size, and do it again for the real thing.

I did a conversion to UTF8, that I think I need to dump. I'm going to dump it, and work on the buffer sizing.

This is what I had before I posted, with the one small change. It's not bad considering I had no clue what to do, but it's all I need, with the base64 at the end of the gig.

WCHAR*	CA_Encryption::_encrypt_DES( WCHAR *pzInput )
{
	BOOL			bResult = FALSE;
	WCHAR			*szOutput = NULL;

	DWORD cbKey64	= sizeof(KEY_64);
	HCRYPTPROV		hProv;
	HCRYPTHASH		hHash = NULL;
	HCRYPTKEY		hKey = NULL;

	const int		DES_SIZE = 64;
	static BYTE		hash[DES_SIZE];
	DWORD			buffer_size = sizeof(hash);
	DWORD			buffer_length;		

	// Get the size of the conversion
	int iChar = WideCharToMultiByte(CP_UTF8, 0, pzInput, -1, NULL, 0, NULL, NULL);	
	char *szInput = new char[iChar];
	WideCharToMultiByte(CP_UTF8, 0, pzInput, -1, szInput, iChar, NULL, NULL);
	
	// Aquire a handle the Encryption provider
	bResult = CryptAcquireContextW( &hProv, 0, MS_STRONG_PROV, PROV_RSA_FULL, 0);
	bResult = CryptCreateHash(hProv, CALG_MD5, 0, 0, &hHash);
	bResult = CryptHashData(hHash, KEY_64, sizeof(KEY_64), 0);	
	bResult = CryptDeriveKey(hProv, CALG_DES, hHash, 0, &hKey);
	bResult = CryptSetKeyParam(hKey, KP_IV, IV_64, 0);

	// Get the size of the buffer
	buffer_length = strlen((char *)szInput);	
	BYTE *szBuffer = new BYTE[64];
	memcpy( szBuffer, (BYTE*)szInput, sizeof(szInput) );
	delete [] szInput;

	// Get the size of the hash and length
	bResult = CryptEncrypt(hKey, 0, FALSE, 0, szBuffer, &buffer_size, buffer_length);	
	
			
	CryptDestroyKey(hKey);
	CryptDestroyHash(hHash);
	CryptReleaseContext(hProv, 0);	
	
	return szOutput;
}


This is what I'm trying to copy from asp.net

VB
Public Shared Function Encrypt(ByVal value As String) As String
        If value <> "" Then
            Dim cryptoProvider As DESCryptoServiceProvider = New DESCryptoServiceProvider()
            Dim ms As MemoryStream = New MemoryStream()
            Dim cs As CryptoStream = New CryptoStream(ms, cryptoProvider.CreateEncryptor(KEY_64, IV_64), _
            CryptoStreamMode.Write)
            Dim sw As StreamWriter = New StreamWriter(cs)

            sw.Write(value)
            sw.Flush()
            cs.FlushFinalBlock()
            ms.Flush()

            'convert back to a string
            Return Convert.ToBase64String(ms.GetBuffer(), 0, ms.Length)
        Else
            Encrypt = ""
        End If

    End Function

GeneralRe: DES Encryption Pin
Randor 16-Dec-11 13:25
professional Randor 16-Dec-11 13:25 
GeneralRe: DES Encryption Pin
jkirkerx17-Dec-11 8:05
professionaljkirkerx17-Dec-11 8:05 
GeneralRe: DES Encryption Pin
Randor 17-Dec-11 9:47
professional Randor 17-Dec-11 9:47 
GeneralRe: DES Encryption Pin
jkirkerx17-Dec-11 20:49
professionaljkirkerx17-Dec-11 20:49 
GeneralRe: DES Encryption Pin
Randor 18-Dec-11 3:55
professional Randor 18-Dec-11 3:55 
GeneralRe: DES Encryption Pin
jkirkerx18-Dec-11 8:32
professionaljkirkerx18-Dec-11 8:32 
GeneralRe: DES Encryption Pin
Randor 18-Dec-11 11:29
professional Randor 18-Dec-11 11:29 
GeneralRe: DES Encryption Pin
jkirkerx18-Dec-11 14:18
professionaljkirkerx18-Dec-11 14:18 
GeneralRe: DES Encryption Pin
Randor 19-Dec-11 6:19
professional Randor 19-Dec-11 6:19 
GeneralRe: DES Encryption Pin
jkirkerx19-Dec-11 8:06
professionaljkirkerx19-Dec-11 8:06 
GeneralRe: DES Encryption Pin
Randor 19-Dec-11 8:29
professional Randor 19-Dec-11 8:29 
GeneralRe: DES Encryption Pin
jkirkerx19-Dec-11 8:47
professionaljkirkerx19-Dec-11 8:47 
GeneralRe: DES Encryption Pin
jkirkerx19-Dec-11 9:30
professionaljkirkerx19-Dec-11 9:30 
GeneralRe: DES Encryption Pin
Randor 19-Dec-11 10:36
professional Randor 19-Dec-11 10:36 
GeneralRe: DES Encryption Pin
jkirkerx19-Dec-11 10:53
professionaljkirkerx19-Dec-11 10:53 
GeneralPerfect Match! Pin
jkirkerx19-Dec-11 12:13
professionaljkirkerx19-Dec-11 12:13 
GeneralRe: Perfect Match! Pin
Randor 19-Dec-11 14:03
professional Randor 19-Dec-11 14: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.