我有一个字符串,我使用Crypto使用C中的以下方法加密了:
std::ifstream t(filename); //File to be encrypt
std::stringstream buffer;
buffer << t.rdbuf();
ofstream combined_file2(filename2); //Encrypted file
combined_file2 << encrypt(buffer.str());
string encrypt(string data)
{
// Key and IV setup
std::string key = "0123456789abcdef";
std::string iv = "aaaaaaaaaaaaaaaa";
//Alternative
//byte key[CryptoPP::AES::DEFAULT_KEYLENGTH], iv[CryptoPP::AES::BLOCKSIZE];
//memset(key, 0x00, CryptoPP::AES::DEFAULT_KEYLENGTH);
//memset(iv, 0x00, CryptoPP::AES::BLOCKSIZE);
std::string plaintext = data;
std::string ciphertext;
// Create Cipher Text
CryptoPP::AES::Encryption aesEncryption((byte *)key.c_str(), CryptoPP::AES::DEFAULT_KEYLENGTH);
CryptoPP::CBC_Mode_ExternalCipher::Encryption cbcEncryption(aesEncryption, (byte *)iv.c_str());
//Alternative
//CryptoPP::AES::Encryption aesEncryption(key, CryptoPP::AES::DEFAULT_KEYLENGTH);
//CryptoPP::CBC_Mode_ExternalCipher::Encryption cbcEncryption(aesEncryption, iv);
CryptoPP::StreamTransformationFilter stfEncryptor(cbcEncryption, new CryptoPP::StringSink(ciphertext));
stfEncryptor.Put(reinterpret_cast<const unsigned char*>(plaintext.c_str()), plaintext.length() + 1);
stfEncryptor.MessageEnd();
return ciphertext;
}
当我尝试在C#应用程序中解密文件时,收到消息,数据长度无效.我认为字节数组的长度不是16的倍数,所以我得到了错误.我尝试使用填充,但无法正常工作.
这是我解密文件的方式:
string plaintext = Decrypt(File.ReadAllBytes(path));
private static string Decrypt(byte[] cipherText)
{
if (cipherText == null || cipherText.Length <= 0)
throw new ArgumentNullException("cipherText");
byte[] Key = GetBytes(ConfigurationManager.AppSettings["aes_key"]);
byte[] IV = GetBytes(ConfigurationManager.AppSettings["aes_iv"]);
// Declare the string used to hold the decrypted text.
string plaintext = null;
// Create an RijndaelManaged object with the specified key and IV.
using (RijndaelManaged rijAlg = new RijndaelManaged())
{
rijAlg.Key = Key;
//rijAlg.IV = IV;
//for testing
rijAlg.IV = new byte[] { 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97, 97 };
// Create a decrytor to perform the stream transform.
ICryptoTransform decryptor = rijAlg.CreateDecryptor(rijAlg.Key, rijAlg.IV);
// Create the streams used for decryption.
using (MemoryStream msDecrypt = new MemoryStream(cipherText))
{
using (CryptoStream csDecrypt = new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read))
{
using (StreamReader srDecrypt = new StreamReader(csDecrypt))
{
// Read the decrypted bytes from the decrypting stream
// and place them in a string.
plaintext = srDecrypt.ReadToEnd();
}
}
}
}
return plaintext;
}
我该如何解决这个问题?是否有任何功能可以执行填充,或者填充是错误的方式?
解决方法:
根据评论:
In C++ I save a file combined_file2 << encrypt(buffer.str()); which I
read in my C# program string plaintext =
Decrypt(File.ReadAllBytes(path));
我不认为嵌入式NULL会因为返回一个字符串而导致加密函数出现问题,因为它包含一个明确的长度.
但是,嵌入式NULL和将文件写入C中的磁盘(或从C#中的磁盘读取)的方式将是一个问题,因为它将在第一个嵌入式NULL处停止:Combined_file2<<加密(buffer.str()). 也许类似以下内容会有所帮助:
StringSource ss(ciphertext, true /*pumpAll*/);
FileSink fs("my-encrypted-file.bin", true /*binary*/);
ss.TransferTo(fs);
如果使用的是C流,则对流对象使用write方法:
ofstream combined_file2;
...
combined_file2.write(ciphertext.data(), ciphertext.size());