Convert.FromBase64String的长度无效

时间:2014-11-19 15:09:24

标签: c# string encryption cryptography

所以我试图解密一个加密的字符串,它加密很好,有时候解密它,如果我很幸运但通常字符串长度太长..首先这里是我调用的代码解密方法

decrypted = Decrypt(recieved, getString(anarchyAes.Key));
addToChat(decrypted);

收到的只是加密字符串,另一个参数是解密密钥。这是Decrypt方法..

public static string Decrypt(string cipherString, string securityKey)
    {
        var key = securityKey;
        var keyArray = Encoding.UTF8.GetBytes(key);

        var tdes = new TripleDESCryptoServiceProvider
        {
            Key = keyArray,
            Mode = CipherMode.ECB,
            Padding = PaddingMode.PKCS7
        };

        var cTransform = tdes.CreateDecryptor();
        var toEncryptArray = Convert.FromBase64String(cipherString);
        var resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length);
        tdes.Clear();
        return Encoding.UTF8.GetString(resultArray);
    }

它失败

var toEncryptArray = Convert.FromBase64String(cipherString);

当字符串超过7个字符给出错误时,64个基本字符串的长度无效。我想知道如何解决这个问题,我们对此表示赞赏。

加密代码:

public static string Encrypt(string toEncrypt, string securityKey)
        {
            var key = securityKey;
            var keyArray = Encoding.UTF8.GetBytes(key);

            var tdes = new TripleDESCryptoServiceProvider
            {
                Key = keyArray,
                Mode = CipherMode.ECB,
                Padding = PaddingMode.PKCS7
            };

            var cTransform = tdes.CreateEncryptor();
            var toEncryptArray = Encoding.UTF8.GetBytes(toEncrypt);
            var resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length);
            tdes.Clear();
            return Convert.ToBase64String(resultArray, 0, resultArray.Length);
        }

0 个答案:

没有答案