?
Solved

Decrypt via Crypto API

Posted on 2003-02-26
4
Medium Priority
?
1,626 Views
Last Modified: 2007-12-19
I am using the Crypto API definition from Project Jedi.  I can encrypt the file just fine, but going through the decrypt process I am having to do some work that I know is not correct, and need somebody to look at my decryption code, and tell me what I am doing wrong....   I will comment it in areas I think is needed.  I will also enclude my encryption procedure.


function TfrmMain.EnCrypt(cPassword: String): String;
var
 hProv      : HCRYPTPROV;

 hHash      : HCRYPTHASH;

 dwLength   : DWORD;
 Key        : String;

 hKey       : HCRYPTKEY;

 pbKeyBlob  : PByte;
 dwBlobLen  : Integer;
 str        : String;
 Password   : String;
begin
  if not Wcrypt2.CryptAcquireContext(@hProv, nil, nil, PROV_RSA_FULL, 0) then
   begin
    if GetLastError <> NTE_KEYSET_NOT_DEF then Exit;
    if not Wcrypt2.CryptAcquireContext(@hProv, nil, nil, PROV_RSA_FULL,
                                       CRYPT_NEWKEYSET) then Exit;
   end;

  if not Wcrypt2.CryptCreateHash(hProv, CALG_MD5, 0, 0, @hHash) then Exit;

  dwLength := Length(Key);
  if not Wcrypt2.CryptHashData(hHash, PByte(Key), dwLength, 0) then Exit;

  if not Wcrypt2.CryptDeriveKey(hProv, CALG_RC2, hHash, 0, @hKey) then Exit;

  // Now that are keys are generated, and stored, we can now crypt, FINALLY!

  Password := cPassword;

  dwBlobLen := Length(Password);

  Wcrypt2.CryptEncrypt(hKey, 0, True, 0, nil, @dwBlobLen, 0);

  dwLength := Length(Password);
  GetMem(pbKeyBlob, dwBlobLen);

  try
   Move(Password[1], pbKeyBlob^, dwLength);
   if not Wcrypt2.CryptEncrypt(hKey, 0, True, 0, pbKeyBlob, @dwLength,
                               dwBlobLen) then Exit;
   SetLength(Str, dwBlobLen);
   Move(pbKeyBlob^, Str[1], dwBlobLen);
  finally
   FreeMem(pbKeyBlob);
   Wcrypt2.CryptDestroyKey(hKey);
   Wcrypt2.CryptDestroyHash(hHash);
   Wcrypt2.CryptReleaseContext(hProv, 0);
   Result := Str;
  end;
end;

// Something that is not right is the L variable.  When I call this function, the L is the length of the original password, and you will see what I have to do with it later...
function TfrmMain.DeCrypt(CypherText: String; L: Integer): String;
var
 hProv      : HCRYPTPROV;

 hHash      : HCRYPTHASH;

 dwLength   : DWORD;
 Key        : String;

 hKey       : HCRYPTKEY;

 pbKeyBlob  : PByte;
 dwBlob     : String;
 dwBuffLen  : Integer;
 str        : String;
 Password   : String;
begin
  if not Wcrypt2.CryptAcquireContext(@hProv, nil, nil, PROV_RSA_FULL, 0) then
   begin
    if GetLastError <> NTE_KEYSET_NOT_DEF then Exit;
    if not Wcrypt2.CryptAcquireContext(@hProv, nil, nil, PROV_RSA_FULL,
                                       CRYPT_NEWKEYSET) then Exit;
   end;

  if not Wcrypt2.CryptCreateHash(hProv, CALG_MD5, 0, 0, @hHash) then Exit;

  dwLength := Length(Key);
  if not Wcrypt2.CryptHashData(hHash, PByte(Key), dwLength, 0) then Exit;

  if not Wcrypt2.CryptDeriveKey(hProv, CALG_RC2, hHash, 0, @hKey) then Exit;

  Wcrypt2.CryptDestroyHash(hHash); // Release the hash object.  Not needed anymore.

  Password := CypherText;
  dwBuffLen := Length(Password) * 10; // buffer was no big enough.  Must be a multiple of Length.
  dwBlob    := Password; // Set the blob to be decrypted.
  (* BOOL WINAPI CryptDecrypt(
                   HCRYPTKEY hKey,
                   HCRYPTHASH hHash,
                   BOOL Final,
                   DWORD dwFlags,
                   BYTE* pbData,
                   DWORD* pdwDataLen
  *)
  dwLength := Length(Password);
  GetMem(pbKeyBlob, dwBuffLen);  //using bloblen caused massive cpu spike
  try
   Move(Password[1], pbKeyBlob^, dwLength);
   Wcrypt2.CryptDecrypt(hKey, 0, True, 0, pbKeyBlob, @dwBuffLen); // Change BuffLen to Length
   //if not Wcrypt2.CryptDecrypt(hKey, 0, True, 0, @dwBuffLen, @dwBlobLen) then
    //Exit;
   SetLength(Str, dwBuffLen);
   Move(pbKeyBlob^, Str[1], (dwBuffLen));
   //Move(pbKeyBlob^, Str[1], dwBuffLen);
  finally
   FreeMem(pbKeyBlob);
   Wcrypt2.CryptDestroyKey(hKey);
   Wcrypt2.CryptReleaseContext(hProv, 0);
   // This is it right here.  I should not have to be calling this, or even doing this.  I know that this cannot be right.  Can anybody direct me in where I am wrong?  
   Str := Copy(Str, 1, L);
   Result := Str; // Hangs here.
  end;
end;


Cheers!
0
Comment
Question by:cjm30305
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
4 Comments
 
LVL 3

Expert Comment

by:LukA_YJK
ID: 8042473
Wow...
0
 

Expert Comment

by:CleanupPing
ID: 9317049
cjm30305:
This old question needs to be finalized -- accept an answer, split points, or get a refund.  For information on your options, please click here-> http:/help/closing.jsp#1 
EXPERTS:
Post your closing recommendations!  No comment means you don't care.
0
 
LVL 10

Expert Comment

by:kacor
ID: 9981559
No comment has been added lately, so it's time to clean up this TA.
I will leave a recommendation in the Cleanup topic area for this question:
       PAQ and refund points            contains a lot of knowledge
Please leave any comments here within the next seven days.

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

Some days and here is the Christmas Time. I wish good luck and good health for you all and for your loved ones

kacor
EE Cleanup Volunteer
0
 

Accepted Solution

by:
modulo earned 0 total points
ID: 10007387
PAQed, with points refunded (200)

modulo
Community Support Moderator
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses
Course of the Month14 days, 16 hours left to enroll

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question