Advertisement
Guest User

Untitled

a guest
May 5th, 2016
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.43 KB | None | 0 0
  1. uses
  2. Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
  3. Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, Chilkat_v9_5_0_TLB;
  4.  
  5. ...
  6.  
  7. procedure TForm1.Button1Click(Sender: TObject);
  8. var
  9. rsa: TChilkatRsa;
  10. success: Integer;
  11. publicKey: PWideChar;
  12. privateKey: PWideChar;
  13. plainText: PWideChar;
  14. rsaEncryptor: TChilkatRsa;
  15. usePrivateKey: Integer;
  16. encryptedStr: PWideChar;
  17. rsaDecryptor: TChilkatRsa;
  18. decryptedStr: PWideChar;
  19.  
  20. begin
  21. rsa := TChilkatRsa.Create(Self);
  22.  
  23. success := rsa.UnlockComponent('Anything for 30-day trial');
  24. if (success <> 1) then
  25. begin
  26. Memo1.Lines.Add('RSA component unlock failed');
  27. Exit;
  28. end;
  29.  
  30. // This example also generates the public and private
  31. // keys to be used in the RSA encryption.
  32. // Normally, you would generate a key pair once,
  33. // and distribute the public key to your partner.
  34. // Anything encrypted with the public key can be
  35. // decrypted with the private key. The reverse is
  36. // also true: anything encrypted using the private
  37. // key can be decrypted using the public key.
  38.  
  39. // Generate a 1024-bit key. Chilkat RSA supports
  40. // key sizes ranging from 512 bits to 4096 bits.
  41. success := rsa.GenerateKey(1024);
  42. if (success <> 1) then
  43. begin
  44. Memo1.Lines.Add(rsa.LastErrorText);
  45. Exit;
  46. end;
  47.  
  48. // Keys are exported in XML format:
  49. publicKey := rsa.ExportPublicKey();
  50. privateKey := rsa.ExportPrivateKey();
  51.  
  52. plainText := 'Encrypting and decrypting should be easy!';
  53.  
  54. // Start with a new RSA object to demonstrate that all we
  55. // need are the keys previously exported:
  56. rsaEncryptor := TChilkatRsa.Create(Self);
  57.  
  58. // Encrypted output is always binary. In this case, we want
  59. // to encode the encrypted bytes in a printable string.
  60. // Our choices are "hex", "base64", "url", "quoted-printable".
  61. rsaEncryptor.EncodingMode := 'hex';
  62.  
  63. // We'll encrypt with the public key and decrypt with the private
  64. // key. It's also possible to do the reverse.
  65. success := rsaEncryptor.ImportPublicKey(publicKey);
  66.  
  67. usePrivateKey := 0;
  68. encryptedStr := rsaEncryptor.EncryptStringENC(plainText,usePrivateKey);
  69. Memo1.Lines.Add(encryptedStr);
  70.  
  71. // Now decrypt:
  72. rsaDecryptor := TChilkatRsa.Create(Self);
  73.  
  74. rsaDecryptor.EncodingMode := 'hex';
  75. success := rsaDecryptor.ImportPrivateKey(privateKey);
  76.  
  77. usePrivateKey := 1;
  78. decryptedStr := rsaDecryptor.DecryptStringENC(encryptedStr,usePrivateKey);
  79.  
  80. Memo1.Lines.Add(decryptedStr);
  81.  
  82. end;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement