Example usage for org.apache.commons.codec.binary Hex decodeHex

List of usage examples for org.apache.commons.codec.binary Hex decodeHex

Introduction

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

Prototype

public static byte[] decodeHex(char[] data) throws IllegalArgumentException 

Source Link

Document

Converts an array of characters representing hexadecimal values into an array of bytes of those same values.

Usage

From source file:com.mfizz.ruby.demo.UnmarshalMain.java

static public void main(String[] args) throws Exception {
    // use following code in ruby to test encoding stuff
    // Marshal.dump(counter).unpack('H*')

    Unmarshaller parser = new Unmarshaller();

    // simple integer in ruby
    // counter = 1
    byte[] itemBytes = Hex.decodeHex("04086906".toCharArray());

    RubyType obj = Marshal.load(itemBytes);
    logger.info("unmarshalled: {}", Ruby.toString(obj));
}

From source file:com.tc.simple.apn.quicktests.Test.java

/**
 * @param args/*from w w  w.ja v  a  2s. c  o  m*/
 */

public static void main(String[] args) {
    SSLSocket socket = null;

    try {
        String host = "gateway.sandbox.push.apple.com";
        int port = 2195;

        String token = "de7f197546e41a76684f8e2d89f397ed165298d7772f4bd9b0f39c674b185b0f";
        System.out.println(token.toCharArray().length);

        //String token = "8cebc7c08f79fa62f0994eb4298387ff930857ff8d14a50de431559cf476b223";

        KeyStore keyStore = KeyStore.getInstance("PKCS12");

        keyStore.load(Test.class.getResourceAsStream("egram-dev-apn.p12"), "xxxxxxxxx".toCharArray());
        KeyManagerFactory keyMgrFactory = KeyManagerFactory
                .getInstance(KeyManagerFactory.getDefaultAlgorithm());
        keyMgrFactory.init(keyStore, "xxxxxxxxx".toCharArray());

        SSLContext sslContext = SSLContext.getInstance("TLS");
        sslContext.init(keyMgrFactory.getKeyManagers(), null, null);
        SSLSocketFactory socketFactory = sslContext.getSocketFactory();

        socket = (SSLSocket) socketFactory.createSocket(host, port);
        String[] cipherSuites = socket.getSupportedCipherSuites();
        socket.setEnabledCipherSuites(cipherSuites);
        socket.startHandshake();

        char[] t = token.toCharArray();
        byte[] b = Hex.decodeHex(t);

        OutputStream outputstream = socket.getOutputStream();

        String payload = "{\"aps\":{\"alert\":\"yabadabadooo\"}}";

        int expiry = (int) ((System.currentTimeMillis() / 1000L) + 7200);

        ByteArrayOutputStream bout = new ByteArrayOutputStream();

        DataOutputStream dos = new DataOutputStream(bout);

        //command
        dos.writeByte(1);

        //id
        dos.writeInt(900);

        //expiry
        dos.writeInt(expiry);

        //token length.
        dos.writeShort(b.length);

        //token
        dos.write(b);

        //payload length
        dos.writeShort(payload.length());

        //payload.
        dos.write(payload.getBytes());

        byte[] byteMe = bout.toByteArray();

        socket.getOutputStream().write(byteMe);

        socket.setSoTimeout(900);
        InputStream in = socket.getInputStream();

        System.out.println(APNErrors.getError(in.read()));

        in.close();

        outputstream.close();

    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        try {
            socket.close();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

}

From source file:gov.nih.nci.cadsr.cadsrpasswordchange.core.OracleObfuscation.java

public static void main(String[] args) throws Exception {
    OracleObfuscation x = new OracleObfuscation("$_12345&"); // just a
    // random// w  ww .j  ava  2  s. c  o m
    // pick for
    // testing
    String text = CommonUtil.pad("BB", Constants.MAX_ANSWER_LENGTH); //args[0];

    byte[] encrypted = x.encrypt(text.getBytes());
    String encoded = new String(Hex.encodeHex(encrypted));
    System.out.println("Encrypted/Encoded: \"" + encoded + "\"");

    byte[] decoded = Hex.decodeHex(encoded.toCharArray());
    String decrypted = new String(x.decrypt(decoded));
    //      String decrypted = new String(x.decrypt(encrypted));
    System.out.println("Decoded/Decrypted: \"" + decrypted + "\"");
}

From source file:com.lhings.java.utils.ByteMan.java

public static void main(String[] args) throws DecoderException {
    byte[] a = { (byte) 0x12, (byte) 0xfa, (byte) 0xa4, (byte) 0xc5 };
    for (byte b : a)
        System.out.print(b + ", ");
    System.out.println();//from  w ww.j a v  a  2s  .c  o m
    String str = Hex.encodeHexString(a);
    System.out.println(str);
    byte[] c = Hex.decodeHex(str.toCharArray());
    for (byte b : c)
        System.out.print(b + ", ");
    System.out.println();
}

From source file:Main.java

public static void testChecksum(String inputString, String expectedSum) throws Exception {
    byte[] sample = Hex.decodeHex(inputString.toCharArray());
    byte[] sampleCheckSum = Hex.decodeHex(expectedSum.toCharArray());
    char checksum = checkSum(sample);
    byte[] result = { (byte) (checksum & 0xFF), (byte) ((checksum >> 8) & 0xFF) };
    System.out.println("Calculated checksum: " + byteArrayToString(result) + " vs expected: " + expectedSum);

    // Confirmation
    char checksumComp = (char) (checksum ^ 0xFFFF);
    byte verifySample[] = Arrays.copyOf(sample, sample.length + 2);
    verifySample[verifySample.length - 4] = (byte) (checksumComp & 0xFF);
    verifySample[verifySample.length - 3] = (byte) (checksumComp >> 8);
    char verifier = checkSum(verifySample);
    byte[] verifierResult = { (byte) (verifier & 0xFF), (byte) ((verifier >> 8) & 0xFF) };
    System.out.println("Verifier = " + byteArrayToString(verifierResult));
}

From source file:Main.java

public static byte[] decodeHexString(String arg) throws DecoderException {
    return Hex.decodeHex(arg.toCharArray());
}

From source file:com.bjwg.back.util.EncodeUtils.java

public static byte[] decodeHex(String input) {
    try {//from  ww w .j av a 2 s . c o m
        return Hex.decodeHex(input.toCharArray());
    } catch (DecoderException e) {
        throw new IllegalStateException("Hex Decoder exception", e);
    }
}

From source file:cn.newtouch.util.utils.encode.EncodeUtils.java

/**
 * Hex?.//from  www  .j a v  a  2 s  .co m
 */
public static byte[] hexDecode(String input) {
    try {
        return Hex.decodeHex(input.toCharArray());
    } catch (DecoderException e) {
        throw new IllegalStateException("Hex Decoder exception", e);
    }
}

From source file:com.peterchen.core.utils.EncodeUtils.java

/**
 * Hex?./*from  ww w.j  a va 2s  .  c om*/
 */
public static byte[] decodeHex(String input) {
    try {
        return Hex.decodeHex(input.toCharArray());
    } catch (DecoderException e) {
        throw new IllegalArgumentException("Unsupported Encoding Exception", e);
    }
}

From source file:com.apabi.qrcode.utils.EncodeUtils.java

/**
 * Hex?./*w  w  w.ja  v a 2s .  com*/
 */
public static byte[] hexDecode(final String input) {
    try {
        return Hex.decodeHex(input.toCharArray());
    } catch (DecoderException e) {
        throw new IllegalStateException("Hex Decoder exception", e);
    }
}