locked
AES encryption error: The input data is not a complete block? RRS feed

  • Question

  • User-1664485818 posted

    Hi everyone, when I run my code, I receive error “The input data is not a complete block”.

    I know something is wrong with the input date, but I’m just passing in a string newMessage = "I love programming”

    The code breaks at

    byte[] enc = icrypt.TransformFinalBlock(textbytes, 0, textbytes.Length);

    Any pointers much appreciated…

    string newMessage = "I love programming";
    string encMessage;
    encMessage = Encryp(newMessage);
      public static string Encryp(string decrypted)
            {
                string IV = "qo1lc3sjd8zpt9cx";  //16 chars = 128 bytes
                string Key = "ow7dxys8glfor9tnc2ansdfo1etkfjcv"; // 32 chars = 256 bytes
    
                byte[] textbytes = ASCIIEncoding.ASCII.GetBytes(decrypted);
              
                AesCryptoServiceProvider encdec = new AesCryptoServiceProvider();
    
                encdec.BlockSize = 128;
                encdec.KeySize = 256;
                encdec.Key = ASCIIEncoding.ASCII.GetBytes(Key);
                encdec.IV = ASCIIEncoding.ASCII.GetBytes(IV);
                encdec.Padding = PaddingMode.PKCS7;
                encdec.Mode = CipherMode.CBC;
    
                ICryptoTransform icrypt = encdec.CreateDecryptor(encdec.Key, encdec.IV);
    
                byte[] enc = icrypt.TransformFinalBlock(textbytes, 0, textbytes.Length);
                icrypt.Dispose();
    
                return Convert.ToBase64String(enc);
            }

    <div id="mouseposition-extension-element-full-container" style="; inset: 0px; pointer-events: none; z-index: 2147483647; font-weight: 400;"> <div id="mouseposition-extension-element-rect-display" style="display: none; ; background: rgba(255, 255, 255, 0.7); outline: black solid 1px; font-size: 12px; z-index: 2147483647; justify-content: center; align-items: center; user-select: none; cursor: default; color: #000000; font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; width: 0px; height: 0px;">

    
    

    </div>

    
    

    </div>

    Sunday, February 21, 2021 8:17 PM

Answers

  • User1535942433 posted

    Hi brucey,

    According to your description,I have reproduce your problem. As far as I think,your problem is that  is not the encrypted stuff.

    More details,you could refer to below codes:

     public static string Encryp(string decrypted)
            {
                string IV = "qo1lc3sjd8zpt9cx";  //16 chars = 128 bytes
                string Key = "ow7dxys8glfor9tnc2ansdfo1etkfjcv"; // 32 chars = 256 bytes
                byte[] textbytes = UnicodeEncoding.ASCII.GetBytes(decrypted);
                AesCryptoServiceProvider encdec = new AesCryptoServiceProvider();
                encdec.BlockSize = 128;
                encdec.KeySize = 256;
                encdec.Key = ASCIIEncoding.ASCII.GetBytes(Key);
                encdec.IV = ASCIIEncoding.ASCII.GetBytes(IV);
                encdec.Padding = PaddingMode.PKCS7;
                encdec.Mode = CipherMode.CBC;
                ICryptoTransform icrypt = encdec.CreateEncryptor(encdec.Key, encdec.IV);
                byte[] enc = icrypt.TransformFinalBlock(textbytes, 0, textbytes.Length);
                icrypt.Dispose();
                return Convert.ToBase64String(enc);
            }
            static void Main(string[] args)
            {
                string newMessage = "I love programming";
                string encMessage;
                encMessage = Encryp(newMessage);
            }

    Best regards,

    Yijing Sun

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, February 22, 2021 5:50 AM

All replies

  • User1535942433 posted

    Hi brucey,

    According to your description,I have reproduce your problem. As far as I think,your problem is that  is not the encrypted stuff.

    More details,you could refer to below codes:

     public static string Encryp(string decrypted)
            {
                string IV = "qo1lc3sjd8zpt9cx";  //16 chars = 128 bytes
                string Key = "ow7dxys8glfor9tnc2ansdfo1etkfjcv"; // 32 chars = 256 bytes
                byte[] textbytes = UnicodeEncoding.ASCII.GetBytes(decrypted);
                AesCryptoServiceProvider encdec = new AesCryptoServiceProvider();
                encdec.BlockSize = 128;
                encdec.KeySize = 256;
                encdec.Key = ASCIIEncoding.ASCII.GetBytes(Key);
                encdec.IV = ASCIIEncoding.ASCII.GetBytes(IV);
                encdec.Padding = PaddingMode.PKCS7;
                encdec.Mode = CipherMode.CBC;
                ICryptoTransform icrypt = encdec.CreateEncryptor(encdec.Key, encdec.IV);
                byte[] enc = icrypt.TransformFinalBlock(textbytes, 0, textbytes.Length);
                icrypt.Dispose();
                return Convert.ToBase64String(enc);
            }
            static void Main(string[] args)
            {
                string newMessage = "I love programming";
                string encMessage;
                encMessage = Encryp(newMessage);
            }

    Best regards,

    Yijing Sun

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, February 22, 2021 5:50 AM
  • User-1664485818 posted

    Thanks yij sun :)

    encdec.CreateEncryptor
    
    
    
    
    Monday, February 22, 2021 11:11 AM