rt,百分酬谢

Delphi(Pascal) code
procedure btnEncryptClick(Sender: TObject); var Cipher: TDCP_cipher; // the cipher to use CipherIV: array of byte; // the initialisation vector (for chaining modes) Hash: TDCP_hash; // the hash to use HashDigest: array of byte; // the result of hashing the passphrase with the salt Salt: array[0..7] of byte; // a random salt to help prevent precomputated attacks strmInput, strmOutput: TFileStream; i: integer; begin if FileExists(boxOutputFile.Text) then if (MessageDlg(Output file already exists. Overwrite?,mtConfirmation,mbYesNoCancel,0) <> mrYes) then Exit; strmInput := nil; strmOutput := nil; try strmInput := TFileStream.Create(boxInputFile.Text,fmOpenRead); strmOutput := TFileStream.Create(boxOutputFile.Text,fmCreate); Hash := TDCP_hash(cbxHash.Items.Objects[cbxHash.ItemIndex]); SetLength(HashDigest,Hash.HashSize div 8); for i := 0 to 7 do Salt[i] := Random(256); // just fill the salt with random values (crypto secure PRNG would be better but not _really_ necessary) strmOutput.WriteBuffer(Salt,Sizeof(Salt)); // write out the salt so we can decrypt! Hash.Init; Hash.Update(Salt[0],Sizeof(Salt)); // hash the salt Hash.UpdateStr(boxPassphrase.Text); // and the passphrase Hash.Final(HashDigest[0]); // store the output in HashDigest Cipher := TDCP_cipher(cbxCipher.Items.Objects[cbxCipher.ItemIndex]); if (Cipher is TDCP_blockcipher) then // if the cipher is a block cipher we need an initialisation vector begin SetLength(CipherIV,TDCP_blockcipher(Cipher).BlockSize div 8); for i := 0 to (Length(CipherIV) - 1) do CipherIV[i] := Random(256); // again just random values for the IV strmOutput.WriteBuffer(CipherIV[0],Length(CipherIV)); // write out the IV so we can decrypt! Cipher.Init(HashDigest[0],Min(Cipher.MaxKeySize,Hash.HashSize),CipherIV); // initialise the cipher with the hash as key TDCP_blockcipher(Cipher).CipherMode := cmCBC; // use CBC chaining when encrypting end else Cipher.Init(HashDigest[0],Min(Cipher.MaxKeySize,Hash.HashSize),nil); // initialise the cipher with the hash as key Cipher.EncryptStream(strmInput,strmOutput,strmInput.Size); // encrypt the entire file Cipher.Burn; // important! get rid of keying information strmInput.Free; strmOutput.Free; MessageDlg(File encrypted,mtInformation,[mbOK],0); except strmInput.Free; strmOutput.Free; MessageDlg(An error occurred while processing the file,mtError,[mbOK],0); end; end;

顺问,有人成功在bcb2007上使用dcpcrypt过么?

http://blog.csdn.net/phphot/archive/2008/03/13/2176280.aspx

看看这个,很容易改的

要注意其中CipherIV、HashDigest用到了pascal动态数组SetLength(),你要自己改成
char *CipherIV,*HashDigest;
CipherIV=new char[TDCP_blockcipher(Cipher).BlockSize/8];
HashDigest=new char[Hash.HashSize/8];

最后别忘了释放
delete CipherIV;
delete HashDigest;