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!

Maksym Gontar
  • 22,765
  • 10
  • 78
  • 114

2 Answers2

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.

John Feminella
  • 303,634
  • 46
  • 339
  • 357
  • thanks, I have tried both decode and encode methods but still it's not unsigned 8bit data given in result... – Maksym Gontar Feb 16 '10 at 18:10
  • I don't think that's right. The output of decodeBase64 is `byte[]`. – John Feminella Feb 16 '10 at 19:00
  • accepted, I really had to use decode 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. – Maksym Gontar Feb 17 '10 at 09:02
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);
    }
}
David Buck
  • 3,752
  • 35
  • 31
  • 35
  • With Informatica the commons.codec.binary.Base64 library was not available. The solution here worked for me. – ADH Sep 09 '21 at 15:39