我有服务器在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());
}
}
}