Use Compute_RSA_Fundamentals_2Factors or Use GenerateAsymetricKeyPair To Create key can Encrypt can not Decrypt
`
procedure StoreRSAKeyPairPartsToStream(const Parts: RSAKeyStorePartSet; AKey: TRSAKeyPair; Store: TStream);
var
iVersion: integer;
pBuffer: TBytes;
begin
if AKey = nil then exit;
if Store = nil then exit;
pBuffer := TEncoding.UTF8.GetBytes(RSAKeySig);
Store.WriteBuffer(pBuffer, Length(pBuffer));
iVersion := RSAKeyStoreVersion;
Store.WriteBuffer( iVersion, SizeOf( iVersion));
Store.WriteBuffer( Parts, SizeOf( Parts));
if (PartN in Parts) and (AKey.F_RSA_n <> nil) then
StoreHugeCardinal_Primitive(AKey.F_RSA_n.Value, Store);
if (PartE in Parts) and (AKey.F_RSA_e <> nil) then
StoreHugeCardinal_Primitive(AKey.F_RSA_e.Value, Store);
if (PartD in Parts) and (AKey.F_RSA_d <> nil) then
StoreHugeCardinal_Primitive(AKey.F_RSA_d.Value, Store);
if (PartCRT in Parts) and
(AKey.F_RSA_p <> nil) and (AKey.F_RSA_q <> nil) and (AKey.F_RSA_dp <> nil)
and (AKey.F_RSA_dq <> nil) and (AKey.F_RSA_qinv <> nil) then
begin
StoreHugeCardinal_Primitive(AKey.F_RSA_p.Value, Store);
StoreHugeCardinal_Primitive(AKey.F_RSA_q.Value, Store);
StoreHugeCardinal_Primitive(AKey.F_RSA_dp.Value, Store);
StoreHugeCardinal_Primitive(AKey.F_RSA_dq.Value, Store);
StoreHugeCardinal_Primitive(AKey.F_RSA_qinv.Value, Store)
end;
end;
test code
var
n, e, d, Totient: THugeCardinal;
Count1: Integer;
p, q, dp, dq, qinv: THugeCardinal;
Lib: TCryptographicLibrary;
Codec: TCodec;
AKey: TRSAKeyPair;
s, y: string;
begin
Lib := TCryptographicLibrary.Create( nil);
Codec := TCodec.Create( nil);
AKey := TRSAKeyPair.CreateEmpty;
Totient := nil; n := nil; e := nil; d := nil; p := nil; q := nil; dp := nil; dq := nil; qinv := nil;
Count1 := 0;
Compute_RSA_Fundamentals_2Factors(KeyBitLength, StandardExponent, n, e, d, Totient,
p, q, dp, dq, qinv,
OnProgress, nil,
20,
FPool, Count1, FWasAborted);
AStream.Clear;
AKey.F_RSA_n := NewWrap(n);
AKey.F_RSA_e := NewWrap(e);
AKey.F_RSA_d := NewWrap(d);
StoreRSAKeyPairPartsToStream([PartN, PartE, PartD], AKey, AStream);
AStream.Position := 0;
Codec.InitFromStream(AStream);
Codec.EncryptAnsiString( 'test', y);
Codec.Reset;
Codec.DecryptAnsiString( s, y);
end;
Use Compute_RSA_Fundamentals_2Factors or Use GenerateAsymetricKeyPair To Create key can Encrypt can not Decrypt ` procedure StoreRSAKeyPairPartsToStream(const Parts: RSAKeyStorePartSet; AKey: TRSAKeyPair; Store: TStream); var iVersion: integer; pBuffer: TBytes; begin if AKey = nil then exit; if Store = nil then exit; pBuffer := TEncoding.UTF8.GetBytes(RSAKeySig); Store.WriteBuffer(pBuffer, Length(pBuffer)); iVersion := RSAKeyStoreVersion; Store.WriteBuffer( iVersion, SizeOf( iVersion)); Store.WriteBuffer( Parts, SizeOf( Parts)); if (PartN in Parts) and (AKey.F_RSA_n <> nil) then StoreHugeCardinal_Primitive(AKey.F_RSA_n.Value, Store); if (PartE in Parts) and (AKey.F_RSA_e <> nil) then StoreHugeCardinal_Primitive(AKey.F_RSA_e.Value, Store); if (PartD in Parts) and (AKey.F_RSA_d <> nil) then StoreHugeCardinal_Primitive(AKey.F_RSA_d.Value, Store); if (PartCRT in Parts) and (AKey.F_RSA_p <> nil) and (AKey.F_RSA_q <> nil) and (AKey.F_RSA_dp <> nil) and (AKey.F_RSA_dq <> nil) and (AKey.F_RSA_qinv <> nil) then begin StoreHugeCardinal_Primitive(AKey.F_RSA_p.Value, Store); StoreHugeCardinal_Primitive(AKey.F_RSA_q.Value, Store); StoreHugeCardinal_Primitive(AKey.F_RSA_dp.Value, Store); StoreHugeCardinal_Primitive(AKey.F_RSA_dq.Value, Store); StoreHugeCardinal_Primitive(AKey.F_RSA_qinv.Value, Store) end; end;
test code var n, e, d, Totient: THugeCardinal; Count1: Integer; p, q, dp, dq, qinv: THugeCardinal; Lib: TCryptographicLibrary; Codec: TCodec; AKey: TRSAKeyPair; s, y: string; begin Lib := TCryptographicLibrary.Create( nil); Codec := TCodec.Create( nil); AKey := TRSAKeyPair.CreateEmpty; Totient := nil; n := nil; e := nil; d := nil; p := nil; q := nil; dp := nil; dq := nil; qinv := nil; Count1 := 0; Compute_RSA_Fundamentals_2Factors(KeyBitLength, StandardExponent, n, e, d, Totient, p, q, dp, dq, qinv, OnProgress, nil, 20, FPool, Count1, FWasAborted); AStream.Clear; AKey.F_RSA_n := NewWrap(n); AKey.F_RSA_e := NewWrap(e); AKey.F_RSA_d := NewWrap(d); StoreRSAKeyPairPartsToStream([PartN, PartE, PartD], AKey, AStream); AStream.Position := 0; Codec.InitFromStream(AStream); Codec.EncryptAnsiString( 'test', y); Codec.Reset; Codec.DecryptAnsiString( s, y); end;
when run on .Decrypt will raise a av error