请问大家有RSA1024位加密解密算法吗?麻烦大家指点一下吧。我这里搜索了两周了还没有找到,实在是感觉自己效率太低了。
原来找到的好像都不是很好用。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;
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;procedure LbEncodeBase64(InStream, OutStream : TStream); 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;
procedure LbEncodeBase64(InStream, OutStream : TStream);
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;
以上为上次下载的RSA算法,发现加密后数据的末尾有好多的零。不知道这个加密算法是否有问题。
我就死马当活马医,用了发现,解密也成功了,就是解密的结果不是我的加密数据。请问是不是加密算法有问题??
另外,解密算法是税控卡中的算法,他们的算法应该没有问题,所以我在怀疑我下载的算法有问题,但是在网上再也没有下载到好用的算法了。请大家帮忙看看吧。
pRSA = ^RSA;
RSA = record
pad: integer;
version: integer;
meth: pRSA_METHOD;
n: pBIGNUM;
e: pBIGNUM;
d: pBIGNUM;
p: pBIGNUM;
q: pBIGNUM;
dmp1: pBIGNUM;
dmq1: pBIGNUM;
iqmp: pBIGNUM;
ex_data: CRYPTO_EX_DATA;
references: integer;
flags: integer;
_method_mod_n: pBN_MONT_CTX;
_method_mod_p: pBN_MONT_CTX;
_method_mod_q: pBN_MONT_CTX;
bignum_data: ^byte;
blinding: ^BN_BLINDING;
end;
function RSA_new: pRSA; cdecl;external 'libeay32.dll';
procedure RSA_free(r: pRSA); cdecl;external 'libeay32.dll';
function RSA_new_method(method: pRSA_METHOD): pRSA; cdecl;external 'libeay32.dll';
function RSA_size(pkey: pRSA): integer; cdecl;external 'libeay32.dll';
function RSA_generate_key(bits: integer; exp: Cardinal;
progress: TProgressCallbackFunction; cb_arg: pointer):pRSA; cdecl;external 'libeay32.dll';
function RSA_check_key(arg0: pRSA): integer; cdecl;external 'libeay32.dll';
function RSA_public_encrypt(flen: integer; from: PCharacter; _to: PCharacter; rsa: pRSA; padding: integer): integer; cdecl;external 'libeay32.dll';
function RSA_private_encrypt(flen: integer; from: PCharacter; _to: PCharacter; rsa: pRSA; padding: integer): integer; cdecl;external 'libeay32.dll';
function RSA_public_decrypt(flen: integer; from: PCharacter; _to: PCharacter; rsa: pRSA; padding: integer): integer; cdecl;external 'libeay32.dll';
function RSA_private_decrypt(flen: integer; from: PCharacter; _to: PCharacter; rsa: pRSA; padding: integer): integer; cdecl;external 'libeay32.dll';
procedure Tfrmbc_bs_tcpnet.FormShow(Sender: TObject);
{var
ss,srandom,ssrandom:string;
begin
ServerSocket1.ServerType:=stThreadBlocking;
ServerSocket1.close;
ServerSocket1.open;
iThreadCount:=0;
self.StatusBar1.Panels[0].Text:='服务器正在监听';
self.StatusBar1.Panels[1].Text:='端口:1000,最大连接数目:10';
self.StatusBar1.Panels[2].Text:='连接数目:'+inttostr(iThreadCount);}
var
ss,skkbhN:string;
NN1,NN2,NN,EE,srandom:string;
Checkcode:string; s1,s2:Pointer;
command:string;
ssrandom,sCrsa:PCharacter;
prsaData:pRSA;
rn,ilen:Integer;
begin
//ss:=Cutblank_wanjun(showstr(data));
skkbhN:='9876543210123626';
DM1.DataModule1.Tado_SK_JKGLSJ.Close;
DM1.DataModule1.Tado_SK_JKGLSJ.SQL.Clear;
DM1.DataModule1.Tado_SK_JKGLSJ.SQL.Text:='select * from SK_JKGLSJ where SKKBH ='''+skkbhN+'''';
DM1.DataModule1.Tado_SK_JKGLSJ.Open;
if DM1.DataModule1.Tado_SK_JKGLSJ.RecordCount>0 then
begin
NN1:=DM1.DataModule1.Tado_SK_JKGLSJ.FieldByName('NN1').AsString;
NN2:=DM1.DataModule1.Tado_SK_JKGLSJ.FieldByName('NN2').AsString;
NN:=NN1+NN2;
EE:=DM1.DataModule1.Tado_SK_JKGLSJ.FieldByName('EE').AsString;
end;
ssrandom:='1234567890123456'; s1:=Pointer(hextostring(NN));
s2:=Pointer(hextostring(EE));
prsaData:=RSA_new;
prsaData.n:=BN_new();
prsaData.e:=BN_new();
prsaData.n:= BN_bin2bn(s1,128,prsaData.n);
prsaData.e:= BN_bin2bn(s2,4,prsaData.e); sCrsa:='';
ilen:=length(ssrandom);
rn:=RSA_public_encrypt(ilen,Pointer(ssrandom),Pointer(sCrsa),prsaData,RSA_PKCS1_PADDING);
if rn=-1 then
begin
command:=hextostring('0505');
end
else begin
Checkcode:=checksum(sCrsa,128);
command:=hextostring(sCrsa+Checkcode);
end;
showmessage(command);
BN_clear_free(prsaData.n);
BN_clear_free(prsaData.e);
RSA_free(prsaData);
end;
[email protected]
密码:delphi
这种写法,比人用MADEDIT一下就看到了密码了。
我一般都是模数,公钥,等用数字分开写。