c# - AES 负字节

标签 c# java aes

以下是在 Java 中使用 AES 加密的摘录:

  encryptedData =   encryptCipher.doFinal(strToEncrypt.getBytes());

以下是c#中的摘录

 DecryptStringFromBytes_Aes(encrypted, myAes.Key, myAes.IV);

两者都使用字节数组,一个进行加密,另一个进行解密,Java 中的加密会产生一些存储在字节数组中的负值。

C# 使用字节数组来解密,但 C# 中的字节被定义为仅包含 0..255 之间的数字 - Java 将其 Byte 类型定义为 -128 到 127。

因此,我无法将加密数据发送到用 C# 编写的远程应用程序,因为它无法使用从 Java 应用程序发送的字节数组进行解密。

有没有人想出一个解决方案,让我告诉java在加密时不要产生负数?

该代码来自 Microsoft,MemoryStream 需要 byte[] 来为加密代码创建流... 不管是否提到过,我用 sbyte 替换了 byte[],但没有效果,因为 MemoryStream 需要 byte[]

static string DecryptStringFromBytes_Aes(sbyte[] cipherText, byte[] Key, byte[] IV)
    {
        // Check arguments. 
        if (cipherText == null || cipherText.Length <= 0)
            throw new ArgumentNullException("cipherText");
        if (Key == null || Key.Length <= 0)
            throw new ArgumentNullException("Key");
        if (IV == null || IV.Length <= 0)
            throw new ArgumentNullException("Key");

        // Declare the string used to hold 
        // the decrypted text. 
        string plaintext = null;

        // Create an Aes object 
        // with the specified key and IV. 
        using (Aes aesAlg = Aes.Create())
        {
            aesAlg.Key = Key;
            aesAlg.IV = IV;

            // Create a decrytor to perform the stream transform.
            ICryptoTransform decryptor = aesAlg.CreateDecryptor(aesAlg.Key, aesAlg.IV);

            // Create the streams used for decryption. 
            using (MemoryStream msDecrypt = new MemoryStream((byte)cipherText))
            {
                using (CryptoStream csDecrypt = new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read))
                {
                    using (StreamReader srDecrypt = new StreamReader(csDecrypt))
                    {


                        // Read the decrypted bytes from the decrypting stream
                        // and place them in a string.
                        plaintext = srDecrypt.ReadToEnd();
                    }
                }
            }

        }

        return plaintext;


    }

最佳答案

Java 的字节是有符号的,C# 的字节是无符号的(C# 中还有一种 sbyte 类型,但没有人使用,其工作方式与 Java 的字节类似)。

没关系。它们在某些方面是不同的,即

  • 当转换为 int 时,C# 的字节将进行零扩展,Java 的字节将进行符号扩展(这就是为什么当字节为在 Java 中使用)。
  • 当转换为字符串时,Java 的字节会将其 128 - 255 范围映射为 -128 - -1。忽略它即可。

这些字节的实际值(即它们的位模式)才是真正重要的,一个 0xAA 的字节将是 0xAA,无论您将其解释为 170(如在 C# 中)还是 -86(如在 java )。这是同样的事情,只是将其打印为字符串的方式不同。

<小时/>

new MemoryStream((byte)cipherText)) 绝对没有做正确的事情(或者任何事情,它甚至不应该编译)。相关的 new MemoryStream((byte[])cipherText)) 也不起作用,您不能像这样在原始数组之间进行转换。 cipherText 应该只是一个 byte[] 开始。

关于c# - AES 负字节,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23079047/

相关文章:

c# - UDP "Connect"- C# 中的速度

java - 使用 charAt() 查找空格、换行符和制表符

python - 将二进制数据加密为二进制数据并解密

javascript - 将 AES 解密从 CryptoJS 移植到 Php

c# - 使用 DateTime.MinValue 还是可以为 null 的 DateTime 更好?

c# - 如何获取带换行符的textarea文本?

java - 使用itext生成pdf后直接下载到浏览器

java - 解密java中在php中使用AES加密的字符串

c# - 同步调用异步方法

java - 如何在android中的gridview中添加图像按钮