我正在使用 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 - 在编码/解密时无法设置密码”