我使用的是here找到的DCPcrypt库。
下面是加密字符串的一小段代码
InitializationVector: AnsiString;
const Key: Ansistring = 'keykeykeykey';
// Encrypt a string and return the Base64 encoded result
function Encrypt(DataToEncrypt: ansistring):ansistring;
var
Cipher : TDCP_rijndael;
Data: string;
IV: array[0..15] of byte; // the initialization vector
i:Integer;
begin
// Pad Key, IV and Data with zeros as appropriate
FillChar(IV,Sizeof(IV),0); // make the IV all zeros
Data := PadWithZeros(DataToEncrypt,BlockSize);
for i := 0 to (Length(IV) - 1) do //just random values for the IV
IV[i] := Random(256);
Cipher := TDCP_rijndael.Create(nil);
if Length(Key) <= 16 then
Cipher.Init(Key[1],128,@IV[1])
else if Length(Key) <= 24 then
Cipher.Init(Key[1],192,@IV[1])
else
Cipher.Init(Key[1],256,@IV[1]);
// Encrypt the data
Cipher.EncryptCBC(Data[1],Data[1],Length(Data));
// Free the cipher and clear sensitive information
Cipher.Free;
SetString(InitializationVector,PAnsiChar(@IV[1]),Length(IV));
InitializationVector := Base64EncodeStr(InitializationVector);
//Base64 encoded result
Result := Base64EncodeStr(Data);
end;我可以解密得到的字符串,但只能解密其中的一半。找到了一个类似的帖子,但他在用base64编码密码时找到了答案,我正在做的事情。Here。
如有任何帮助,我们不胜感激!
发布于 2011-04-27 05:05:08
默认情况下,Delphi 2009/2010和XE中的字符串是Unicode字符串。
这意味着单个字符可以占用1或更多的字节。
您在代码中放入了很好的旧AnsiString,但是忘记了一个。
这意味着到Unicode的转换会把你的解密搞得一团糟,因为在加密的时候,即使是一个单独的改变位也会把一切都搞乱。
从头到尾坚持使用AnsiStrings,你应该会很好。
更改:
function Encrypt(DataToEncrypt: ansistring):ansistring;
var
Cipher : TDCP_rijndael;
Data: string;
IV: array[0..15] of byte; // the initialization vector
i:Integer;
begin至
// Encrypt a string and return the Base64 encoded result
function Encrypt(DataToEncrypt: AnsiString): AnsiString;
var
Cipher: TDCP_rijndael;
//Data: string; <<- change to ansistring
Data: AnsiString;
IV: array[0..15] of byte; // the initialization vector
i: Integer;https://stackoverflow.com/questions/5793621
复制相似问题