2017-06-04 42 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多。請確保加密文本在解密之前未被更改。

相關問題