0

我有一个 16 字节的十六进制密钥“ F81AFDEA26D680BF ”和一个16 字节的十六进制加密文本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);
    }
4

1 回答 1

1

您的数据似乎都不是 Base-64 编码的,因此这不是您要使用的功能。看来你已经有了一个KeyHexAscii函数,你会想要使用你编写的任何函数来逆转它。

理想情况下,您将编写加密接口以对字节数组进行操作。它不应该参与编码和解码数据。您应该处理读取数据并将其转换为其他地方的字节。

于 2013-10-25T16:43:36.337 回答