Example usage for org.apache.commons.codec.binary Base64 decodeBase64

List of usage examples for org.apache.commons.codec.binary Base64 decodeBase64

Introduction

In this page you can find the example usage for org.apache.commons.codec.binary Base64 decodeBase64.

Prototype

public static byte[] decodeBase64(final byte[] base64Data) 

Source Link

Document

Decodes Base64 data into octets

Usage

From source file:Main.java

public static void main(String[] args) {
    String hello = "aaaaaaaa";

    byte[] decoded = Base64.decodeBase64(hello.getBytes());

    System.out.println(Arrays.toString(decoded));

    String decodedString = new String(decoded);
    System.out.println(hello + " = " + decodedString);
}

From source file:base64test.Base64Test.java

/**
 * @param args the command line arguments
 *//*from  www  .  j  a v a  2 s.  c om*/
public static void main(String[] args) {
    try {
        if (!Base64.isBase64(args[0])) {
            throw new Exception("Arg 1 is not a Base64 string!");
        } else {
            String decodedBase64String = new String(Base64.decodeBase64(args[0]));
            File tempFile = File.createTempFile("base64Test", ".tmp");
            tempFile.deleteOnExit();
            FileWriter fw = new FileWriter(tempFile, false);
            PrintWriter pw = new PrintWriter(fw);
            pw.write(decodedBase64String);
            pw.close();
            String fileType = getFileType(tempFile.toPath());
            System.out.println(fileType);
            System.in.read();
        }
    } catch (Exception e) {
        System.err.println(e.getMessage());
    }
}

From source file:com.doctor.base64.CommonsCodecBase64.java

public static void main(String[] args) {
    String plainText = "Base64??"
            + "??ASCII???"
            + "????Base64";

    // ??//from w w w  . ja v a  2 s.c o  m
    String base64String = Base64.encodeBase64String(plainText.getBytes(StandardCharsets.UTF_8));
    System.out.println(base64String);

    String plainTxt = new String(Base64.decodeBase64(base64String), StandardCharsets.UTF_8);
    Preconditions.checkArgument(plainTxt.equals(plainText));

    // ??
    base64String = new String(Base64.encodeBase64Chunked(plainText.getBytes(StandardCharsets.UTF_8)),
            StandardCharsets.UTF_8);
    plainTxt = new String(Base64.decodeBase64(base64String), StandardCharsets.UTF_8);
    Preconditions.checkArgument(plainTxt.equals(plainText));

}

From source file:sqsAlertInbound.java

public static void main(String[] args) throws Exception {

    // get credentials
    String user = "jreilly";
    AWSCredentials credentials = whgHelper.getCred(user);

    // use credentials to set access to SQS
    AmazonSQS sqs = whgHelper.setQueueAccess(credentials);

    // define queue that messages will be retrieved from
    String thisQueue = "alertInbound";
    String nextQueue = "alertPersist";

    while (1 > 0) {

        // pull list of current messages (up to 10) in the queue
        List<Message> messages = whgHelper.getMessagesFromQueue(thisQueue, sqs);
        System.out.println("Count of messages in " + thisQueue + ": " + messages.size());

        try {//from ww w  .j av  a 2  s.c om

            for (Message message : messages) {

                whgHelper.printMessage(message);
                for (Entry<String, String> entry : message.getAttributes().entrySet()) {
                    whgHelper.printMessageEntry(entry);
                }

                // validate JSON for completeness and form and handle errors
                //               if (sqs == null) {
                //                  sqs.sendMessage(new SendMessageRequest("alertErrorHandling", message.getBody()));
                //               }

                // call a function to transform message
                String alertJSON = String.valueOf(Base64.decodeBase64(message.getBody()));
                System.out.println("Transformed JSON: " + alertJSON);

                // send message to next queue
                System.out.println("Sending message to next queue.");
                sqs.sendMessage(new SendMessageRequest(nextQueue, alertJSON));

                // delete message from this queue
                System.out.println("Deleting message.\n");
                String messageRecieptHandle = message.getReceiptHandle();
                sqs.deleteMessage(new DeleteMessageRequest(thisQueue, messageRecieptHandle));

            }
            Thread.sleep(20000); // do nothing for 1000 miliseconds (1 second)

        } catch (AmazonServiceException ase) {
            whgHelper.errorMessagesAse(ase);
        } catch (AmazonClientException ace) {
            whgHelper.errorMessagesAce(ace);
        }
    }
}

From source file:com.sapienter.jbilling.tools.ConvertToBinHexa.java

/**
 * @param args//from   ww w. j a  v a  2 s .  c o  m
 */
public static void main(String[] args) {
    String driver = null;

    if (args.length < 3) {
        System.err.println("Usage: url username password [driver]");
        System.exit(1);
        return;
    }
    if (args.length < 4) {
        driver = "org.postgresql.Driver";
    } else {
        driver = args[3];
    }

    System.out.println("Converting credit cards ... ");
    int count = 0;
    try {
        connection = getConnection(args[0], args[1], args[2], driver);
        ResultSet rows = getCCRowsToUpdate();
        while (rows == null) { //rows.next() - skip CC
            int rowId = rows.getInt(1);
            String cryptedNumber = rows.getString(2);
            String cryptedName = rows.getString(3);
            int userId = rows.getInt(4);

            JBCrypto oldCrypt = JBCrypto.getCreditCardCrypto();
            oldCrypt.setUseHexForBinary(false);
            String plainNumber;
            try {
                plainNumber = oldCrypt.decrypt(cryptedNumber);
            } catch (Exception e) {
                plainNumber = "Not available";
                System.out.println("User id " + userId + " cc id " + rowId + " with bad cc number");
            }
            String plainName;
            try {
                plainName = oldCrypt.decrypt(cryptedName);
            } catch (RuntimeException e) {
                plainName = "Not available";
                System.out.println("User id " + userId + " cc id " + rowId + " with bad cc name");
            }
            // now recrypt using the new way
            JBCrypto newCrypt = JBCrypto.getCreditCardCrypto();
            newCrypt.setUseHexForBinary(true);
            cryptedName = newCrypt.encrypt(plainName);
            cryptedNumber = newCrypt.encrypt(plainNumber);
            //System.out.println("new " + cryptedName + " and " + cryptedNumber);
            updateCCRow(rowId, cryptedName, cryptedNumber);
            count++;
        }
        rows.close();

        System.out.println("Converting user passwords ... ");
        count = 0;
        rows = getUserRowsToUpdate();
        while (rows.next()) {
            int rowId = rows.getInt(1);
            String oldPassword = rows.getString(2);

            try {
                String newPassword = Util.binaryToString(Base64.decodeBase64(oldPassword.getBytes()));
                System.out.println("new " + newPassword + " old " + oldPassword);
                updateUserRow(rowId, newPassword);
                count++;
            } catch (Exception e) {
                System.out.println("Error with password " + oldPassword + " :" + e.getMessage());
            }
        }
        rows.close();

        connection.close();
    } catch (Exception e) {
        System.err.println("Error! " + e.getMessage());
        e.printStackTrace();
        System.exit(2);
    }
    System.out.println("Finished! " + count + " rows populated");

}

From source file:com.hadoopvietnam.commons.crypt.J2MECrypto.java

public static void main(String[] args) {
    J2MECrypto crypto = new J2MECrypto("LetfyActionCrypto".getBytes());
    String code = System.currentTimeMillis() + " " + "tk1cntt@gmail.com" + " " + System.currentTimeMillis();
    String encode = new String(Base64.encodeBase64(crypto.encrypt((code).getBytes())));
    System.out.println(encode);//from w w w  .  j  a v  a 2  s  .  co m
    String decode = new String(crypto.decrypt(Base64.decodeBase64(encode.getBytes())));
    System.out.println(decode);
    System.out.println(DigestUtils.md5Hex(code));
}

From source file:com.tcs.ebw.security.EBWSecurity.java

public static void main(String args[]) {

    try {/*  w  w w. j a va 2s.com*/

        EBWSecurity sec = new EBWSecurity();

        String testData2 = "Pramodh B Somashekara 231259 Channels !@#$%^&*()";

        if (args.length > 0)
            testData2 = (String) args[0];

        if (testData2 == null) {

            System.out.println("No Arguements in command line...Continuing with 'ELECTUSR'");

            testData2 = "ELECTUSR";

        }

        byte[] enc;

        System.out.println("----------------ENCRYPTION-------------");

        System.out.println("Original String:" + testData2);

        /** Set IMRS Key here **/

        sec.setSecretKey(sec.getSecretKey());

        testData2 = (new String(Base64.encodeBase64(sec.encryptSymmetric(testData2.getBytes()))));

        System.out.println("After Base64 Encode:" + testData2);

        testData2 = java.net.URLEncoder.encode(testData2, "utf-8");

        System.out.println("Encrypted[Base64+EncryptAlgo+URLEncoded][" + testData2 + "]");

        System.out.println("---------------DECRYPTION--------------");

        System.out.println("Encrypted[Base64+EncryptAlgo+URLEncoded]" + testData2);

        testData2 = java.net.URLDecoder.decode(testData2, "utf-8");

        testData2 = new String(sec.decryptSymmetric(
                Base64.decodeBase64(java.net.URLDecoder.decode(testData2, "utf-8").getBytes())));

        System.out.println("Decrypted[Base64+EncryptAlgo][" + testData2 + "]");

    } catch (NoSuchAlgorithmException e) {

        // TODO Auto-generated catch block

        e.printStackTrace();

    } catch (NoSuchPaddingException e) {

        // TODO Auto-generated catch block

        e.printStackTrace();

    } catch (BadPaddingException e) {

        // TODO Auto-generated catch block

        e.printStackTrace();

    } catch (InvalidKeyException e) {

        // TODO Auto-generated catch block

        e.printStackTrace();

    } catch (IllegalBlockSizeException e) {

        // TODO Auto-generated catch block

        e.printStackTrace();

    } catch (Exception e) {

        // TODO Auto-generated catch block

        e.printStackTrace();

    }

}

From source file:com.ddling.utils.MyBase64.java

public static String decodeStr(String str) {
    Base64 base64 = new Base64();
    byte[] debytes = base64.decodeBase64(new String(str).getBytes());
    return new String(debytes);
}

From source file:com.ddling.client.utils.MyBase64.java

/**
 * Base64?//from  ww  w . ja  v  a  2  s.co m
 * @param str ??
 * @return ??
 */
public static String decodeStr(String str) {
    Base64 base64 = new Base64();
    byte[] debytes = base64.decodeBase64(new String(str).getBytes());
    return new String(debytes);
}

From source file:Main.java

public static byte[] fromBase64String(String base64String) {
    return Base64.decodeBase64(base64String.getBytes());
}