我有一个16字节十六进制密钥“ F81AFDEA26D680BF ”,还有一个 16byte加密文本,十六进制为“ 3508D26A7064CF68 ”。 我需要使用DES来解密上面的文本。我收到一个错误“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);
}
答案 0 :(得分:1)
您的所有数据都不是Base-64编码的,因此这不是您想要使用的功能。看起来你已经有了KeyHexAscii
函数,你会想要使用你编写的任何反转它的函数。
理想情况下,您可以编写加密接口来操作字节数组。它不应该涉及编码和解码数据。您应该处理读取数据并将其转换为其他位置的字节。