我正在使用Windows 8 64位,Delphi XE7和
所以我开始做一个自己的“演示”应用程序,以了解它是如何工作的,当我尝试从备忘录中解密加密的字符串时,我也被卡住了。
Keygeneration
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;
Decrypt(例外情况“TSimpleCodec.Init在未初始化时重置”抛出行代码.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;
那么我做错了什么?我还尝试使用“可视”组件,而不是像在加密过程中那样在运行时创建它们。
=============================================== =====================
当我通过此代码将私钥和公共密钥加载时,它正在工作
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 - 无法在加密/解密时设置密码”
答案 0 :(得分:1)
TurboPower LockBox 3(来自https://github.com/SeanBDurkin/tplockbox的v3.6.2.0)附带了一个演示程序,可以在这个问题中执行某些您想要做的事情。请参阅程序" LockBox3_Demo.exe",选项卡" 4。 RSA - 密钥生成和存储"和" 5。 RSA - Signature&验证"
我在以下列表中稍微清理了您发布的代码。这对我有用(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没有显示调用DecryptRSA_String()的外层代码。我猜他用str
的空实际参数值调用了DecryptRSA_String()。这会抛出TSimpleCodec.Init Reset when not initalized
异常。