2013-10-25 43 views
0

獲取無效字符我有一個​​16字節的六角扳手「F81AFDEA26D680BF」,也是一個16字節的加密文本十六進制爲「3508D26A7064CF68」。 我需要使用DES解密上述文本。我收到一個錯誤「Base-64字符串中的無效字符」。使用的代碼是中的Base-64字符串

static byte[] bytes = Encoding.ASCII.GetBytes(KeyHexAscii("F81AFDEA26D680BF")); 
public static string Decrypt(string cryptedString) 
    { 
     if (String.IsNullOrEmpty(cryptedString)) 
     { 
      throw new ArgumentNullException("The string which needs to be decrypted can not be null."); 
     } 

     DESCryptoServiceProvider cryptoProvider = new DESCryptoServiceProvider(); 
     MemoryStream memoryStream = new MemoryStream(Convert.FromBase64String(cryptedString)); 
     CryptoStream cryptoStream = new CryptoStream(memoryStream, cryptoProvider.CreateDecryptor(bytes, bytes), CryptoStreamMode.Read); 
     StreamReader reader = new StreamReader(cryptoStream); 

     return reader.ReadToEnd(); 
    } 

    public static string Encrypt(string originalString) 
    { 
     if (String.IsNullOrEmpty(originalString)) 
     { 
      throw new ArgumentNullException("The string which needs to be encrypted can not be null."); 
     } 

     DESCryptoServiceProvider cryptoProvider = new DESCryptoServiceProvider(); 
     MemoryStream memoryStream = new MemoryStream(); 
     CryptoStream cryptoStream = new CryptoStream(memoryStream, cryptoProvider.CreateEncryptor(bytes, bytes), CryptoStreamMode.Write); 

     StreamWriter writer = new StreamWriter(cryptoStream); 
     writer.Write(originalString); 
     writer.Flush(); 
     cryptoStream.FlushFinalBlock(); 
     writer.Flush(); 

     return Convert.ToBase64String(memoryStream.GetBuffer(), 0, (int)memoryStream.Length); 
    } 
+2

您正在將一個十六進制編碼(基本16)字符串傳遞給接受基本64字符串的方法。你期望會發生什麼? – Jon

+0

我需要使用十六進制16進制密鑰來解密特定的加密16hex。建議我用一個代碼來做到這一點。 – user2462086

回答

1

您的數據都沒有顯示爲Base-64編碼,因此這不是您想要使用的功能。看起來你已經有了一個KeyHexAscii函數,你可以使用你寫的任何函數來反轉它。

理想情況下,你會編寫你的密碼接口來操作字節數組。它不應該參與編碼和解碼數據。您應該處理讀取數據並將其轉換爲其他地方的字節。

+0

'KeyHexAscii'將十六進制轉爲文本而不是字節。這不是解碼十六進制的有效方法。所以'KeyHexAscii'發生的任何事情都是不正確的。 –