암호화 도중 방생한 오류에 대해 질문드립니다.

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);

                }
1개의 좋아요

공유해주실수 있는 선에서 코드를 적어주시면 좀더 원인을 찾기가 쉬울 것 같아요.
AES 128 복호화 관련 코드 예제를 아래 링크로 공유 드리니 한번 비교해보시는 것도 좋을 것 같아요.

3개의 좋아요

관련소스 올렸습니다.

1개의 좋아요

코드상 어떤 라인에서 발생되는지 확인 될까요?

1개의 좋아요

두번째 소스 에 있습니다. decrypt 로 검색하면 될듯 합니다.

byte[] decryptBinary = 
    AES.Instance.Decrypt(bodyBinary, EncryptType.CLES_CONTROLLER, 2);
1개의 좋아요

인코딩 이슈 추정되는데요. utf8 문자열을 유니코드로 풀려하거나 유니코드를 아스키로 풀려하는 등에서 나올 수 있는 오류 메시지 같아 보입니다. 구글에서 “transformfinalblock The input data is not a complete block” 로 검색해 보니 대부분 그러한 원인의 해결 내용들이 검색되네요.

https://social.msdn.microsoft.com/Forums/en-US/1ac64f73-4e1d-4510-b897-761508b5ea57/aes-encryption-error-the-input-data-is-not-a-complete-block?forum=aspgettingstarted

1개의 좋아요