使用ASP.NET将错误加密并解密数据

时间:2014-01-20 06:49:04

标签: c# asp.net .net c#-4.0 cryptography

I am using this code for Encrypt and Dycrpt the data using asp.net .

但我收到错误: -

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

}
}

这是我的aspx.cs代码:

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

2 个答案:

答案 0 :(得分:0)

来自SymmetricAlgorithm.IV的文档:

  

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

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

(不清楚为什么你的变量被调用rgbIV,而且你没有任何适当的using语句。我希望你没有使用固定的IV和密钥< em>真正的代码......否则它没有提供太多安全性。)

答案 1 :(得分:0)

@ user3168616:以下是有效的更正代码。

问题在于您为Key和rgbIV提供的字符串的长度 Key和rgbIV的长度应为16咬 在bites中获取字符串长度的代码

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

    }
}