将base64编码的字符串转换为java字节数组[重复]

时间:2022-10-24 14:11:01

I am writing a decryption class (AES/CBC/PKCS7Padding) where the encrypted data is coming from C#. I want to take the following string (which is base64 encoded):

我正在编写一个解密类(AES / CBC / PKCS7Padding),其中加密数据来自C#。我想采取以下字符串(base64编码):

usiTyri3/gPJJ0F6Kj9qYL0w/zXiUAEcslUH6/zVIjs=

and convert it to a byte array in java to pass into the SecretKeySpec as the key. I know there is the issue of C# having unsigned bytes and java only having signed bytes. How then can I pass this sting which has values greater than 127 within it and have java accept the key and initialization vectors?

并将其转换为java中的字节数组,作为密钥传递给SecretKeySpec。我知道存在C#具有无符号字节且java仅具有有符号字节的问题。那么我怎么能通过这个值大于127的sting并让java接受密钥和初始化向量?

1 个解决方案

#1


You don't have to worry about byte signedness because base64 encoded data never uses more than 6 bits in each byte (that's why it's called base 64, because you only use 64 characters which is 6 bits, to represent part of a data byte).

您不必担心字节签名,因为base64编码数据在每个字节中从不使用超过6位(这就是为什么它被称为base 64,因为您只使用64位字符,即6位,表示数据字节的一部分) 。

If your concern is the resulting data (3 data bytes for every 4 base64 characters), don't worry about that, either. An unsigned byte 255 in C# is the same as the signed byte -1 in Java.

如果你担心的是结果数据(每4个base64字符有3个数据字节),也不用担心。 C#中的无符号字节255与Java中的带符号字节-1相同。

To encode data, you can bitwise-and each byte with 0xff and store it in an int, then encode the least significant 8 bits. Or just bitwise-or each byte with 0x80 and store it in ant int and decode the least significant 8 bits.

要对数据进行编码,您可以按位和每个字节使用0xff并将其存储在int中,然后对最低有效8位进行编码。或者只是按位 - 或每个字节带有0x80并将其存储在ant int中并解码最低有效8位。

But I think you would be better off using Bouncy Castle or the standard JCE to deal with all that stuff. The 'S' in PKCS7 means Standard so data encrypted in C# should decrypt fine in Java and vice versa.

但我认为你最好使用Bouncy Castle或标准的JCE来处理所有这些事情。 PKCS7中的'S'表示标准,因此在C#中加密的数据应该在Java中解密,反之亦然。

#1


You don't have to worry about byte signedness because base64 encoded data never uses more than 6 bits in each byte (that's why it's called base 64, because you only use 64 characters which is 6 bits, to represent part of a data byte).

您不必担心字节签名,因为base64编码数据在每个字节中从不使用超过6位(这就是为什么它被称为base 64,因为您只使用64位字符,即6位,表示数据字节的一部分) 。

If your concern is the resulting data (3 data bytes for every 4 base64 characters), don't worry about that, either. An unsigned byte 255 in C# is the same as the signed byte -1 in Java.

如果你担心的是结果数据(每4个base64字符有3个数据字节),也不用担心。 C#中的无符号字节255与Java中的带符号字节-1相同。

To encode data, you can bitwise-and each byte with 0xff and store it in an int, then encode the least significant 8 bits. Or just bitwise-or each byte with 0x80 and store it in ant int and decode the least significant 8 bits.

要对数据进行编码,您可以按位和每个字节使用0xff并将其存储在int中,然后对最低有效8位进行编码。或者只是按位 - 或每个字节带有0x80并将其存储在ant int中并解码最低有效8位。

But I think you would be better off using Bouncy Castle or the standard JCE to deal with all that stuff. The 'S' in PKCS7 means Standard so data encrypted in C# should decrypt fine in Java and vice versa.

但我认为你最好使用Bouncy Castle或标准的JCE来处理所有这些事情。 PKCS7中的'S'表示标准,因此在C#中加密的数据应该在Java中解密,反之亦然。