Click here to Skip to main content
15,892,537 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
AnswerRe: how to change panes in status bar of a dialog box application Pin
Iain Clarke, Warrior Programmer19-Nov-08 21:54
Iain Clarke, Warrior Programmer19-Nov-08 21:54 
Questiondecrypting a string question [modified] Pin
monsieur_jj19-Nov-08 19:42
monsieur_jj19-Nov-08 19:42 
AnswerRe: decrypting a string question Pin
Randor 19-Nov-08 21:52
professional Randor 19-Nov-08 21:52 
GeneralRe: decrypting a string question Pin
monsieur_jj19-Nov-08 22:09
monsieur_jj19-Nov-08 22:09 
GeneralRe: decrypting a string question Pin
CPallini19-Nov-08 22:16
mveCPallini19-Nov-08 22:16 
GeneralRe: decrypting a string question Pin
monsieur_jj20-Nov-08 15:34
monsieur_jj20-Nov-08 15:34 
GeneralRe: decrypting a string question Pin
Randor 20-Nov-08 21:58
professional Randor 20-Nov-08 21:58 
GeneralRe: decrypting a string question [modified] Pin
monsieur_jj20-Nov-08 22:09
monsieur_jj20-Nov-08 22:09 
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&lt;const BYTE *&gt;(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, &amp;lul_buflen);

	ThreeDESdecrypt((unsigned char *)CypherText,len3,(unsigned char *)MyPassword,len2,(unsigned char *)MyString,&amp;len1);
return 0;
}


The bold part CryptDecrypt returns false and the plaintext is garbage, What I expect the plaintext to contain "test".
The thing is this is the code that encrypts test:

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);
        }


That is the service that sends me the encrypted string which i need to decrypt I tested "test" as my string and I get "Zed5OmjUWs8=" as the encrypted string. also the key being used as the security key is "h3bmull3r"

Please advise.

merci,
Jayjay

modified on Friday, November 21, 2008 4:59 AM

AnswerRe: decrypting a string question Pin
CPallini19-Nov-08 21:59
mveCPallini19-Nov-08 21:59 
GeneralRe: decrypting a string question Pin
monsieur_jj20-Nov-08 22:05
monsieur_jj20-Nov-08 22:05 
QuestionModifying one cell in a CListCtrl Pin
sunny_vc19-Nov-08 18:58
sunny_vc19-Nov-08 18:58 
AnswerRe: Modifying one cell in a CListCtrl Pin
Hamid_RT19-Nov-08 20:28
Hamid_RT19-Nov-08 20:28 
AnswerRe: Modifying one cell in a CListCtrl Pin
Prasann Mayekar19-Nov-08 20:32
Prasann Mayekar19-Nov-08 20:32 
AnswerRe: Modifying one cell in a CListCtrl Pin
Nishad S19-Nov-08 20:41
Nishad S19-Nov-08 20:41 
GeneralRe: Modifying one cell in a CListCtrl Pin
sunny_vc19-Nov-08 21:37
sunny_vc19-Nov-08 21:37 
GeneralRe: Modifying one cell in a CListCtrl Pin
Nishad S19-Nov-08 22:00
Nishad S19-Nov-08 22:00 
GeneralRe: Modifying one cell in a CListCtrl Pin
sunny_vc19-Nov-08 22:40
sunny_vc19-Nov-08 22:40 
GeneralRe: Modifying one cell in a CListCtrl Pin
Nishad S19-Nov-08 23:06
Nishad S19-Nov-08 23:06 
GeneralRe: Modifying one cell in a CListCtrl Pin
sunny_vc20-Nov-08 22:02
sunny_vc20-Nov-08 22:02 
GeneralRe: Modifying one cell in a CListCtrl Pin
Nishad S20-Nov-08 22:13
Nishad S20-Nov-08 22:13 
QuestionGet Documents Path Pin
MsmVc19-Nov-08 18:40
MsmVc19-Nov-08 18:40 
QuestionRe: Get Documents Path Pin
Rajesh R Subramanian19-Nov-08 18:49
professionalRajesh R Subramanian19-Nov-08 18:49 
AnswerRe: Get Documents Path Pin
MsmVc19-Nov-08 19:02
MsmVc19-Nov-08 19:02 
AnswerRe: Get Documents Path Pin
Rajesh R Subramanian19-Nov-08 19:15
professionalRajesh R Subramanian19-Nov-08 19:15 
AnswerRe: Get Documents Path Pin
MsmVc19-Nov-08 19:12
MsmVc19-Nov-08 19:12 

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.