Convert base64 encoded string to java byte array

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

usiTyri3 / gPJJ0F6Kj9qYL0w / zXiUAEcslUH6 / zVIjs =

and convert it to byte array in java to pass to SecretKeySpec as key. I know there is a problem with C # which has unsigned bytes and java only has bytes. How then can I pass this sting that has values ​​greater than 127 in it and accept java key and initialization vectors?

+1


a source to share


1 answer


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

If your problem is related to the received data (3 bytes of data for every 4 base64 characters) then don't worry about it. Unsigned byte 255 in C # is the same as byte -1 in Java.



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

But I think you'd be better off using Bouncy Castle or the standard JCE to get the hang of it all. The "S" in PKCS7 stands for Standard, so data encrypted in C # must be decrypted in Java and vice versa.

+2


a source







All Articles