C# RC2加密/解密

C# RC2加密/解密,c#,encryption,cryptography,cryptographicexception,rc2-cipher,C#,Encryption,Cryptography,Cryptographicexception,Rc2 Cipher,当我尝试解密字符串时,visual studio会引发异常: System.Security.Cryptography.Cryptography异常,并表示用于解密的数据长度无效。在RC2_解密方法中,当编译器到达cs.Close时出现异常 static byte[] RC2_Encrypt(byte[] bytesToBeEncrypted, byte[] passwordBytes) { byte[] encryptedBytes = null; st

当我尝试解密字符串时,visual studio会引发异常: System.Security.Cryptography.Cryptography异常,并表示用于解密的数据长度无效。在RC2_解密方法中,当编译器到达cs.Close时出现异常

static byte[] RC2_Encrypt(byte[] bytesToBeEncrypted, byte[] passwordBytes)
    {
        byte[] encryptedBytes = null;
        string salt = "D495560961CCCFE0";
        byte[] saltBytes = Encoding.UTF8.GetBytes(salt);
        using (MemoryStream msStream = new MemoryStream())
        {
            using (RC2CryptoServiceProvider RC2 = new RC2CryptoServiceProvider())
            {
                RC2.KeySize = 128;
                RC2.BlockSize = 64;
                var key = new Rfc2898DeriveBytes(passwordBytes, saltBytes, 1000);
                RC2.Key = key.GetBytes(RC2.KeySize / 8);
                RC2.IV = key.GetBytes(RC2.BlockSize / 8);
                RC2.Mode = CipherMode.CBC;
                using (var cs = new CryptoStream(msStream, RC2.CreateEncryptor(), CryptoStreamMode.Write))
                {
                    cs.Write(bytesToBeEncrypted, 0, bytesToBeEncrypted.Length);
                    cs.Close();
                }
                encryptedBytes = msStream.ToArray();
            }
        }
        return encryptedBytes;
    }

static byte[] RC2_Decrypt(byte[] bytesToBeDecrypted, byte[] passwordBytes)
    {
        byte[] decryptedBytes = null;
        string salt = "D495560961CCCFE0";
        byte[] saltBytes = Encoding.UTF8.GetBytes(salt);
        using (MemoryStream msStream = new MemoryStream())
        {
            using (RC2CryptoServiceProvider RC2 = new RC2CryptoServiceProvider())
            {
                RC2.KeySize = 128;
                RC2.BlockSize = 64;
                var key = new Rfc2898DeriveBytes(passwordBytes, saltBytes, 1000);
                RC2.Key = key.GetBytes(RC2.KeySize / 8);
                RC2.IV = key.GetBytes(RC2.BlockSize / 8);
                RC2.Mode = CipherMode.CBC;
                using (var cs = new CryptoStream(msStream, RC2.CreateDecryptor(), CryptoStreamMode.Write))
                {
                    cs.Write(bytesToBeDecrypted, 0, bytesToBeDecrypted.Length);
                    cs.Close();
                }
                decryptedBytes = msStream.ToArray();
            }
        }
        return decryptedBytes;
    }
在这里,我只是测试这些方法。首先,我尝试加密一个简单的字符串

static void Main(string[] args)
    {
        string password = "770A8A65DA156D24EE2A093277530142";
        byte[] passwordBytes = Encoding.UTF8.GetBytes(password);
        Console.WriteLine("Encrypting");
        string str = "Hello world";
        Console.WriteLine(EncString(str, password));
        byte[] encArray = Encoding.UTF8.GetBytes(str);
        Console.WriteLine(DecString(str, password));
        Console.ReadKey();
    }
我用于字符串加密的方法:

 static string EncString(string message, string password)
    {
        byte[] byresToBeEncrypted = Encoding.UTF8.GetBytes(message);
        byte[] passwordBytes = Encoding.UTF8.GetBytes(password);
        byte[] bytesToBeEncrypted = RC2_Encrypt(byresToBeEncrypted, passwordBytes);
        string result = Convert.ToBase64String(byresToBeEncrypted);
        return result;
    }

static string DecString(string message, string password)
    {
        byte[] bytesToBeEncrypted = Encoding.UTF8.GetBytes(message);
        byte[] passwordBytes = Encoding.UTF8.GetBytes(password);
        byte[] bytesToBeDecrypted = RC2_Decrypt(bytesToBeEncrypted, passwordBytes);
        string result = Encoding.UTF8.GetString(bytesToBeDecrypted);
        return result;
    }

这些方法可以对文本文件进行加密和解密,这就是我所需要的。但我还有一个问题。为什么这不适用于简单字符串变量

解密端说它有多少字节?您希望它有多少字节?当编译器到达Cs.Write时,调试器说ByTestObedCaryPted在数组中包含11个元素。说真的,我不知道它应该包含多少字节。我正在使用相同的AES模板,它可以工作。我尝试了你的代码,效果很好。你能给我们造成错误的参数吗?好的。我添加了整个项目。您似乎没有在主方法中使用密文进行解密。看来你在试图解密明文。