4

Is there some Java equivalent of Convert.FromBase64String which

Converts the specified string, which encodes binary data as base-64 digits, to an equivalent 8-bit unsigned integer array.

I have tried:

Will be grateful for any suggestions!

3
  • 1
    I refuse to give the link to apache commons-codec more than once a day.. Commented Feb 16, 2010 at 17:36
  • java doesn't have unsigned types btw Commented Feb 16, 2010 at 18:23
  • correct, but I need to have same values as it would be in c# or whathever (means int 255 instead of byte -1) Commented Feb 16, 2010 at 22:29

2 Answers 2

6

In general, chances are good that if the standard Java libraries don't have something you need, the Apache Commons framework does. In this case, you want the decodeBase64 method from commons.codec.binary.Base64.

Sign up to request clarification or add additional context in comments.

3 Comments

thanks, I have tried both decode and encode methods but still it's not unsigned 8bit data given in result...
I don't think that's right. The output of decodeBase64 is byte[].
accepted, I really had to use <i>decode</i> method instead of encode (confused by msdn description...) and add &0xff mask to have unsigned value equivalent. In such case values in c# and java are exactly same.
1

The key and the initial vector are converted using Convert.FromBase64String inAES encryption in C#.

In java, you can use DatatypeConverter.parseBase64Binary(string) method. In order to use it import import javax.xml.bind.*;

Here is the example program

import java.util.Base64;

import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import javax.xml.bind.*;

class AESEncryption {

    private static final String key = "type your key here.(base64)";
    private static final String initVector = "type your initial vector here.(base64)";

    static byte[] encodedKey = DatatypeConverter.parseBase64Binary(key);
    static byte[] encodedIV = DatatypeConverter.parseBase64Binary(initVector);

    public static String encrypt(final String value) {
        try {
            IvParameterSpec iv = new IvParameterSpec(encodedIV);
            SecretKeySpec skeySpec = new SecretKeySpec(encodedKey, "AES");

            Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5PADDING");
            cipher.init(Cipher.ENCRYPT_MODE, skeySpec, iv);

            byte[] encrypted = cipher.doFinal(value.getBytes());

            return Base64.getEncoder().encodeToString(encrypted);

        } catch (final Exception ex) {
            ex.printStackTrace();
        }
        return null;
    }

    public static String decrypt(final String encrypted) {
        try {
            IvParameterSpec iv = new IvParameterSpec(encodedIV);
            SecretKeySpec skeySpec = new SecretKeySpec(encodedKey, "AES");

            Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5PADDING");
            cipher.init(Cipher.DECRYPT_MODE, skeySpec, iv);

            byte[] original = cipher.doFinal(Base64.getDecoder().decode(encrypted));

            return new String(original);

        } catch (final Exception ex) {
            ex.printStackTrace();
        }

        return null;
    }

    public static void main(final String[] args) {

        String originalString = "Here is your text to be encrypted.";
        System.out.println("Original String to encrypt - " + originalString);

        String encryptedString = encrypt(originalString);
        System.out.println("Encrypted String - " + encryptedString);

        String decryptedString = decrypt(encryptedString);
        System.out.println("After decryption - " + decryptedString);
    }
}

1 Comment

With Informatica the commons.codec.binary.Base64 library was not available. The solution here worked for me.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.