I am using Tforge and Delphi and I am trying to encrypt TidBytes
with AES
var Key,MyBytearray: ByteArray;
MyTidBytes:TidBytes;
Key:= ByteArray.FromText('1234567890123456');
EncryptedText:= TCipher.AES.ExpandKey(Key, CTR_ENCRYPT or PADDING_NONE).EncryptByteArray(MyBytearray);
This code works fine with ByteArray
but I want to use it with idBytes is this possible?
How I will convert ByteArray
to TidBytes
?
ByteArray
is declared as a record
that internally holds an IBytes
interfaced object wrapping the byte data. TIdBytes
is declared as a simple dynamic array instead. As such, you can't directly typecast between them. You must copy the raw bytes back and forth.
You can do that manually, eg:
MyBytearray := ...;
MyTidBytes := RawToBytes(MyBytearray.Raw^, MyBytearray.Len);
// RawToBytes() is an Indy function in the IdGlobal unit...
...
MyTidBytes := ...;
MyBytearray := ByteArray.FromBytes(MyTidBytes);
// FromBytes() accepts any type of raw byte array as input, including dynamic arrays ...
Or, alternatively, ByteArray
has Implicit
conversion operators for TBytes
, and TIdBytes
is typecast-compatible with TBytes
as they are both dynamic arrays, eg:
MyBytearray := ...;
TBytes(MyTidBytes) := MyBytearray;
...
MyTidBytes := ...;
MyBytearray := TBytes(MyTidBytes);