2012-03-02 1 views
1

CFB 모드에서 AES 인 것으로 암호화 된 부분이있는 기존 데이터 형식이 있습니다. 평문 데이터 길이와 암호화 된 데이터 길이는 동일합니다.C#에서 AES 암호화 (일반 텍스트 길이가 암호화 된 길이와 같음)

C#에서는 암호화 된 길이가 블록 크기의 배수가 될 것으로 기대하는 것처럼 보이기 때문에 데이터를 해독하려는 예외가 발생합니다.

솔루션을 연구 할 때 Crypto ++를 사용하여 데이터를 성공적으로 해독하는 빠른 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

+1

[this] (http://stackoverflow.com/questions/3142279/encrypt-data-with-c-sharp-aescryptoserviceprovider-crypted-with-bouncycastle-aes) 질문에 대한 답변을 확인 했습니까? –

+0

@GregS q & a는 약간의 사랑을 사용할 수 있습니다. 착각하지 않으면 기본적으로 구현됩니다. –

답변

0

... 코드는 다음과 같습니다 :

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

여기 8 비트 피드백 CFB 암호화를 달성하는 것은 RijndaelManaged 클래스를 사용하는 방법을 보여주는 예이다. AesManaged는 공식 NIST AES가 지원하지 않기 때문에 CFB를 지원하지 않습니다. AES 이고 Rijndael이 128 비트 블록 크기로 제한되고 128, 192 및 256 비트 키가 제한됨을 알면 RijndaelManaged 클래스를 사용하여 CFB 기능을 사용할 수 있습니다. 참고 : 저는 C# 또는 .NET 전문가가 아니므로 개선을 환영합니다. 내가 cryptlib를 사용하려고 재 방문하고 내 문제를 해결

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

응답 주셔서 감사 ... 나는 당신이 제공 한 코드의 해독 부분을 사용하여 시도했다. 예외를 throw하지 않고 데이터를 해독 할 수 있었지만 암호화 한 것에 대한 올바른 일반 텍스트를 반환하지 않았습니다. – magnvs