我使用 D 的 Deimos openssl 标头将 D 链接到 OpenSsl,并使用 ldc 1.8.0 编译器尝试加密字符串作为小测试。加密的字节数组与我的预期不一致。当我运行程序并加密字符串并随后解密时,我会取回原始字符串。但是中间的加密字节数组在代码的执行之间并不一致。
所以我的问题是,这是预期的行为吗,OpenSsl 中的 AES 是否在内容中添加了某种盐,因此更难攻击,或者这是我的错误?
import std.stdio;
import std.conv;
import std.string;
import std.outbuffer;
import deimos.openssl.aes;
void main()
{
writeln("hello world");
const auto encryption_passphrase = "foo!";
writeln("The encryption key is \"" ~ encryption_passphrase ~ "\"");
const auto encryption_content = "bar";
writeln("The to be encrypted content is: \"" ~ encryption_content ~ "\"");
writeln("The content lenght is " ~ encryption_content.length.to!string);
writeln("----------");
writeln("encrypting");
AES_KEY encryption_key;
AES_set_encrypt_key(cast(ubyte*) encryption_passphrase.toStringz, 128, &encryption_key);
OutBuffer buf = new OutBuffer();
buf.write(encryption_content);
ubyte[] inbuffer = buf.toBytes();
ubyte[] encryptedbuffer = new ubyte[inbuffer.length];
AES_encrypt(&inbuffer[0], &encryptedbuffer[0], &encryption_key);
writeln("The encrypted content is: \"" ~ (cast(char*)encryptedbuffer).fromStringz ~ "\"");
writeln("----------");
writeln("decrypting");
AES_KEY decryption_key;
AES_set_decrypt_key(cast(ubyte*) encryption_passphrase.toStringz, 128, &decryption_key);
ubyte[] outbuffer = new ubyte[inbuffer.length];
AES_decrypt(&encryptedbuffer[0], &outbuffer[0], &decryption_key);
writeln("the decrypted content is: \"" ~ (cast(char*)outbuffer).fromStringz ~ "\"");
}