How to use AES-256 encryption in lockbox 3 using delphi

百般思念 提交于 2019-12-04 05:30:27

问题


I've downloaded Lockbox3 about a week ago and i can't use it, and i couldn't understand the demo because it's complex i couldn't get the codes i want from it, I'd like to use lockbox 3 AES-256 encryption to encrypt strings in Delphi.


回答1:


The method and property names pretty much say it all. Here is a method which encrypts a string and then decrypts it back again, assuming you've setup the codec properties at design time, which are also self-describing.

procedure TForm1.actEncryptStringExecute( Sender: TObject );
var
  Plaintext, sReconstructedPlaintext: string;
  base64Ciphertext: ansistring;
begin
sPlainText := 'I love LockBox 3!';
if not InputQuery( 'Plaintext', 'Enter plaintext that you want to encrypt (UTF-16LE encoding):', sPlainText) then exit;
codec.EncryptString( sPlaintext, base64Ciphertext);
ShowMessageFmt('The base64 encoding of the encoded ciphertext is'#13#10+'%s',[base64Ciphertext]);
codec.DecryptString( sReconstructedPlaintext, base64Ciphertext);
ShowMessageFmt('After decryption, this decrypts back to %s',[sReconstructedPlaintext])
end;

Have another look at the demo program. The handler for Encrypt button, encrypts a file instead of a string. That aside, if you strip away the decorative fluff, like posting information to a memo, and handling exceptions if the user specified a non-existant file, its increddibly simple - it basically boils down to one line...

codecMainDemo.EncryptFile( edtPlaintextFile.Text, edtCiphertextFile.Text );

To encrypt a string, you call EncryptString(). To encrypt a file you call EncryptFile().

The demo shows the setup, to wit:

  1. Put an TCryptographicLibrary component on your form;
  2. Put a TCodec component on your form;
  3. Select your prefered cipher
  4. Select your prefered chaining mode; and
  5. Set the password

and Bob's your uncle!

Let me know if you have any problems.



来源:https://stackoverflow.com/questions/9449613/how-to-use-aes-256-encryption-in-lockbox-3-using-delphi

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!