base-64 char数组或string-azure服务器的长度无效

时间:2017-06-04 17:55:44

标签: c# azure encryption

我有服务器在azure上运行,经过几个小时的调试我改变了azure在64位机器上运行,现在当我尝试加密或解密时我有这个错误:" base-64 char的长度无效数组或字符串"会是什么呢? (当我运行本地测试所有工作时)

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Web;

namespace Server
{
    public class Crypto
    {
        static readonly string PasswordHash = "P@@Sw0rd";
        static readonly string SaltKey = "S@LT&KEY";
        static readonly string VIKey = "@1B2c3D4e5F6g7H8";

        public static string Encrypt(string plainText)
        {
            byte[] plainTextBytes = Encoding.UTF8.GetBytes(plainText);

            byte[] keyBytes = new Rfc2898DeriveBytes(PasswordHash, Encoding.ASCII.GetBytes(SaltKey)).GetBytes(256 / 8);
            var symmetricKey = new RijndaelManaged() { Mode = CipherMode.CBC, Padding = PaddingMode.Zeros };
            var encryptor = symmetricKey.CreateEncryptor(keyBytes, Encoding.ASCII.GetBytes(VIKey));

            byte[] cipherTextBytes;

            using (var memoryStream = new MemoryStream())
            {
                using (var cryptoStream = new CryptoStream(memoryStream, encryptor, CryptoStreamMode.Write))
                {
                    cryptoStream.Write(plainTextBytes, 0, plainTextBytes.Length);
                    cryptoStream.FlushFinalBlock();
                    cipherTextBytes = memoryStream.ToArray();
                    cryptoStream.Close();
                }
                memoryStream.Close();
            }

            StringBuilder crypt = new StringBuilder(Convert.ToBase64String(cipherTextBytes));
            if (crypt.Length > 0)
            {
                for (int i = 0; i < 3; i++)
                {
                    if (crypt[crypt.Length - 1 - i] == '=')
                    {
                        crypt[crypt.Length - 1 - i] = '*';
                    }
                    else break;
                }
            }
            return crypt.ToString();
        }

        public static string Decrypt(string encryptedText)
        {
            StringBuilder crypt = new StringBuilder(encryptedText);
            if (crypt.Length > 0)
            {
                for (int i = 0; i < 3; i++)
                {
                    if (crypt[crypt.Length - 1 - i] == '*')
                    {
                        crypt[crypt.Length - 1 - i] = '=';
                    }
                    else break;
                }
            }
            byte[] cipherTextBytes = Convert.FromBase64String(crypt.ToString());
            byte[] keyBytes = new Rfc2898DeriveBytes(PasswordHash, Encoding.ASCII.GetBytes(SaltKey)).GetBytes(256 / 8);
            var symmetricKey = new RijndaelManaged() { Mode = CipherMode.CBC, Padding = PaddingMode.None };

            var decryptor = symmetricKey.CreateDecryptor(keyBytes, Encoding.ASCII.GetBytes(VIKey));
            var memoryStream = new MemoryStream(cipherTextBytes);
            var cryptoStream = new CryptoStream(memoryStream, decryptor, CryptoStreamMode.Read);
            byte[] plainTextBytes = new byte[cipherTextBytes.Length];

            int decryptedByteCount = cryptoStream.Read(plainTextBytes, 0, plainTextBytes.Length);
            memoryStream.Close();
            cryptoStream.Close();
            return Encoding.UTF8.GetString(plainTextBytes, 0, decryptedByteCount).TrimEnd("\0".ToCharArray());
        }
    }
}

1 个答案:

答案 0 :(得分:0)

在我的Azure Web App(x64平台)上测试了您的代码之后,我发现它在我身边运行良好。这是我的测试代码。

{{1}}

这是我从服务器获得的结果。

enter image description here

我建议您测试服务器中的高级代码并发布结果。

  

base-64 char数组或字符串

的长度无效

解密加密文本时经常会发生异常。如果将加密文本存储在数据库中,由于用于存储数据的列没有足够的空间,有时加密文本会丢失一些数据。例如,您定义的列类型为nvarchar(32),加密文本的长度为56,大于32.请确保在解密之前未更改加密文本。