Catch 문안에 e.message 로 나온 메시지 입니다.
The input data is not a complete block
구체적으로 무엇을 의미하는 말인가요? ㅠㅠ 암호화 패킷으로 들어온것을
AES (128 인듯) 로 복호화 할 때 나오는 듯 합니다.
=== AES.cs =========================================================
using Serilog;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;
namespace IgsCommon.Util.Encrypt
{
/// <summary>
/// 암호화에 사용할 키를 결정한다
/// </summary>
public enum EncryptType
{
/// <summary>
/// 특징점 암호화
/// </summary>
FILE,
/// <summary>
/// 얼굴리더기 통신 암호화 패킷
/// </summary>
CLES_CONTROLLER
}
public class AES
{
// 멀티스레드 환경에서 객체 간섭 없도록 구현
private static volatile AES _instance;
private static object syncRoot = new Object();
public static AES Instance
{
get
{
if (_instance == null)
{
lock (syncRoot)
{
if (_instance == null)
_instance = new AES();
}
}
return _instance;
}
}
// 암호화 공용키 선언
private byte[] fileKey;
private byte[] vecfileKey;
private byte[] clesControllerkey = new byte[16]
{
0x31, 0x09, 0xE8, 0xD5, 0xDD, 0x44, 0x59, 0x86, 0x10, 0x21, 0xE0, 0x7A, 0x89, 0xFA, 0xB7, 0x6C
};
// 0xF2, 0x48, 0x92, 0x3B, 0x3C, 0xCF, 0xAA, 0xC9, 0x06, 0x26, 0x36, 0xDC, 0xB3, 0x20, 0x19, 0x4F
// 0x81, 0x92, 0x30, 0x82, 0xD9, 0x2A, 0xBB, 0xFA, 0x8B, 0xCA, 0x8E, 0x38, 0x1E, 0xEE, 0x46, 0xE1
private byte[] clesControllerVecKey = new byte[16]
{
0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF, 0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF
};
private byte[] v2ControllerKey = new byte[16]
{
0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF, 0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF
};
private byte[] v2ControllerVecKey = new byte[16]
{
0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF, 0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF
};
private AES()
{
fileKey = new byte[16];
fileKey[0] = (byte)0x9D;
fileKey[1] = (byte)0xA7;
fileKey[2] = (byte)0x9D;
fileKey[3] = (byte)0xC4;
fileKey[4] = (byte)0x00;
fileKey[5] = (byte)0x64;
fileKey[6] = (byte)0x18;
fileKey[7] = (byte)0x0D;
fileKey[8] = (byte)0x1F;
fileKey[9] = (byte)0x70;
fileKey[10] = (byte)0xE9;
fileKey[11] = (byte)0xAB;
fileKey[12] = (byte)0x86;
fileKey[13] = (byte)0x1A;
fileKey[14] = (byte)0x32;
fileKey[15] = (byte)0x0D;
vecfileKey = new byte[16];
vecfileKey[0] = (byte)0x01;
vecfileKey[1] = (byte)0x23;
vecfileKey[2] = (byte)0x45;
vecfileKey[3] = (byte)0x67;
vecfileKey[4] = (byte)0x89;
vecfileKey[5] = (byte)0xAB;
vecfileKey[6] = (byte)0xCD;
vecfileKey[7] = (byte)0xEF;
vecfileKey[8] = (byte)0x01;
vecfileKey[9] = (byte)0x23;
vecfileKey[10] = (byte)0x45;
vecfileKey[11] = (byte)0x67;
vecfileKey[12] = (byte)0x89;
vecfileKey[13] = (byte)0xAB;
vecfileKey[14] = (byte)0xCD;
vecfileKey[15] = (byte)0xEF;
}
/// <summary>
/// 암호화 규격을 정의한 객체를 생성하는 팩토리 메서드
/// </summary>
/// <param name="encType">암호화 유형</param>
/// <returns></returns>
public RijndaelManaged GetRijndaelManaged(EncryptType encType = EncryptType.FILE)
{
if (encType == EncryptType.FILE)
{
return new RijndaelManaged
{
Mode = CipherMode.CBC,
Padding = PaddingMode.PKCS7,
KeySize = 128,
BlockSize = 128,
Key = fileKey,
IV = vecfileKey
};
}
else if (encType == EncryptType.CLES_CONTROLLER)
{
return new RijndaelManaged
{
Mode = CipherMode.CBC,
Padding = PaddingMode.PKCS7,
KeySize = 128,
BlockSize = 128,
Key = clesControllerkey,
IV = clesControllerVecKey
};
}
else
{
throw new Exception("enc Type이 일치하지 않습니다.");
}
}
public byte[] Encrypt(byte[] plainBytes, EncryptType encType, int offset = 0)
{
return GetRijndaelManaged(encType).CreateEncryptor()
.TransformFinalBlock(plainBytes, offset, plainBytes.Length - offset);
}
public byte[] Decrypt(byte[] encryptedData, EncryptType encType, int offset = 0)
{
RijndaelManaged rijndaelManaged = GetRijndaelManaged(encType);
return rijndaelManaged.CreateDecryptor()
.TransformFinalBlock(encryptedData, offset, encryptedData.Length - offset);
}
// Encrypts plaintext using AES 128bit key and a Chain Block Cipher and returns a base64 encoded string
/// <summary>
/// 텍스트를 암호화하는 메서드
/// </summary>
/// <param name="plainText">원문</param>
/// <param name="encType">암호화 유형</param>
/// <returns>암호화 된 텍스트</returns>
public String Encrypt(String plainText, EncryptType encType)
{
string retString = string.Empty;
try
{
var plainBytes = Encoding.Default.GetBytes(plainText);
retString = Convert.ToBase64String(Encrypt(plainBytes, encType));
}
catch (Exception ee)
{
Log.Error(ee, "unknown Exception");
}
return retString;
}
/// <summary>
/// 텍스트를 복호화하는 메서드
/// </summary>
/// <param name="encryptedText">암호화 텍스트</param>
/// <param name="encType">암호화 유형</param>
/// <returns>복호화 된 텍스트</returns>
public String Decrypt(String encryptedText, EncryptType encType)
{
string retString = string.Empty;
if (string.IsNullOrWhiteSpace(encryptedText))
{
return retString;
}
try
{
var encryptedBytes = Convert.FromBase64String(encryptedText);
retString = Encoding.Default.GetString(Decrypt(encryptedBytes, encType));
}
catch (Exception ee)
{
Log.Error(ee, string.Empty);
}
return retString;
}
}
}
— 복호화 처리 함수 ----------------------------------------------------
{
EndianStream stream = null;
try
{
byte[] temp = new byte[2];
temp[0] = bodyBinary[1];
temp[1] = bodyBinary[0];
ushort len = BitConverter.ToUInt16(temp);
if (HeaderPacket.EncoderKeyVersion == 0x10) //HeaderPacket.EncoderKeyVersion == 0x10)
{
byte[] decryptBinary = AES.Instance.Decrypt(bodyBinary, EncryptType.CLES_CONTROLLER, 2);
stream = new EndianStream(decryptBinary, true);
//stream = new EndianStream(bodyBinary, 2, true);
}
else
{
stream = new EndianStream(bodyBinary, 2, true);
}