2012-03-02 122 views
1

我有一個現有的數據格式,它的一部分以CFB模式下的AES加密。明文數據長度和加密數據長度相同。CFB中的C#AES加密,其中明文長度等於加密長度

在C#中,幾乎每一個我所採取的角度似乎期待加密長度是塊大小的整數倍,所以,我得到一個異常嘗試解密數據。

在研究解決方案,我用加密+和寫了一個快速的C++應用程序,成功地對數據進行解密,所以我敢肯定,我使用的是正確的算法,密鑰和IV。這工作正常,但我想盡可能保持C#中的所有內容。有什麼建議麼?下面

工作C++代碼:

//define key 
unsigned char key[16]; 
//populate key 
//... 


//define iv 
unsigned char iv[16]; 
//populate iv 
//... 

std::ifstream inFile; 

//open file 
inFile.open("file.aes",ios::binary); 

//get file size 
inFile.seekg(0,ios::end); 
int fileSize = (int) inFile.tellg(); 
inFile.seekg(offset, ios::beg); 

//read/close file 
char* inBytes = new char[fileSize]; 
inFile.read(inBytes,fileSize); 
inFile.close(); 

//configure decryption 
CFB_Mode<AES>::Decryption cfbDecryption(key, 16, iv); 

//populate output bytes 
char* outBytes = new char[fileSize]; 
cfbDecryption.ProcessData((byte*) outBytes,(byte*) inBytes,fileSize); 

//open/write/close output file 
std::ofstream outFile; 
outFile.open("out.dec"); 
outFile.write(outBytes,fileSize); 
outFile.close(); 

delete[] inBytes; 
+2

你可以發佈你已經嘗試過的C#代碼,你所得到的例外呢? – Xint0 2012-03-02 23:08:30

+1

你有沒有檢查[這個]的答案(http://stackoverflow.com/questions/3142279/encrypt-data-with-c-sharp-aescryptoserviceprovider-crypted-with-bouncycastle-aes)的問題? – 2012-03-03 01:29:30

+0

@GregS q&a可以使用一些愛,它基本上是實現,如果我沒有錯誤 – 2012-03-03 12:32:35

回答

0

我重新審視試圖用cryptlib,它解決了我的問題...代碼如下:

using cryptlib; 

byte[] key = new byte[16] {...key bytes here...}; 

byte[] iv = new byte[16] {...iv bytes here...}; 

byte[] enc; //ciphertext bytes (i populated them from a filestream) 

crypt.Init(); 
int cryptContext = crypt.CreateContext(crypt.UNUSED, crypt.ALGO_AES); 
crypt.SetAttribute(cryptContext, crypt.CTXINFO_MODE, crypt.MODE_CFB); 
crypt.SetAttributeString(cryptContext, crypt.CTXINFO_KEY, key, 0, 16); 
crypt.SetAttributeString(cryptContext, crypt.CTXINFO_IV, iv, 0, 16); 
crypt.Decrypt(cryptContext, enc); //ciphertext bytes replaced with plaintext bytes 
crypt.DestroyContext(cryptContext); 
2

這裏是示出了如何使用RijndaelManaged類來實現的8位反饋CFB加密的例子。 AesManaged不支持CFB,因爲我相信官方的NIST AES不支持它。注意到AES 只是Rijndael限制爲128位塊大小和128,192和256位密鑰大小,您可以使用RijndaelManaged類來獲得您的CFB功能。注意:我不是C#或.NET專家,因此歡迎進行改進。

using System; 
using System.Text; 
using System.Security.Cryptography; 
using System.IO; 

namespace AesCFB8Mode 
{ 
    class AESCFB8Example 
    { 
     static void Example() 
     { 
      // 
      // Encrypt a small sample of data 
      // 
      String Plain = "The quick brown fox"; 
      byte[] plainBytes = Encoding.UTF8.GetBytes(Plain); 
      Console.WriteLine("plaintext length is " + plainBytes.Length); 
      Console.WriteLine("Plaintext is " + BitConverter.ToString(plainBytes)); 

      byte [] savedKey = new byte[16]; 
      byte [] savedIV = new byte[16]; 
      byte[] cipherBytes; 
      using (RijndaelManaged Aes128 = new RijndaelManaged()) 
      { 
       // 
       // Specify a blocksize of 128, and a key size of 128, which make this 
       // instance of RijndaelManaged an instance of AES 128. 
       // 
       Aes128.BlockSize = 128; 
       Aes128.KeySize = 128; 

       // 
       // Specify CFB8 mode 
       // 
       Aes128.Mode = CipherMode.CFB; 
       Aes128.FeedbackSize = 8; 
       Aes128.Padding = PaddingMode.None; 
       // 
       // Generate and save random key and IV. 
       // 
       Aes128.GenerateKey(); 
       Aes128.GenerateIV(); 

       Aes128.Key.CopyTo(savedKey, 0); 
       Aes128.IV.CopyTo(savedIV, 0); 

       using (var encryptor = Aes128.CreateEncryptor()) 
       using (var msEncrypt = new MemoryStream()) 
       using (var csEncrypt = new CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write)) 
       using (var bw = new BinaryWriter(csEncrypt, Encoding.UTF8)) 
       { 
        bw.Write(plainBytes); 
        bw.Close(); 

        cipherBytes = msEncrypt.ToArray(); 
        Console.WriteLine("Cipher length is " + cipherBytes.Length); 
        Console.WriteLine("Cipher text is " + BitConverter.ToString(cipherBytes)); 
       } 
      } 

      // 
      // Now decrypt the cipher back to plaintext 
      // 

      using (RijndaelManaged Aes128 = new RijndaelManaged()) 
      { 
       Aes128.BlockSize = 128; 
       Aes128.KeySize = 128; 
       Aes128.Mode = CipherMode.CFB; 
       Aes128.FeedbackSize = 8; 
       Aes128.Padding = PaddingMode.None; 

       Aes128.Key = savedKey; 
       Aes128.IV = savedIV; 

       using (var decryptor = Aes128.CreateDecryptor()) 
       using (var msEncrypt = new MemoryStream(cipherBytes)) 
       using (var csEncrypt = new CryptoStream(msEncrypt, decryptor, CryptoStreamMode.Read)) 
       using (var br = new BinaryReader(csEncrypt, Encoding.UTF8)) 
       { 
        //csEncrypt.FlushFinalBlock(); 
        plainBytes = br.ReadBytes(cipherBytes.Length); 

        Console.WriteLine("Decrypted plain length is " + plainBytes.Length); 
        Console.WriteLine("Decrypted plain text is " + BitConverter.ToString(plainBytes)); 
       } 
      } 
     } 

     static void Main(string[] args) 
     { 
      Example(); 
     } 
    } 
} 
+1

感謝您的迴應......我嘗試使用您提供的代碼的解密部分。雖然它能夠在不拋出異常的情況下對數據進行解密,但它並沒有爲我所加密的內容返回正確的明文。 – magnvs 2012-03-05 14:52:06