Is there an equivalent encryption procedure for the given code below. The current implementation does not support encryption and throws a 'Feature not supported.' exception.
The routine below successfully decrypted this cipher 'G6DR0607ZXRu24envBxPRA==' to its plain text value '1'. I need to reverse the process and get the same given cipher.
I hope someone can help me provide a working equivalent code to encrypt a plain text value, using the same procedure found in the DecryptAES256 routine.
const
Key: array of byte = [$61,$A6,$02,$D1,$E6,$89,$87,$A3,$7C,$0B,$54,$D2,$64,$7D,
$B9,$41,$D0,$E6,$56,$DE,$CF,$A2,$5B,$6C,$76,$4A,$BB,$FA,
$DB,$CD,$41,$2D];
IV: array of byte = [$86,$78,$1C,$D2,$66,$91,$F7,$91,$3B,$2A,$44,$10,$DF,$38,
$E4,$47];
function DecryptAES256(const Value: String): String;
var
Codec: TCodec;
CryptographicLibrary: TCryptographicLibrary;
CipherStream, PlainTextStream: TStream;
buffer: array of Byte;
begin
Codec := TCodec.Create( nil);
CryptographicLibrary := TCryptographicLibrary.Create( nil);
CipherStream := TMemoryStream.Create;
PlainTextStream := TMemoryStream.Create;
try
Codec.CryptoLibrary := CryptographicLibrary;
CryptographicLibrary.RegisterStreamCipher(StreamToBlock_Adapter_CSharpVariant);
Codec.StreamCipherId := 'CSharp.StreamToBlock';
Codec.BlockCipherId := Format( AES_ProgId, [256]);
Codec.AsymetricKeySizeInBits := 256;
Codec.Cipher := '[AES-256*]';
Codec.ChainModeId := CBC_ProgId;
CipherStream.WriteBuffer( Key[0], Length(Key));
CipherStream.Position := 0;
Codec.InitFromStream(CipherStream);
CipherStream.Size := 0;
CipherStream.WriteBuffer( IV[0], Length(IV));
Base64_to_stream(Value, CipherStream);
CipherStream.Position := 0;
Codec.DecryptStream(PlainTextStream, CipherStream);
PlainTextStream.Position := 0;
SetLength(buffer, PlainTextStream.Size);
PlainTextStream.ReadBuffer(buffer[0], Length(buffer));
Result := StripNonAscii(ByteToString(buffer));
finally
CipherStream.Free;
PlainTextStream.Free;
Codec.Free;
CryptographicLibrary.Free;
end;
end;
Here is a sample program to encrypt/decrypt UTF-16LE strings in AES-256, modifying the padding to be interoperable with C# stype of padding. Lockbox 3 wasn't made for unorthodox padding techniques, such as C# uses, but with a bit of work, we can adapt.
Listing 1: program CSharp2.dpr
Listing 2: umfmCSharp2Demo.pas
Listing 3: umfmCSharp2Demo.dfm