首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >锁盒3-未初始化时重置

锁盒3-未初始化时重置
EN

Stack Overflow用户
提问于 2015-08-04 08:47:32
回答 1查看 1.2K关注 0票数 1

我使用的是Windows 8 64位、Delphi XE7和

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

密钥生成

代码语言:javascript
复制
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;

加密

代码语言:javascript
复制
Call
  EncryptMemoOutput.Lines.Add(EncryptRSA_String(EncryptMemoInput.Text));

操作步骤

代码语言:javascript
复制
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重置时未初始化“抛出行codec.DecryptString(str,base64Ciphertext);)

代码语言:javascript
复制
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;

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

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

更新MON 10.08.2015

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

代码语言:javascript
复制
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,因为我也分别保存了它,它不工作

代码语言:javascript
复制
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 -无法在enc/解密时设置密码“。

EN

回答 1

Stack Overflow用户

发布于 2015-08-05 04:02:32

TurboPower LockBox 3(来自https://github.com/SeanBDurkin/tplockbox的V3.6.2.0版)附带了一个演示程序,它完成了您在这个问题中试图做的一些。请参阅程序"LockBox3_Demo.exe",选项卡“4.rsa-密钥生成和存储”和“5.rsa-签名和验证”。

在下面的清单中,我稍微清除了您发布的代码。这适用于我(XE7,Win32,调试)。调用Button1Click()来运行RSA加密/解密的往返测试。

解决方案

代码语言:javascript
复制
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没有显示调用DecryptRSA_String()的外部级别代码。我猜他调用DecryptRSA_String()时,str的实际参数值为空。这将引发TSimpleCodec.Init Reset when not initalized异常。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/31804666

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档