C#无符号字节加密到Java签名字节解密 [英] C# Unsigned bytes Encryption to Java Signed bytes Decryption

查看:210
本文介绍了C#无符号字节加密到Java签名字节解密的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在C#中有一个应用程序使用RijndaelManaged加密部分文件(因为它们是大文件)。所以我将我的文件转换为字节数组并仅加密它的一部分。

I have an application in C# that encrypt part of my files (because they are big files) using RijndaelManaged. So I convert my file to byte arrays and encrypt only a part of it.

然后我想用Java解密文件。因此,我必须解密仅在C#中加密的部分(意味着那些字节)。

Then I want to decrypt the file using Java. So I have to decrypt only part of the file (means those bytes) that was encrypted in C#.

问题来了。因为在C#中我们有无符号字节,而在Java中我们有有符号字节。所以我的加密和解密没有按照我想要的方式工作。

Here the problem comes. Because in C# we have unsigned bytes and in Java we have signed bytes. So my encryption and decryption not working the way I want.

在C#中我加入了加密字节和普通字节,并用文件保存它们.WriteAllBytes 。所以我不能在这里使用 sbyte 或者我不知道该怎么做:

In C# I have joined the encrypted bytes and normal bytes together and saved them with File.WriteAllBytes. So I can't use sbyte here or I don't know how to do it:

byte[] myEncryptedFile = new byte[myFile.Length];
for (long i = 0; i < encryptedBlockBytes.Length; i++)
{
   myEncryptedFile[i] = encryptedBlockBytes[i];
}
for (long i = encryptedBlockBytes.Length; i < myFile.Length; i++)
{
   myEncryptedFile[i] = myFileBytes[i];

}

File.WriteAllBytes(@"C:\enc_file.big", myEncryptedFile);

(并且Java中的解密代码完全相同)

( And there is an exact same code for decryption in Java )

所以我的问题是:


  • C#中是否有 WriteAllsBytes

  • 或者我可以在Java中使用无符号字节吗?

  • 或者我的问题的任何其他解决方案?

  • Is there any WriteAllsBytes in C#?
  • Or can I use unsigned bytes in Java?
  • Or any other solutions to my problem?

推荐答案

虽然你不能在Java中使用无符号字节,但你可以忽略这个问题。

Although you cannot use unsigned bytes in Java, you may simply ignore the issue.

AES - 以及所有现代对称密码 - 按字节操作,输入和输出已定义为字节(或八位字节)。输入和输出已由NIST标准化,并且测试向量可用。

AES - and all modern symmetric ciphers - operates on bytes, and the input and output have been defined to be bytes (or octets). Input and output have been standardized by NIST and test vectors are available.

如果查看字节的单独位内容,则 {200,201,202}在C#中的 {(字节)200,(字节)201,(字节)202} 在Java中是相同的。这是因为Java使用字节的双补码表示。

If you look at the separate bit content of the bytes then {200,201,202} in C# and {(byte)200, (byte)201, (byte)202} in Java are identical. This is because Java uses two-complement representation of bytes.

将数字 200 作为整数:这将是 11010000 二进制,如果在两个补码中的(带符号)字节中使用,则表示Java中的数字 -56 。现在,对称密码将简单地将这些位转换为另一个位(通常使用完整的位)。

Take the number 200 as integer: this will be 11010000 in binary, representing the number -56 in Java if used in a (signed) byte in two complements. Now symmetric ciphers will simply transform these bits to another (normally using a full block of bits).

一旦你找到答案,当你看到单独的位时,你会发现它们在C#和Java 中是相同的。但是,C#会将这些解释为无符号值,将Java解释为有符号值。

Once you have retrieved the answer you will see that they are identical both in C# and Java when you look at the separate bits. C# will however interpret those as unsigned values and Java as signed values.

如果要在Java中打印或使用这些值作为带符号的数字,则必须转换它们到正整数。这样做的方法是使用 int p = b& 0xFF

If you want to print out or use these values as signed numbers in Java then you have to convert them to positive signed integers. The way to do this is to use int p = b & 0xFF.

这样做会有以下情况(我会再次使用数字200):

This does the following (I'll use the number 200 again):


  1. (负)字节值扩展为有符号整数,记住符号位:

  1. The (negative) byte value is expanded to a signed integer, remembering the sign bit:

11010000 成为 11111111 11111111 11111111 11010000

此值为通过执行二元AND运算符掩码 0xFF 00000000 00000000 00000000 11111111

This value is "masked" with 0xFF or 00000000 00000000 00000000 11111111 by performing the binary AND operator:

11111111 11111111 11111111 11010000& 00000000 00000000 00000000 11111111 = 00000000 00000000 00000000 11010000

此值与值相同 200 作为有符号整数。

This value is identical to the value 200 as a signed integer.

这篇关于C#无符号字节加密到Java签名字节解密的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆