|
![]() |
String conversions when using EncryptStr/DecryptStr methods |
Henrick Wibell Hellström 2018-02-11 12:32:21 Registered user |
The EncryptString/DecryptString functions in stSecUtils and the tCipher.EncryptStr/tCipher.DecryptStr methods take an OctetString variable as argument and also return OctetString values.
This means that if you want to encrypt a "regular" string (i.e. containing character sequences) you have to convert it yourself to an OctetString. OctetString is an alias for AnsiString up to Delphi 2007, RawByteString from Delphi 2009, and TBytes for NextGen compilers. There are routines in unit stDERUtils for converting string values to OctetString values, e.g. UnicodeToUtf8 and UnicodeToBMP. Conversely, after decrypting a cipher text you will get an OctetString value you will have to convert to a character string somehow. One way is by using the corresponding stDERUtils routine Utf8ToUnicode or BMPToUnicode, depending on which encoding that was used when encrypting the text. Also beware that the cipher text returned by EncryptString and tCipher.EncryptStr is pseudo random. If you wish to treat it as text (e.g. to display it, save it in a text file, ini file, XML file etc) you have to convert it e.g. to hexadecimal or base64. There are OSToHex routines in unit stDERUtils, and StrToMIME64Str routines in unit stBase64. Such strings will obviously have to be converted back to OctetString values prior to decrypting the cipher text. |