샘플 응용 프로그램에서 간단한 값의 암호를 해독하는 데 문제가 있습니다. 이 값은 동일한 샘플 응용 프로그램으로 암호화되었습니다. 나는 아래 코드를 열거했다..NET AES-128 암호 해독은 빈 문자열을 반환합니다.
코드 블록이 완료되면 plain
은 빈 문자열입니다. 예외는 발생하지 않습니다.
string plain = null;
using (AesManaged alg = new AesManaged())
{
// Extract the initialization vector from the entire ciphertext
byte[] IV = new byte[alg.IV.Length];
Buffer.BlockCopy(cipherText, 0, IV, 0, alg.IV.Length);
alg.IV = IV;
alg.Key = GetKey();
ICryptoTransform transform = alg.CreateDecryptor();
// Extract the encrypted value from the entire ciphertext
byte[] encrypted = new byte[cipherText.Length - alg.IV.Length];
Buffer.BlockCopy(cipherText, alg.IV.Length, encrypted, 0, cipherText.Length - alg.IV.Length);
using (MemoryStream ms = new MemoryStream(encrypted))
{
using (CryptoStream cs = new CryptoStream(ms, transform, CryptoStreamMode.Read))
{
using (StreamReader r = new StreamReader(cs))
{
cs.Flush();
plain = r.ReadToEnd();
}
}
}
}
I는 cipherText
(32 바이트 바이트 [] 매개 변수로 전달 된)는 바이트 단위 동일한 값 암호화 기인 것과을 갖는 것을 확인 하였다. IV와 Key 역시 byte-for-byte입니다. 참고 : IV는 암호화 논리에 의해 암호화 된 값 앞에 추가됩니다. 이것이 첫 번째 줄이 코드 블록에서 코드를 추출하는 이유입니다.
또한 encrypted
바이트 []의 내용이 암호화 루틴에서 나오는 내용과 동일하다는 것도 확인했습니다. 내용은 단지 16 바이트입니다.
메모리 스트림 ms
에있는 Position
이 0임을 확인했습니다. 루틴 이후에 16이됩니다. 따라서 MemoryStream이 읽힌 것으로 보입니다. 내 의심은 내가 StreamReader
을 잘못 사용하고 있지만 내가 실수 한 부분을 볼 수 없다는 것이다.
암호화 된 바이트 []의 바이트 중 하나를 수정하려고했는데 예상 한대로 CryptographicException
(패딩이 유효하지 않습니다)이 표시됩니다. 따라서 암호화 관점에서 볼 때 모든 것이 IV, 키 및 암호화 된 값으로 정렬되어있는 것으로 보입니다. 웬일인지, 그것이 처리되지 않을지도 모른다?
어떤 통찰력에도 감사드립니다.
완전성을 위해 아래의 암호화 루틴 : byte [] encrypted = null;
using (AesManaged alg = new AesManaged())
{
System.Diagnostics.Debug.WriteLine("Key size: {0}", alg.KeySize);
alg.GenerateIV();
alg.Key = GetKey();
ICryptoTransform transform = alg.CreateEncryptor(alg.Key, alg.IV);
using (MemoryStream ms = new MemoryStream())
using (CryptoStream cs = new CryptoStream(ms, transform, CryptoStreamMode.Write))
using (StreamWriter w = new StreamWriter(cs))
{
w.Write(plainText);
cs.FlushFinalBlock();
// Create a byte array big enough to hold the IV and the encrypted value
encrypted = new byte[alg.IV.Length + ms.Length];
// Copy the random generated initialization vector to the start of the encrypted bytes
Buffer.BlockCopy(alg.IV, 0, encrypted, 0, alg.IV.Length);
// Copy the encrypted value at the end
Buffer.BlockCopy(ms.ToArray(), 0, encrypted, alg.IV.Length, (int)ms.Length);
}
}