c# - 'Stream 不支持使用 CryptoStream 对象寻找'

标签 c# encryption cryptography

我正在尝试使用以下代码加密一些数据:

public static byte[] EncryptString(byte[] input, string password)
{
    PasswordDeriveBytes pderiver = new PasswordDeriveBytes(password, null);
    byte[] ivZeros = new byte[8];
    byte[] pbeKey = pderiver.CryptDeriveKey("RC2", "MD5", 128, ivZeros);

    RC2CryptoServiceProvider RC2 = new RC2CryptoServiceProvider();

    byte[] IV = new byte[8];
    ICryptoTransform encryptor = RC2.CreateEncryptor(pbeKey, IV);

    MemoryStream msEncrypt = new MemoryStream();
    CryptoStream csEncrypt = new CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write);
    csEncrypt.Write(input, 0, input.Length);
    csEncrypt.FlushFinalBlock();

    return msEncrypt.ToArray();
}

但是,当它初始化我的 CryptoStream 对象时,它会抛出以下错误:

"Stream does not support seeking." To clarify, there is no error handling in the above code, so just running this will not "break", persay. But stepping through the code, the CryptoStream object will show this error in its properties once it's been initialized.

这是为什么?我该如何避免呢?

最佳答案

所以代码实际上运行没有异常,但问题是当你在调试器中查看属性时?如果是这样,那很容易 - 某些属性(例如 Position)依赖于能够在流中查找。您不能使用 CryptoStream 执行此操作 - 因此属性评估失败。

您无需避免这种情况 - 这完全没问题。

关于c# - 'Stream 不支持使用 CryptoStream 对象寻找',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1625638/

相关文章:

java - 将 php mcrypt_encrypt MCRYPT_3DES 转换为 Java

c# - 为什么我在 Azure Function 中使用 async/Task 时出错,但在 Console App 中使用时却没有错误?

c# - 何时将 OrderByCompletion (Jon Skeet) 与 Parallel.ForEach 与异步委托(delegate)一起使用

php openssl - RSA_padding_check_PKCS1_type_2 和 RSA_EAY_PRIVATE_DECRYPT :padding check failed:rsa_eay. c:602:

java - C++/Openssl 从编码字节中获取 RSA key (由 java 编码)

c++ - 安全清除内存并重新分配

linux - 使用 openssl 作为命令行计算 AES128 CMAC

performance - AS3 中的快速 RSA 加密

c# - WPF 中针对不同客户的不同 GUI 定义

c# - TextBox.Text += "string";与 TextBox.AppendText ("string");