1

我正在使用 Windows 8 64 位、Delphi XE7 和

所以我开始做一个自己的“演示”应用程序来了解它是如何工作的,当我尝试从备忘录中解密加密字符串时,我也陷入了困境。

在此处输入图像描述 在此处输入图像描述

密钥生成

procedure TMainForm.Generate_RSA_Keys;
var
  Signatory1: TSignatory;
  codecRSA: TCodec;
  CryptographicLibrary1: TCryptographicLibrary;
  msPublic,msPrivate:TMemoryStream;
begin
  Application.ProcessMessages;
  //=================ini====================
  codecRSA:=TCodec.Create(nil);
  CryptographicLibrary1:=TCryptographicLibrary.Create(nil);
  Signatory1:=TSignatory.Create(nil);
  //=============TCodec===================
  codecRSA.CryptoLibrary  := CryptographicLibrary1;
  codecRSA.StreamCipherId := 'native.RSA';
  codecRSA.ChainModeId:= 'native.CBC';
  codecRSA.AsymetricKeySizeInBits := 1024;

  //====Signatory1=====================
  Signatory1.Codec :=codecRSA;

  //==========Save Keys==================
  msPublic:=TMemoryStream.Create;
  msPrivate:=TMemoryStream.Create;
  if Signatory1.GenerateKeys then
  begin
    Signatory1.StoreKeysToStream(msPublic,[partPublic]);
    Signatory1.StoreKeysToStream(msPrivate,[partPrivate]);
    msPublic.SaveToFile(Keypath + PublicKey);
    msPrivate.SaveToFile(Keypath + PrivateKey);
  end;
  msPublic.Free;
  msPrivate.Free;
  codecRSA.Free;
  CryptographicLibrary1.Free;
  Signatory1.Free;
end;

加密

Call
  EncryptMemoOutput.Lines.Add(EncryptRSA_String(EncryptMemoInput.Text));

程序

function TMainForm.EncryptRSA_String(str:string):String;
var
  Signatory1: TSignatory;
  codecRSA: TCodec;
  CryptographicLibrary1: TCryptographicLibrary;
  ms:TMemoryStream;
  base64Ciphertext: string;
begin
  Result :='';
  //=================ini====================
  codecRSA:=TCodec.Create(nil);
  CryptographicLibrary1:=TCryptographicLibrary.Create(nil);
  Signatory1:=TSignatory.Create(nil);
  //=============TCodec===================
  codecRSA.CryptoLibrary  := CryptographicLibrary1;
  codecRSA.StreamCipherId := 'native.RSA';
  codecRSA.ChainModeId:= 'native.CBC';
  codecRSA.AsymetricKeySizeInBits := 1024;

  //====Signatory1=====================
  Signatory1.Codec :=codecRSA;
  //===Load public key=============
  ms:=TMemoryStream.Create;
  ms.LoadFromFile(Keypath + PublicKey);
  Signatory1.LoadKeysFromStream(ms,[partPublic]);
  codecRSA.EncryptString( str, base64Ciphertext);
  codecRSA.EncryptUtf8string( str, base64Ciphertext);
  Result := base64Ciphertext;
  //==free===========
  ms.Free;
  codecRSA.Free;
  CryptographicLibrary1.Free;
  Signatory1.Free;
end;

解密(异常“TSimpleCodec.Init Reset when not initalized”在 codec.DecryptString(str, base64Ciphertext); 行抛出)

function  TMainForm.DecryptRSA_String(str:String):string ;
var
  ms:TMemoryStream;
  base64Ciphertext: String;
begin
  Result :='';
  codec.Reset;
  //===Load public key=============
  ms:=TMemoryStream.Create;
  ms.LoadFromFile(Keypath + PrivateKey);
  Signatory.LoadKeysFromStream(ms,[partPrivate]);
  codec.DecryptString(str, base64Ciphertext);
  Result := base64Ciphertext;

  //==free===========
  ms.Free;
end;

那么我做错了什么?我还尝试使用“可视”组件,而不是像在加密过程中那样在运行时创建它们。

==================================================== ===================

更新星期一 10.08.2015

当我通过此代码加载私有和公共密钥时,它的工作

procedure TMainForm.Button6Click(Sender: TObject);
var
  Store: TStream;
  sRSAKeyFileName, Plain, a: String;
  sPlaintext, sReconstructedPlaintext: string;
  base64Ciphertext: String;
begin
  sRSAKeyFileName := 'C:\Users\Robin\Desktop\Dateien\Development\OpenSSL\Delphi\Win32\Debug\Keys\Lockbox.key';
  Store := TFileStream.Create( sRSAKeyFileName, fmOpenRead);
try
  Store.Position := 0;
  Signatory.LoadKeysFromStream( Store, [partPublic, partPrivate]);

  sPlainText := 'I love LockBox 3!';
  codec.EncryptString( sPlaintext, base64Ciphertext);
  codec.DecryptString( sReconstructedPlaintext, base64Ciphertext);
  ShowMessage(base64Ciphertext + #13#10 + sReconstructedPlaintext);
finally
  Store.Free
  end
end;

当我尝试加载密钥 serpatley 时,因为我也单独保存了它,所以它不起作用

procedure TMainForm.btEncryptClick(Sender: TObject);
var
  g, f: TFileStream;
  s: String;
begin
  g := TFileStream.Create('C:\Users\Robin\Desktop\Dateien\Development\OpenSSL\Delphi\Win32\Debug\Keys\public.key', fmOpenRead); //openssl key
  g.Position := 0;
  Signatory.LoadKeysFromStream(g, [partPublic]);

  s := EncryptMemoInput.Text;
  codec.EncryptString(s, sCryped);
  EncryptMemoOutput.lines.Add(sCryped);

  codec.Reset;

  f := TFileStream.Create('C:\Users\Robin\Desktop\Dateien\Development\OpenSSL\Delphi\Win32\Debug\Keys\private.key', fmOpenRead);
  Signatory.LoadKeysFromStream(f, [partPrivate]);
  codec.EncryptString(sUncrypted, sCryped);
  ShowMessage(sUncrypted);


  g.Free;
  f.Free;
end;

加密字符串确实有效,不加密它会抛出“TSimpleCodec.Init - 未初始化时重置”。

当我调用“codec.EncryptString(sUncrypted, sCryped);”时引发

当我关闭应用程序时,它会抛出“TSimpleCodec.Init - 在编码/解密时无法设置密码”

4

1 回答 1

1

TurboPower LockBox 3(来自https://github.com/SeanBDurkin/tplockbox的 v3.6.2.0 )带有一个演示程序,可以完成您在这个问题中尝试做的一些事情。请参阅程序“LockBox3_Demo.exe”,选项卡“4. RSA - 密钥生成和存储”和“5. RSA - 签名和验证”。

我在以下清单中稍微清理了您发布的代码。这对我有用(XE7、Win32、DEBUG)。调用 Button1Click() 运行 RSA 加密/解密的往返测试。

一个解法

uses TPLB3.CryptographicLibrary, TPLB3.Signatory, TPLB3.Codec,
     TPLB3.Asymetric;
{$R *.dfm}

procedure Generate_RSA_Keys( var msPublic, msPrivate: TMemoryStream);
var
  Signatory1: TSignatory;
  codecRSA: TCodec;
  CryptographicLibrary1: TCryptographicLibrary;
begin
  Application.ProcessMessages;
  //=================ini====================
  codecRSA:=TCodec.Create(nil);
  CryptographicLibrary1:=TCryptographicLibrary.Create(nil);
  Signatory1:=TSignatory.Create(nil);
  //=============TCodec===================
  codecRSA.CryptoLibrary  := CryptographicLibrary1;
  codecRSA.StreamCipherId := 'native.RSA';
  codecRSA.ChainModeId:= 'native.CBC';
  codecRSA.AsymetricKeySizeInBits := 1024;

  //====Signatory1=====================
  Signatory1.Codec :=codecRSA;

  //==========Save Keys==================
  msPublic:=TMemoryStream.Create;
  msPrivate:=TMemoryStream.Create;
  if Signatory1.GenerateKeys then
  begin
    Signatory1.StoreKeysToStream(msPublic,[partPublic]);
    Signatory1.StoreKeysToStream(msPrivate,[partPrivate]);
    msPublic.Position := 0;
    msPrivate.Position := 0;
  end;
  codecRSA.Free;
  CryptographicLibrary1.Free;
  Signatory1.Free;
end;


function EncryptRSA_String( const str:string; msPublic: TMemoryStream): string;
var
  Signatory1: TSignatory;
  codecRSA: TCodec;
  CryptographicLibrary1: TCryptographicLibrary;
  base64Ciphertext: string;
begin
  Result :='';
  //=================ini====================
  codecRSA :=TCodec.Create(nil);
  CryptographicLibrary1 := TCryptographicLibrary.Create(nil);
  Signatory1 :=TSignatory.Create(nil);
  //=============TCodec===================
  codecRSA.CryptoLibrary  := CryptographicLibrary1;
  codecRSA.StreamCipherId := 'native.RSA';
  codecRSA.ChainModeId:= 'native.CBC';
  codecRSA.AsymetricKeySizeInBits := 1024;

  //====Signatory1=====================
  Signatory1.Codec :=codecRSA;
  //===Load public key=============
  Signatory1.LoadKeysFromStream( msPublic, [partPublic]);
  msPublic.Position := 0;
  codecRSA.EncryptString( str, base64Ciphertext, TEncoding.UTF8);
  Result := base64Ciphertext;
  //==free===========
  codecRSA.Free;
  CryptographicLibrary1.Free;
  Signatory1.Free;
end;


function DecryptRSA_String( const base64Ciphertext: string; const msPublic, msPrivate: TMemoryStream): string ;
var
  Signatory1: TSignatory;
  codecRSA: TCodec;
  CryptographicLibrary1: TCryptographicLibrary;
begin
  Result :='';
  //=================ini====================
  codecRSA :=TCodec.Create(nil);
  CryptographicLibrary1 := TCryptographicLibrary.Create(nil);
  Signatory1 :=TSignatory.Create(nil);
  //=============TCodec===================
  codecRSA.CryptoLibrary  := CryptographicLibrary1;
  codecRSA.StreamCipherId := 'native.RSA';
  codecRSA.ChainModeId:= 'native.CBC';
  codecRSA.AsymetricKeySizeInBits := 1024;

  //====Signatory1=====================
  Signatory1.Codec :=codecRSA;
  Signatory1.LoadKeysFromStream( msPrivate,[partPrivate]);
  codecRSA.DecryptString( result, base64Ciphertext, TEncoding.UTF8);

  //==free===========
  codecRSA.Free;
  CryptographicLibrary1.Free;
  Signatory1.Free;
end;


procedure TForm27.Button1Click(Sender: TObject);
var
  msPublic,msPrivate: TMemoryStream;
  PlainText: string;
  CipherText: string;
  Recon: string;
begin
  PlainText := 'Some text';
  Generate_RSA_Keys( msPublic,msPrivate);
  CipherText := EncryptRSA_String( PlainText, msPublic);
  Recon      := DecryptRSA_String( CipherText, msPublic, msPrivate);
  msPublic.Free;
  msPrivate.Free;
  if Recon = PlainText then
      ShowMessage( 'PASS')
    else
      ShowMessage( 'FAIL')
end;

OP 代码有什么问题?

OP 没有显示调用 DecryptRSA_String() 的外层代码。我猜他用一个空的实际参数值调用了 DecryptRSA_String() str。这将引发该TSimpleCodec.Init Reset when not initalized异常。

于 2015-08-05T04:02:32.600 回答