Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/assembly/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 使用ASP.NET加密和解密数据时出错_C#_Asp.net_.net_C# 4.0_Cryptography - Fatal编程技术网

C# 使用ASP.NET加密和解密数据时出错

C# 使用ASP.NET加密和解密数据时出错,c#,asp.net,.net,c#-4.0,cryptography,C#,Asp.net,.net,C# 4.0,Cryptography,但是我得到了一个错误:-- I am using this code for Encrypt and Dycrpt the data using asp.net . 这是我的aspx.cs代码: **Specified initialization vector (IV) does not match the block size for this algorithm**. here is a code:- my .cs file is:- public static class En

但是我得到了一个错误:--

I am using this code for Encrypt and Dycrpt the data using asp.net .
这是我的aspx.cs代码:

**Specified initialization vector (IV) does not match the block size for this algorithm**. 

here is a code:-

my .cs file is:-

public static class Encrypt_Decrypt
{
static Encrypt_Decrypt()
{

}
public static string EncryptString(string ClearText)
{

    byte[] clearTextBytes = Encoding.UTF8.GetBytes(ClearText);

    System.Security.Cryptography.SymmetricAlgorithm rijn = SymmetricAlgorithm.Create();

    MemoryStream ms = new MemoryStream();
    byte[] rgbIV = Encoding.ASCII.GetBytes("hanuservicestalknsolve");
    byte[] key = Encoding.ASCII.GetBytes("hanuservicestalknsolve");
CryptoStream cs = new CryptoStream(ms, rijn.CreateEncryptor(key, rgbIV),CryptoStreamMode.Write);

    cs.Write(clearTextBytes, 0, clearTextBytes.Length);

    cs.Close();

    return Convert.ToBase64String(ms.ToArray());
}
private static string DecryptString(string EncryptedText)
{
    byte[] encryptedTextBytes = Convert.FromBase64String(EncryptedText);

    MemoryStream ms = new MemoryStream();

    System.Security.Cryptography.SymmetricAlgorithm rijn = SymmetricAlgorithm.Create();


    byte[] rgbIV = Encoding.ASCII.GetBytes("hanuservicestalknsolve");
    byte[] key = Encoding.ASCII.GetBytes("hanuservicestalknsolve");
    CryptoStream cs = new CryptoStream(ms, rijn.CreateDecryptor(key, rgbIV),
    CryptoStreamMode.Write);

    cs.Write(encryptedTextBytes, 0, encryptedTextBytes.Length);

    cs.Close();

    return Encoding.UTF8.GetString(ms.ToArray());

}
}
根据以下文件:

IV属性的大小必须与BlockSize属性的大小除以8相同

我怀疑您会发现
rijn.BlockSize
是128,所以您应该提供一个32字节的IV


(不清楚为什么您的变量被称为
rgbIV
,并且您没有任何使用
语句的合适
。我希望您在实际代码中也没有使用固定的IV和键……否则它不会提供太多安全性。)

@user3168616:下面是正确的代码

问题在于为Key和rgbIV提供的字符串的长度 键和rgbIV的长度应为16位 获取以位为单位的字符串长度的代码

 string eventi = Encrypt_Decrypt.EncryptString(DataBinder.Eval(e.Item.DataItem,     "name_of_post_id").ToString());
    string post = Encrypt_Decrypt.EncryptString(DataBinder.Eval(e.Item.DataItem, "compreq_eventid").ToString());
代码的修改版本(唯一的更改是字符串长度)

Console.WriteLine(System.Text.ASCIIEncoding.ASCII.GetByteCount("abcdefghijklmnopabcdefghijklmnop"));
class Program
{
    static void Main(string[] args)
    {
        string strText = "this is the string";
        string encryptedString = Encrypt_Decrypt.EncryptString(strText);
        Console.WriteLine(encryptedString);
        string decryptedString = Encrypt_Decrypt.DecryptString(encryptedString);
        Console.WriteLine(decryptedString);
        Console.ReadKey();
    }
}

public static class Encrypt_Decrypt
{
    public static string EncryptString(string ClearText)
    {

        byte[] clearTextBytes = Encoding.UTF8.GetBytes(ClearText);

        System.Security.Cryptography.SymmetricAlgorithm rijn = SymmetricAlgorithm.Create();

        MemoryStream ms = new MemoryStream();
        byte[] rgbIV = Encoding.ASCII.GetBytes("abcdefghijklmnop");
        byte[] key = Encoding.ASCII.GetBytes("abcdefghijklmnop");
        CryptoStream cs = new CryptoStream(ms, rijn.CreateEncryptor(key, rgbIV), CryptoStreamMode.Write);

        cs.Write(clearTextBytes, 0, clearTextBytes.Length);

        cs.Close();

        return Convert.ToBase64String(ms.ToArray());
    }
    public static string DecryptString(string EncryptedText)
    {
        byte[] encryptedTextBytes = Convert.FromBase64String(EncryptedText);

        MemoryStream ms = new MemoryStream();

        System.Security.Cryptography.SymmetricAlgorithm rijn = SymmetricAlgorithm.Create();


        byte[] rgbIV = Encoding.ASCII.GetBytes("abcdefghijklmnop");
        byte[] key = Encoding.ASCII.GetBytes("abcdefghijklmnop");
        CryptoStream cs = new CryptoStream(ms, rijn.CreateDecryptor(key, rgbIV),
        CryptoStreamMode.Write);

        cs.Write(encryptedTextBytes, 0, encryptedTextBytes.Length);

        cs.Close();

        return Encoding.UTF8.GetString(ms.ToArray());

    }
}