这个RSA加密算法好用吗?为什么我用1024位,产生了172个字节?请高手解说一下,谢谢
function TLbRSA.EncryptString(const InString : string) : string;
{ encrypt string data with RSA public key }
begin
Result := RSAEncryptString(InString, FPublicKey, True);
end;
{ -------------------------------------------------------------------------- }
procedure TLbRSA.GenerateKeyPair;
{ generate RSA public/private key pair }
begin
if Assigned(FPrivateKey) then
FPrivateKey.Free;
if Assigned(FPublicKey) then
FPublicKey.Free;
try
GenerateRSAKeysEx(FPrivateKey, FPublicKey, FKeySize,
FPrimeTestIterations, RSACallback);
except
raise Exception.Create(sRSAKeyPairErr);
end;
end;
{ -------------------------------------------------------------------------- }
function TLbRSA.OutBufSizeNeeded(InBufSize : Cardinal) : Cardinal;
{ return size of ciphertext buffer required to encrypt plaintext InBuf }
var
BlkCount : Cardinal;
begin
BlkCount := InBufSize div cRSAPlainBlockSize[FKeySize]; {!!.02}
if (InBufSize mod cRSAPlainBlockSize[FKeySize]) > 0 then {!!.02}
Inc(BlkCount);
Result := BlkCount * cRSACipherBlockSize[FKeySize]; {!!.02}
end;
{ -------------------------------------------------------------------------- }
procedure TLbRSA.RSACallback(var Abort : Boolean);
{ pass callback on via OnProgress event }
begin
Abort := False;
if Assigned(FOnProgress) then
FOnProgress(Self, Abort);
end;
{ -------------------------------------------------------------------------- }
{!!.02}
procedure TLbRSA.SetKeySize(Value : TLbAsymKeySize);
begin
FKeySize := Value;
FPublicKey.KeySize := FKeySize;
FPrivateKey.KeySize := FKeySize;
end;
function TLbRSA.EncryptString(const InString : string) : string;
{ encrypt string data with RSA public key }
begin
Result := RSAEncryptString(InString, FPublicKey, True);
end;
{ -------------------------------------------------------------------------- }
procedure TLbRSA.GenerateKeyPair;
{ generate RSA public/private key pair }
begin
if Assigned(FPrivateKey) then
FPrivateKey.Free;
if Assigned(FPublicKey) then
FPublicKey.Free;
try
GenerateRSAKeysEx(FPrivateKey, FPublicKey, FKeySize,
FPrimeTestIterations, RSACallback);
except
raise Exception.Create(sRSAKeyPairErr);
end;
end;
{ -------------------------------------------------------------------------- }
function TLbRSA.OutBufSizeNeeded(InBufSize : Cardinal) : Cardinal;
{ return size of ciphertext buffer required to encrypt plaintext InBuf }
var
BlkCount : Cardinal;
begin
BlkCount := InBufSize div cRSAPlainBlockSize[FKeySize]; {!!.02}
if (InBufSize mod cRSAPlainBlockSize[FKeySize]) > 0 then {!!.02}
Inc(BlkCount);
Result := BlkCount * cRSACipherBlockSize[FKeySize]; {!!.02}
end;
{ -------------------------------------------------------------------------- }
procedure TLbRSA.RSACallback(var Abort : Boolean);
{ pass callback on via OnProgress event }
begin
Abort := False;
if Assigned(FOnProgress) then
FOnProgress(Self, Abort);
end;
{ -------------------------------------------------------------------------- }
{!!.02}
procedure TLbRSA.SetKeySize(Value : TLbAsymKeySize);
begin
FKeySize := Value;
FPublicKey.KeySize := FKeySize;
FPrivateKey.KeySize := FKeySize;
end;
解决方案 »
- 发送邮件 idsmtp、NMSMTP均失败(错误:不是 用户被锁定,就是 Authentication failed )
- CSDN上delphi版在哪?
- 一个关于数组传递的入门问题.
- 绝对难题
- 能判断某个菜单下有没有子菜单吗?
- 请问在一个MDI窗体菜单中,如何做一个DLL
- 关于结构体的问题
- IntraWeb中如何连接ADO?
- delphi7 webservi问题。Invalid Content type text/html,SOAP expect text/xml
- 我想问问,用delphi如何操作autocad???????
- 怎么利用磁盘索引,快速查找文件?
- Application.messagebox的默认按钮的问题
function RSAEncryptString(const InString : string;
Key : TLbRSAKey; Encrypt : Boolean) : string;
{ encrypt/decrypt string data with RSA key }
var
InStream : TMemoryStream;
OutStream : TMemoryStream;
WorkStream : TMemoryStream;
begin
InStream := TMemoryStream.Create;
OutStream := TMemoryStream.Create;
WorkStream := TMemoryStream.Create;
InStream.Write(InString[1], Length(InString));
InStream.Position := 0; if Encrypt then begin
RSAEncryptStream(InStream, WorkStream, Key, True);
WorkStream.Position := 0;
LbEncodeBase64(WorkStream, OutStream);
end else begin
LbDecodeBase64(InStream, WorkStream);
WorkStream.Position := 0;
RSAEncryptStream(WorkStream, OutStream, Key, False);
end;
OutStream.Position := 0;
SetLength(Result, OutStream.Size);
OutStream.Read(Result[1], OutStream.Size); InStream.Free;
OutStream.Free;
WorkStream.Free;
end;
var
I, O, Count : Integer;
InBuf : array[1..45] of Byte;
OutBuf : array[0..62] of Char;
Temp : Byte;
begin
FillChar(OutBuf, Sizeof(OutBuf), #0); repeat
Count := InStream.Read(InBuf, SizeOf(InBuf));
if Count = 0 then Break;
I := 1;
O := 0;
while I <= (Count-2) do begin
{ Encode 1st byte }
Temp := (InBuf[I] shr 2);
OutBuf[O] := Char(Lb64Table[Temp and $3F]); { Encode 1st/2nd byte }
Temp := (InBuf[I] shl 4) or (InBuf[I+1] shr 4);
OutBuf[O+1] := Char(Lb64Table[Temp and $3F]); { Encode 2nd/3rd byte }
Temp := (InBuf[I+1] shl 2) or (InBuf[I+2] shr 6);
OutBuf[O+2] := Char(Lb64Table[Temp and $3F]); { Encode 3rd byte }
Temp := (InBuf[I+2] and $3F);
OutBuf[O+3] := Char(Lb64Table[Temp]); Inc(I, 3);
Inc(O, 4);
end; { Are there odd bytes to add? }
if (I <= Count) then begin
Temp := (InBuf[I] shr 2);
OutBuf[O] := Char(Lb64Table[Temp and $3F]); { One odd byte }
if I = Count then begin
Temp := (InBuf[I] shl 4) and $30;
OutBuf[O+1] := Char(Lb64Table[Temp and $3F]);
OutBuf[O+2] := '=';
{ Two odd bytes }
end else begin
Temp := ((InBuf[I] shl 4) and $30) or ((InBuf[I+1] shr 4) and $0F);
OutBuf[O+1] := Char(Lb64Table[Temp and $3F]);
Temp := (InBuf[I+1] shl 2) and $3C;
OutBuf[O+2] := Char(Lb64Table[Temp and $3F]);
end;
{ Add padding }
OutBuf[O+3] := '=';
Inc(O, 4);
end; { Write encoded block to stream }
OutStream.Write(OutBuf, O);
until Count < SizeOf(InBuf);
end;