AES解密错误“输入数据不是完整块“。错误vb.net [英] AES decryption error " The input data is not a complete block." Error vb.net

查看:2753
本文介绍了AES解密错误“输入数据不是完整块“。错误vb.net的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直得到这个输入数据不是一个完整的块。错误时解密。该函数成功加密纯文本并将IV放在文本框中。我使用加密的数据和IV从文本解密原始数据,但我不断得到错误。我不知道我在哪里错了。我的代码

I keep getting this "The input data is not a complete block." error while decrypting. The function successfully encrypts plain text and puts the IV in a textbox. I am using the encrypted data and the IV from text to decrypt the original data but I keep getting the error. I have no idea where I have gone wrong. Heres my code

Imports System.IO 'Import file I/O tools
Imports System.Security.Cryptography 'Import encryption functionality
Imports System.Text 'Import text based processing tools`


Public Class Form1
    Private Function AESEncryption(ByVal clearText As String, ByVal key As String) As String
        Dim salt As String = tbpassword.Text.Insert(tbpassword.Text.Length - 1, "7?1!")

        Dim Md5 As New MD5CryptoServiceProvider()
        Dim Encryptionkey As Byte() = Md5.ComputeHash(Encoding.UTF8.GetBytes(key & salt))



        Dim AES As New AesCryptoServiceProvider
        AES.Key = Encryptionkey
        AES.Mode = CipherMode.CBC
        tbIV.Text = Convert.ToBase64String(AES.IV)

        Dim datain() As Byte = Encoding.UTF8.GetBytes(clearText)

        Dim memorystream As New MemoryStream(datain)
        Dim cstream As New CryptoStream(memorystream, AES.CreateEncryptor, CryptoStreamMode.Write)
        cstream.Write(datain, 0, datain.Length)


        AES.Clear()
        memorystream.Close()

        Return Convert.ToBase64String(memorystream.ToArray())
    End Function
    Private Function AESdecryption(ByVal encrypteddata As String, ByVal key As String) As String
        Dim salt As String = tbpassword.Text.Insert(tbpassword.Text.Length - 1, "7?1!")

        Dim Md5 As New MD5CryptoServiceProvider()
        Dim Encryptionkey As Byte() = Md5.ComputeHash(Encoding.UTF8.GetBytes(key & salt)) 'uses password and salt to create a hash byte array

        Dim EncryptionIn() As Byte = Convert.FromBase64String(encrypteddata)

        Dim AES As New AesCryptoServiceProvider
        AES.Key = Encryptionkey
        AES.Mode = CipherMode.CBC
        AES.IV = Convert.FromBase64String(tbIV.Text)

        Dim ms As New MemoryStream(EncryptionIn)


        Dim cryptostream As New CryptoStream(ms, AES.CreateDecryptor, CryptoStreamMode.Read)
        Dim decrypteddata() As Byte
        ReDim decrypteddata(EncryptionIn.Length - 1)
        cryptostream.Read(decrypteddata, 0, decrypteddata.Length)


        AES.Clear()
        ms.Close()

        Return Convert.ToBase64String(ms.ToArray)

    End Function
    Private Sub btnencrypt_Click(sender As Object, e As EventArgs) Handles btnencrypt.Click
        tbencrypteddata.Text = AESEncryption(tbuserdata.Text, tbpassword.Text)



    End Sub

    Private Sub btndecrypt_Click(sender As Object, e As EventArgs) Handles Button1.Click
        tbdecrypteddata.Text = AESdecryption(tbencrypteddata.Text, tbpassword.Text)
    End Sub
End Class


推荐答案

除非你要手动确保你的输入是BlockSize的倍数(以位为单位),请务必指定一个填充:

Unless you are going to manually make sure your input is in multiples of BlockSize (in bits), make sure to specify a padding:

示例代码:

byte[] Process(byte[] bInput, bool decrypt = false)
{
    byte[] bOutput = null;

    using (var c = System.Security.Cryptography.AesCryptoServiceProvider.Create())
    {
        c.BlockSize = 128;
        c.IV = new byte[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0xa, 0xb, 0xc, 0xd, 0xe, 0xf }; // blocksize / 8 = 16 long
        c.KeySize = 256;
        c.Key = new byte[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0xa, 0xb, 0xc, 0xd, 0xe, 0xf, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0xa, 0xb, 0xc, 0xd, 0xe, 0xf }; // key = keysize / 8 = 32 bytes
        c.Mode = System.Security.Cryptography.CipherMode.CBC;
        c.Padding = System.Security.Cryptography.PaddingMode.PKCS7;
        if (decrypt)
        {
            using (var t = c.CreateDecryptor())
            {
                bOutput = t.TransformFinalBlock(bInput, 0, bInput.Length);
            }
        }
        else
        {
            using (var t = c.CreateEncryptor())
            {
                bOutput = t.TransformFinalBlock(bInput, 0, bInput.Length);
            }
        }
    }

    return bOutput;
}

这篇关于AES解密错误“输入数据不是完整块“。错误vb.net的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆