특정 암호를 반환해야하는 결과를 반환하기 위해 다음 테스트를 수행하려고합니다. 그들은 아래와 같이 Key, IV 및 Plaintext 문자열을 제공합니다.AES C# 암호화 해독 FIPS
하지만 "지정된 초기화 벡터 (IV)가이 알고리즘의 블록 크기와 일치하지 않습니다."
나는 이것에 잠시 동안 머물러 있었고 좋은 간단한 예를 찾을 수 없었고 여러 가지를 시도했다.
다음은 C# 코드입니다. 나는 그것을 매우 간단하게 유지하려고 노력했다. 답변 PER 업데이트]
string AesPlainText = "1654001d3e1e9bbd036a2f26d9a77b7f";
string AesKey = "3ccb6039c354c9de72adc9ffe9f719c2c8257446c1eb4b86f2a5b981713cf998";
string AesIV = "ce7d4f9679dfc3930bc79aab81e11723";
AesCryptoServiceProvider aes = new AesCryptoServiceProvider();
aes.KeySize = 256;
aes.IV = HexToByteArray(AesIV);
aes.Key = HexToByteArray(AesKey);
aes.Mode = CipherMode.CBC;
// Convert string to byte array
byte[] src = Encoding.Unicode.GetBytes(AesPlainText);
// encryption
using (ICryptoTransform encrypt = aes.CreateEncryptor())
{
byte[] dest = encrypt.TransformFinalBlock(src, 0, src.Length);
// Convert byte array to Base64 strings
Console.WriteLine(Convert.ToBase64String(dest));
}
:
감사합니다, 좋은 관찰. Encoding.UTF8.GetBytes를 위의 예제에서 HexToByteArray를 사용하도록 변경했으며 현재 작동합니다.
public static byte[] HexToByteArray(String hex)
{
int NumberChars = hex.Length;
byte[] bytes = new byte[NumberChars/2];
for (int i = 0; i < NumberChars; i += 2)
bytes[i/2] = Convert.ToByte(hex.Substring(i, 2), 16);
return bytes;
}
내 문제를 해결하고 암호화를 실행할 수 있도록 추가 한 기능은 다음과 같습니다. –