2017-06-04 37 views
0

我有服务器上运行Azure,经过几个小时的调试我改变了Azure在64位机器上运行,现在当我尝试加密或解密我有这个错误:“基数为64的char数组或字符串的无效长度”可能是什么? (当我运行本地测试的所有作品)基地64字符阵列或字符串azure服务器无效的长度

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 = "[email protected]@Sw0rd"; 
     static readonly string SaltKey = "[email protected]&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()); 
     } 
    } 
} 
+0

IV必须是不可预知的(阅读:随机)。不要使用静态IV('VIKey'),因为这会使密码具有确定性,因此不会在语义上安全。观察密文的攻击者可以确定何时之前发送了相同的消息前缀。 IV不是秘密的,所以你可以把它和密文一起发送。通常,它只是在密文前面加上,然后在解密之前切掉。 –

回答

0

测试了我的Azure的Web应用程序(x64平台)你的代码后,我发现它的工作好就在我身边。这是我的测试代码。

public ActionResult TestCrypto() 
{ 
    string encryptedText = Crypto.Encrypt("123456"); 
    string text = "Encrypted Text : " + encryptedText + "<br/>"; 
    string originalText = Crypto.Decrypt(encryptedText); 
    text += "Origial Text : " + originalText; 
    return Content(text); 
} 

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

enter image description here

我建议你在测试服务器上的代码,并发布结果。

invalid length for a base-64 char array or string

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

相关问题