decrypting a string question [modified]
-
Hi all, Is there a c++ version of this or at least can a c++ version of this algorithm can be made? Please advise. Can anyone give me a sample on how to use windows crypto api decryption just by inputting an encrypted string? Thanks, Jayjay btw this is made of c#
public static string Decrypt(string cipherString, bool useHashing)
{
byte[] keyArray;
//get the byte code of the stringbyte\[\] toEncryptArray = Convert.FromBase64String(cipherString); System.Configuration.AppSettingsReader settingsReader = new AppSettingsReader(); //Get your key from config file to open the lock! string key = (string)settingsReader.GetValue("SecurityKey", typeof(String)); if (useHashing) { //if hashing was used get the hash code with regards to your key MD5CryptoServiceProvider hashmd5 = new MD5CryptoServiceProvider(); keyArray = hashmd5.ComputeHash(UTF8Encoding.UTF8.GetBytes(key)); //release any resource held by the MD5CryptoServiceProvider hashmd5.Clear(); } else { //if hashing was not implemented get the byte code of the key keyArray = UTF8Encoding.UTF8.GetBytes(key); } TripleDESCryptoServiceProvider tdes = new TripleDESCryptoServiceProvider(); //set the secret key for the tripleDES algorithm tdes.Key = keyArray; //mode of operation. there are other 4 modes. //We choose ECB(Electronic code Book) tdes.Mode = CipherMode.ECB; //padding mode(if any extra byte added) tdes.Padding = PaddingMode.PKCS7; ICryptoTransform cTransform = tdes.CreateDecryptor(); byte\[\] resultArray = cTransform.TransformFinalBlock (toEncryptArray, 0, toEncryptArray.Length); //Release resources held by TripleDes Encryptor tdes.Clear(); //return the Clear decrypted TEXT return UTF8Encoding.UTF8.GetString(resultArray); }
modified on Thursday, November 20, 2008 3:28 AM
-
Hi all, Is there a c++ version of this or at least can a c++ version of this algorithm can be made? Please advise. Can anyone give me a sample on how to use windows crypto api decryption just by inputting an encrypted string? Thanks, Jayjay btw this is made of c#
public static string Decrypt(string cipherString, bool useHashing)
{
byte[] keyArray;
//get the byte code of the stringbyte\[\] toEncryptArray = Convert.FromBase64String(cipherString); System.Configuration.AppSettingsReader settingsReader = new AppSettingsReader(); //Get your key from config file to open the lock! string key = (string)settingsReader.GetValue("SecurityKey", typeof(String)); if (useHashing) { //if hashing was used get the hash code with regards to your key MD5CryptoServiceProvider hashmd5 = new MD5CryptoServiceProvider(); keyArray = hashmd5.ComputeHash(UTF8Encoding.UTF8.GetBytes(key)); //release any resource held by the MD5CryptoServiceProvider hashmd5.Clear(); } else { //if hashing was not implemented get the byte code of the key keyArray = UTF8Encoding.UTF8.GetBytes(key); } TripleDESCryptoServiceProvider tdes = new TripleDESCryptoServiceProvider(); //set the secret key for the tripleDES algorithm tdes.Key = keyArray; //mode of operation. there are other 4 modes. //We choose ECB(Electronic code Book) tdes.Mode = CipherMode.ECB; //padding mode(if any extra byte added) tdes.Padding = PaddingMode.PKCS7; ICryptoTransform cTransform = tdes.CreateDecryptor(); byte\[\] resultArray = cTransform.TransformFinalBlock (toEncryptArray, 0, toEncryptArray.Length); //Release resources held by TripleDes Encryptor tdes.Clear(); //return the Clear decrypted TEXT return UTF8Encoding.UTF8.GetString(resultArray); }
modified on Thursday, November 20, 2008 3:28 AM
Bonjour JayJay, Yes that function can be implemented in C++ using Microsoft Cryptographic Service Providers.[^] From looking at the source code you have provided it appears that the function works as follows: 1.) Function is passed a base64 encoded bytes and a boolean. 2.) Decode base64 encoded bytes to a byte array. 3.) Open a "settings reader" and retrieve a key/pair value from key 'SecurityKey'. 4.) if useHashing boolean is true then create a MD5 hash from the "SecurityKey"value. 5.) if useHashing boolean is false then cast the "SecurityKey"value to a UTF8 encoded unicode string. 6.) Decrypt the byte array using the 'Security' key with TripleDES algorithm. You should research the following funtions from the Microsoft Cryptographic Service. For Initializing Microsoft Cryptographic Service Providers: CryptAcquireContext Function[^] For base64 decoding: CryptBinaryToString Function [^] For computing MD5 hash: CryptCreateHash Function[^] CryptDestroyHash Function[^] For TripleDES: CryptDeriveKey Function[^] CryptDecrypt Function[^] CryptDestroyKey Function[
-
Hi all, Is there a c++ version of this or at least can a c++ version of this algorithm can be made? Please advise. Can anyone give me a sample on how to use windows crypto api decryption just by inputting an encrypted string? Thanks, Jayjay btw this is made of c#
public static string Decrypt(string cipherString, bool useHashing)
{
byte[] keyArray;
//get the byte code of the stringbyte\[\] toEncryptArray = Convert.FromBase64String(cipherString); System.Configuration.AppSettingsReader settingsReader = new AppSettingsReader(); //Get your key from config file to open the lock! string key = (string)settingsReader.GetValue("SecurityKey", typeof(String)); if (useHashing) { //if hashing was used get the hash code with regards to your key MD5CryptoServiceProvider hashmd5 = new MD5CryptoServiceProvider(); keyArray = hashmd5.ComputeHash(UTF8Encoding.UTF8.GetBytes(key)); //release any resource held by the MD5CryptoServiceProvider hashmd5.Clear(); } else { //if hashing was not implemented get the byte code of the key keyArray = UTF8Encoding.UTF8.GetBytes(key); } TripleDESCryptoServiceProvider tdes = new TripleDESCryptoServiceProvider(); //set the secret key for the tripleDES algorithm tdes.Key = keyArray; //mode of operation. there are other 4 modes. //We choose ECB(Electronic code Book) tdes.Mode = CipherMode.ECB; //padding mode(if any extra byte added) tdes.Padding = PaddingMode.PKCS7; ICryptoTransform cTransform = tdes.CreateDecryptor(); byte\[\] resultArray = cTransform.TransformFinalBlock (toEncryptArray, 0, toEncryptArray.Length); //Release resources held by TripleDes Encryptor tdes.Clear(); //return the Clear decrypted TEXT return UTF8Encoding.UTF8.GetString(resultArray); }
modified on Thursday, November 20, 2008 3:28 AM
monsieur_jj wrote:
Is there a c++ version of this
Possibly, yes.
monsieur_jj wrote:
or at least can a c++ version of this algorithm can be made?
This for sure, even the
C#
one is, at the end, done inC++
. :rolleyes: Reference aboutCrypto API
here. There are also articles about, here at CodeProject [^]. :)If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
[My articles] -
Bonjour JayJay, Yes that function can be implemented in C++ using Microsoft Cryptographic Service Providers.[^] From looking at the source code you have provided it appears that the function works as follows: 1.) Function is passed a base64 encoded bytes and a boolean. 2.) Decode base64 encoded bytes to a byte array. 3.) Open a "settings reader" and retrieve a key/pair value from key 'SecurityKey'. 4.) if useHashing boolean is true then create a MD5 hash from the "SecurityKey"value. 5.) if useHashing boolean is false then cast the "SecurityKey"value to a UTF8 encoded unicode string. 6.) Decrypt the byte array using the 'Security' key with TripleDES algorithm. You should research the following funtions from the Microsoft Cryptographic Service. For Initializing Microsoft Cryptographic Service Providers: CryptAcquireContext Function[^] For base64 decoding: CryptBinaryToString Function [^] For computing MD5 hash: CryptCreateHash Function[^] CryptDestroyHash Function[^] For TripleDES: CryptDeriveKey Function[^] CryptDecrypt Function[^] CryptDestroyKey Function[
Hi Randor, Thanks btw this is the encrypt code from where the string passed is derived:
public static string Encrypt(string toEncrypt, bool useHashing)
{
byte[] keyArray;
byte[] toEncryptArray = UTF8Encoding.UTF8.GetBytes(toEncrypt);System.Configuration.AppSettingsReader settingsReader = new AppSettingsReader(); // Get the key from config file string key = (string)settingsReader.GetValue("SecurityKey", typeof(String)); //System.Windows.Forms.MessageBox.Show(key); 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); }
-
Bonjour JayJay, Yes that function can be implemented in C++ using Microsoft Cryptographic Service Providers.[^] From looking at the source code you have provided it appears that the function works as follows: 1.) Function is passed a base64 encoded bytes and a boolean. 2.) Decode base64 encoded bytes to a byte array. 3.) Open a "settings reader" and retrieve a key/pair value from key 'SecurityKey'. 4.) if useHashing boolean is true then create a MD5 hash from the "SecurityKey"value. 5.) if useHashing boolean is false then cast the "SecurityKey"value to a UTF8 encoded unicode string. 6.) Decrypt the byte array using the 'Security' key with TripleDES algorithm. You should research the following funtions from the Microsoft Cryptographic Service. For Initializing Microsoft Cryptographic Service Providers: CryptAcquireContext Function[^] For base64 decoding: CryptBinaryToString Function [^] For computing MD5 hash: CryptCreateHash Function[^] CryptDestroyHash Function[^] For TripleDES: CryptDeriveKey Function[^] CryptDecrypt Function[^] CryptDestroyKey Function[
Randor wrote:
Hope it helps!
If it doesn't, it is not your fault, for sure. :)
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
[My articles] -
Bonjour JayJay, Yes that function can be implemented in C++ using Microsoft Cryptographic Service Providers.[^] From looking at the source code you have provided it appears that the function works as follows: 1.) Function is passed a base64 encoded bytes and a boolean. 2.) Decode base64 encoded bytes to a byte array. 3.) Open a "settings reader" and retrieve a key/pair value from key 'SecurityKey'. 4.) if useHashing boolean is true then create a MD5 hash from the "SecurityKey"value. 5.) if useHashing boolean is false then cast the "SecurityKey"value to a UTF8 encoded unicode string. 6.) Decrypt the byte array using the 'Security' key with TripleDES algorithm. You should research the following funtions from the Microsoft Cryptographic Service. For Initializing Microsoft Cryptographic Service Providers: CryptAcquireContext Function[^] For base64 decoding: CryptBinaryToString Function [^] For computing MD5 hash: CryptCreateHash Function[^] CryptDestroyHash Function[^] For TripleDES: CryptDeriveKey Function[^] CryptDecrypt Function[^] CryptDestroyKey Function[
Bonjour David, I am a bit confused should I use CryptBinaryToString or CryptStringtoBinary? Thanks, Jayjay
-
Bonjour David, I am a bit confused should I use CryptBinaryToString or CryptStringtoBinary? Thanks, Jayjay
-
monsieur_jj wrote:
Is there a c++ version of this
Possibly, yes.
monsieur_jj wrote:
or at least can a c++ version of this algorithm can be made?
This for sure, even the
C#
one is, at the end, done inC++
. :rolleyes: Reference aboutCrypto API
here. There are also articles about, here at CodeProject [^]. :)If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
[My articles]message deleted
-
Use CryptStringToBinary to decode from base64. Use CryptBinaryToString to encode to base64. Best Wishes, -David Delaune
Hi, I did a translation test and here it is unfortunately it ends up garbage:
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;BYTE * value2 = {0};
DWORD lul_len, lul_buflen, lul_skip, lul_pflags;
bool lb_rtn;
BLOB lblob_data;
// value = reinterpret_cast<const BYTE *>(cyphertext);
TCHAR convertedStr[13];
convertedStr[0] = ('\0');
BYTE key1[24] = {0};lul\_len = 13; lul\_buflen = (lul\_len \* 2); std::string value = "Zed5OmjUWs8="; hProv = InitializeCrypt(); bool result = CryptStringToBinary(value.c\_str(), lul\_len, CRYPT\_STRING\_BASE64, key1, &lul\_buflen, &lul\_skip, NULL);
//bool result = CryptAcquireContext(&hProv,SecurityKey,NULL, PROV_RSA_FULL, CRYPT_NEWKEYSET);
//bool result = CryptBinaryToString(value, lul_len, CRYPT_STRING_BASE64, convertedStr, &lul_buflen);
result = CryptCreateHash(hProv,CALG_MD5,0,0,&hHash);
result = CryptDeriveKey(hProv,CALG_3DES,hHash,0,&hKey);
memcpy(plaintext,key1,*ptlen);
result = CryptDecrypt(hKey,NULL,1,0,plaintext,&ctlen);
*ptlen=ctlen;
result = CryptDestroyKey(hKey);
result = CryptDestroyHash(hHash);
result = CryptReleaseContext(hProv,0);
}int main() {
HCRYPTPROV hCryptProv;// = InitializeCrypt();
HCRYPTHASH hHash = 0;
HCRYPTKEY hKey = 0;
PBYTE pbBuffer = NULL;
DWORD dwCount;const BYTE \* value; DWORD lul\_len, lul\_buflen, lul\_skip, lul\_pflags, ptlen; bool lb\_rtn; BLOB lblob\_data; std::string key = "h3bmull3r"; const std::string s = "Zed5OmjUWs8=" ; value = reinterpret\_cast<const BYTE \*>(s.c\_str()); int stringSize = s.size(); TCHAR convertedStr\[12\]; convertedStr\[0\] = ('\\0'); lul\_len = s.size(); lul\_buflen = (lul\_len \* 2); //hCryptProv = InitializeCrypt(); char CypherText\[128\]="Zed5OmjUWs8="; char MyPassword\[\]="h3bmull3r"; unsigned char MyString\[128\]; unsigned long len1,len2,len3; len1=strlen(CypherText); len2=strlen(MyPassword); len3=128; //size of the cypehrtext buffer above memset(MyString,0,128); //clear the buffer len1=128; //size of the plaintext buffer lb\_rtn = CryptBinaryToString(value, lul\_len, CRYPT\_STRING\_BASE64, convertedStr, &lul\_buflen); ThreeDESdecrypt((unsigned char \*)CypherText,len3,(unsigned char \*)MyPassword,len2,(unsigned char \*)MyString,&len1);
ret