解密 AES-256 加密文件不起作用
decrypting a AES-256 encrypted file not working
我使用 Botan 库进行加密,我的加密代码如下所示。
LibraryInitializer init;
AutoSeeded_RNG rng;
string passphrase="mypassword";
PBKDF* pbkdf = get_pbkdf("PBKDF2(SHA-256)");
SecureVector<byte> salt = rng.random_vec(16);
InitializationVector iv(rng,16);
OctetString aes256_key = pbkdf->derive_key(32, passphrase,&salt[0], salt.size(), 10000 );
cout<<"Encryption key : " << aes256_key.as_string() <<endl ;
ifstream infile ("readme.txt");
ofstream outfile ("encrypt.txt");
Pipe pipe(get_cipher("AES-256/EAX", aes256_key,iv, ENCRYPTION) );
pipe.start_msg();
infile>>pipe;
pipe.end_msg();
SecureVector<byte> cl = pipe.read_all();
outfile.write((const char*)cl.begin(), cl.size());
outfile.flush();
outfile.close();
infile.close();
此代码看起来运行良好并加密了输入文件。我发布此代码以确定加密是否存在错误。 (但我假设加密已正确完成)
现在上面的加密文件尝试用下面的代码解密
ifstream infile2 ("encrypt.txt");
ofstream outfile2 ("decrypt.txt");
Pipe pipe2 (get_cipher("AES-256/EAX", aes256_key, iv, DECRYPTION) );
pipe2.start_msg();
infile2 >> pipe2;
pipe2.end_msg();
SecureVector<byte> cl2 = pipe2.read_all();
outfile2.write((const char*)cl2.begin(), cl2.size());
outfile2.close();
infile2.close();
}
与上面生成的解密密钥相同,InitializationVector iv
用于解密。
解密异常AES-256/EAX : message authentication failed
我这里哪里做错了,如何正确解密上面的encryptrd文件。
问题是 ifstream
和 ofstream
假定字符输出。如果您将其配置为使用 std::ios::binary
作为第二个参数来处理二进制文件,那么您的代码应该没问题。这也被 Botan API 引用 if 使用,它也没有明确地 encode 密文。
我使用 Botan 库进行加密,我的加密代码如下所示。
LibraryInitializer init;
AutoSeeded_RNG rng;
string passphrase="mypassword";
PBKDF* pbkdf = get_pbkdf("PBKDF2(SHA-256)");
SecureVector<byte> salt = rng.random_vec(16);
InitializationVector iv(rng,16);
OctetString aes256_key = pbkdf->derive_key(32, passphrase,&salt[0], salt.size(), 10000 );
cout<<"Encryption key : " << aes256_key.as_string() <<endl ;
ifstream infile ("readme.txt");
ofstream outfile ("encrypt.txt");
Pipe pipe(get_cipher("AES-256/EAX", aes256_key,iv, ENCRYPTION) );
pipe.start_msg();
infile>>pipe;
pipe.end_msg();
SecureVector<byte> cl = pipe.read_all();
outfile.write((const char*)cl.begin(), cl.size());
outfile.flush();
outfile.close();
infile.close();
此代码看起来运行良好并加密了输入文件。我发布此代码以确定加密是否存在错误。 (但我假设加密已正确完成)
现在上面的加密文件尝试用下面的代码解密
ifstream infile2 ("encrypt.txt");
ofstream outfile2 ("decrypt.txt");
Pipe pipe2 (get_cipher("AES-256/EAX", aes256_key, iv, DECRYPTION) );
pipe2.start_msg();
infile2 >> pipe2;
pipe2.end_msg();
SecureVector<byte> cl2 = pipe2.read_all();
outfile2.write((const char*)cl2.begin(), cl2.size());
outfile2.close();
infile2.close();
}
与上面生成的解密密钥相同,InitializationVector iv
用于解密。
解密异常AES-256/EAX : message authentication failed
我这里哪里做错了,如何正确解密上面的encryptrd文件。
问题是 ifstream
和 ofstream
假定字符输出。如果您将其配置为使用 std::ios::binary
作为第二个参数来处理二进制文件,那么您的代码应该没问题。这也被 Botan API 引用 if 使用,它也没有明确地 encode 密文。