Hi all,
I have this c# decrypting code:
public static string Decrypt(string cipherString, bool useHashing)
{
byte[] keyArray;
byte[] toEncryptArray = Convert.FromBase64String(cipherString);
System.Configuration.AppSettingsReader settingsReader = new AppSettingsReader();
string key = (string)settingsReader.GetValue("SecurityKey", typeof(String));
if (useHashing)
{
MD5CryptoServiceProvider hashmd5 = new MD5CryptoServiceProvider();
keyArray = hashmd5.ComputeHash(UTF8Encoding.UTF8.GetBytes(key));
hashmd5.Clear();
}
else
keyArray = UTF8Encoding.UTF8.GetBytes(key);
TripleDESCryptoServiceProvider tdes = new TripleDESCryptoServiceProvider();
tdes.Key = keyArray;
tdes.Mode = CipherMode.ECB;
tdes.Padding = PaddingMode.PKCS7;
ICryptoTransform cTransform = tdes.CreateDecryptor();
byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length);
tdes.Clear();
return UTF8Encoding.UTF8.GetString(resultArray);
}
public static string Encrypt(string toEncrypt, bool useHashing)
{
byte[] keyArray;
byte[] toEncryptArray = UTF8Encoding.UTF8.GetBytes(toEncrypt);
System.Configuration.AppSettingsReader settingsReader = new AppSettingsReader();
string key = (string)settingsReader.GetValue("SecurityKey", typeof(String));
if (useHashing)
{
MD5CryptoServiceProvider hashmd5 = new MD5CryptoServiceProvider();
keyArray = hashmd5.ComputeHash(UTF8Encoding.UTF8.GetBytes(key));
hashmd5.Clear();
}
else
keyArray = UTF8Encoding.UTF8.GetBytes(key);
TripleDESCryptoServiceProvider tdes = new TripleDESCryptoServiceProvider();
tdes.Key = keyArray;
tdes.Mode = CipherMode.ECB;
tdes.Padding = PaddingMode.PKCS7;
ICryptoTransform cTransform = tdes.CreateEncryptor();
byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length);
tdes.Clear();
return Convert.ToBase64String(resultArray, 0, resultArray.Length);
}
No Randor advised me to do some thing inorder to translate it to c++ according to his advise by using Microsoft Cryptographic Service Providers I came up with this thanks to sources and samples, my question is why does cryptDecrypt give me zero as a result. The "Zed5OmjUWs8=" string is the encrypted value of "test" I am getting a result far from that any advice?
Thanks,
Jayjay
void ThreeDESdecrypt(unsigned char *cyphertext,unsigned long ctlen,unsigned char *passwd,unsigned long pwlen,unsigned char *plaintext,unsigned long *ptlen)
{
HCRYPTPROV hProv = NULL;
HCRYPTHASH hHash = NULL;
HCRYPTKEY hKey = NULL;
DWORD dMode;
BYTE * value2 = {0};
DWORD lul_len, lul_buflen, lul_skip, lul_pflags;
bool lb_rtn;
BLOB lblob_data;
TCHAR* convertedStr;
BYTE * key1 = NULL;
std::string value = "Zed5OmjUWs8=";
lul_len = strlen(value.c_str());
lul_buflen = (lul_len * 2);
BOOL result = CryptStringToBinary(value.c_str(), lul_len, CRYPT_STRING_BASE64, NULL, &lul_buflen, &lul_skip, NULL);
key1 = new BYTE [ lul_buflen ];
memset ( key1, 0 , lul_buflen );
result = CryptStringToBinary(value.c_str(), lul_len, CRYPT_STRING_BASE64, key1, &lul_buflen, &lul_skip, NULL);
hProv = InitializeCrypt();
result = CryptCreateHash(hProv,CALG_MD5,0,0,&hHash);
result = CryptHashData(hHash, passwd, pwlen, 0);
result = CryptDeriveKey(hProv,CALG_3DES,hHash,0,&hKey);
dMode = CRYPT_MODE_ECB;
DWORD dwLastError = GetLastError();
result = CryptSetKeyParam(hKey, PKCS5_PADDING, reinterpret_cast<const BYTE *>(&dMode), 0);
memcpy(plaintext,key1,*ptlen);
result = CryptDecrypt(hKey,hHash,1,0,plaintext,&lul_buflen);
*ptlen=ctlen;
CryptBinaryToString(plaintext, lul_len, CRYPT_STRING_BASE64, NULL, &lul_buflen);
convertedStr = new TCHAR [lul_buflen];
memset( convertedStr, 0 , lul_buflen );
CryptBinaryToString(plaintext, lul_len, CRYPT_STRING_BASE64, convertedStr, &lul_buflen);
result = CryptDestroyKey(hKey);
result = CryptDestroyHash(hHash);
result = CryptReleaseContext(hProv,0);
}
|