獲取無效字符我有一個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);
}
您正在將一個十六進制編碼(基本16)字符串傳遞給接受基本64字符串的方法。你期望會發生什麼? – Jon
我需要使用十六進制16進制密鑰來解密特定的加密16hex。建議我用一個代碼來做到這一點。 – user2462086