Example usage for javax.crypto.spec SecretKeySpec SecretKeySpec

List of usage examples for javax.crypto.spec SecretKeySpec SecretKeySpec

Introduction

In this page you can find the example usage for javax.crypto.spec SecretKeySpec SecretKeySpec.

Prototype

public SecretKeySpec(byte[] key, String algorithm) 

Source Link

Document

Constructs a secret key from the given byte array.

Usage

From source file:GetMultipleLeads.java

public static void main(String[] args) {
    System.out.println("Executing GetMultipleLeads");
    try {//w w  w.j av  a2s  . c  o  m
        URL marketoSoapEndPoint = new URL("CHANGE ME" + "?WSDL");
        String marketoUserId = "CHANGE ME";
        String marketoSecretKey = "CHANGE ME";

        QName serviceName = new QName("http://www.marketo.com/mktows/", "MktMktowsApiService");
        MktMktowsApiService service = new MktMktowsApiService(marketoSoapEndPoint, serviceName);
        MktowsPort port = service.getMktowsApiSoapPort();

        // Create Signature
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ");
        String text = df.format(new Date());
        String requestTimestamp = text.substring(0, 22) + ":" + text.substring(22);
        String encryptString = requestTimestamp + marketoUserId;

        SecretKeySpec secretKey = new SecretKeySpec(marketoSecretKey.getBytes(), "HmacSHA1");
        Mac mac = Mac.getInstance("HmacSHA1");
        mac.init(secretKey);
        byte[] rawHmac = mac.doFinal(encryptString.getBytes());
        char[] hexChars = Hex.encodeHex(rawHmac);
        String signature = new String(hexChars);

        // Set Authentication Header
        AuthenticationHeader header = new AuthenticationHeader();
        header.setMktowsUserId(marketoUserId);
        header.setRequestTimestamp(requestTimestamp);
        header.setRequestSignature(signature);

        // Create Request
        ParamsGetMultipleLeads request = new ParamsGetMultipleLeads();

        // Request Using LeadKey Selector
        ////////////////////////////////////////////////////////
        LeadKeySelector keySelector = new LeadKeySelector();
        keySelector.setKeyType(LeadKeyRef.EMAIL);

        ArrayOfString aos = new ArrayOfString();
        aos.getStringItems().add("formtest1@marketo.com");
        aos.getStringItems().add("joe@marketo.com");
        keySelector.setKeyValues(aos);
        request.setLeadSelector(keySelector);

        /*
        // Request Using LastUpdateAtSelector
        ////////////////////////////////////////////////////////
        LastUpdateAtSelector leadSelector = new LastUpdateAtSelector();
                 
        GregorianCalendar gc = new GregorianCalendar();
        gc.setTimeInMillis(new Date().getTime());
        gc.add( GregorianCalendar.DAY_OF_YEAR, -2);
                 
        DatatypeFactory factory = DatatypeFactory.newInstance();
                
        ObjectFactory objectFactory = new ObjectFactory();
        JAXBElement<XMLGregorianCalendar> until =objectFactory.createLastUpdateAtSelectorLatestUpdatedAt(factory.newXMLGregorianCalendar(gc));            
                 
        GregorianCalendar since = new GregorianCalendar();
        since.setTimeInMillis(new Date().getTime());
        since.add( GregorianCalendar.DAY_OF_YEAR, -5);
                 
        leadSelector.setOldestUpdatedAt(factory.newXMLGregorianCalendar(since));
        leadSelector.setLatestUpdatedAt(until);     
                 
        request.setLeadSelector(leadSelector);
        */

        /*
        // Request Using StaticList Selector
        ////////////////////////////////////////////////////////
        StaticListSelector staticListSelector = new StaticListSelector();
                
        //staticListSelector.setStaticListId(value)
        ObjectFactory objectFactory = new ObjectFactory();
        JAXBElement<String> listName = objectFactory.createStaticListSelectorStaticListName("SMSProgram.listForTesting");
        staticListSelector.setStaticListName(listName);
                 
        // JAXBElement<Integer> listId = objectFactory.createStaticListSelectorStaticListId(6926);
        // staticListSelector.setStaticListId(listId);
                 
        request.setLeadSelector(staticListSelector);
        */

        ArrayOfString attributes = new ArrayOfString();
        attributes.getStringItems().add("FirstName");
        attributes.getStringItems().add("AnonymousIP");
        attributes.getStringItems().add("Company");

        request.setIncludeAttributes(attributes);

        JAXBElement<Integer> batchSize = new ObjectFactory().createParamsGetMultipleLeadsBatchSize(10);
        request.setBatchSize(batchSize);

        SuccessGetMultipleLeads result = port.getMultipleLeads(request, header);

        JAXBContext context = JAXBContext.newInstance(SuccessGetLead.class);
        Marshaller m = context.createMarshaller();
        m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
        m.marshal(result, System.out);

    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:GetChannels.java

public static void main(String[] args) {
    System.out.println("Executing Get Channels");
    try {/*from   w w w.  j  a  v a2  s  . c om*/
        URL marketoSoapEndPoint = new URL("CHANGEME" + "?WSDL");
        String marketoUserId = "CHANGEME";
        String marketoSecretKey = "CHANGEME";

        QName serviceName = new QName("http://www.marketo.com/mktows/", "MktMktowsApiService");
        MktMktowsApiService service = new MktMktowsApiService(marketoSoapEndPoint, serviceName);

        // Create Signature
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ssZ");
        String text = df.format(new Date());
        String requestTimestamp = text.substring(0, 22) + ":" + text.substring(22);
        String encryptString = requestTimestamp + marketoUserId;

        SecretKeySpec secretKey = new SecretKeySpec(marketoSecretKey.getBytes(), "HmacSHA1");
        Mac mac = Mac.getInstance("HmacSHA1");
        mac.init(secretKey);
        byte[] rawHmac = mac.doFinal(encryptString.getBytes());
        char[] hexChars = Hex.encodeHex(rawHmac);
        String signature = new String(hexChars);

        // Set Authentication Header
        AuthenticationHeader header = new AuthenticationHeader();
        header.setMktowsUserId(marketoUserId);
        header.setRequestTimestamp(requestTimestamp);
        header.setRequestSignature(signature);

        // Create Request
        ParamsGetChannels params = new ParamsGetChannels();
        Tag tags = new Tag();
        ArrayOfString tagArray = new ArrayOfString();
        tagArray.getStringItems().add("Webinar");
        tagArray.getStringItems().add("Blog");
        tagArray.getStringItems().add("Tradeshow");
        tags.setValues(tagArray);

        MktowsPort port = service.getMktowsApiSoapPort();
        SuccessGetChannels result = port.getChannels(params, header);

        JAXBContext context = JAXBContext.newInstance(SuccessGetLead.class);
        Marshaller m = context.createMarshaller();
        m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
        m.marshal(result, System.out);
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:de.serverfrog.pw.crypt.SerpentUtil.java

/**
 * Print all Security providers and their algos
 *
 * @param args args//from   www.  j  a  v a 2  s. c om
 * @throws Exception Exception
 */
public static void main(String[] args) throws Exception {
    ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
    SecretKeySpec keySpec = new SecretKeySpec("test".getBytes("UTF-8"), "AES");
    try (CipherOutputStream outputStream = SerpentUtil.getOutputStream(byteArrayOutputStream, keySpec)) {
        IOUtils.write("TEST", outputStream);
    }
    System.out.println(Arrays.toString(byteArrayOutputStream.toByteArray()));
    System.out.println(byteArrayOutputStream.toString("UTF-8"));
    byte[] toByteArray = byteArrayOutputStream.toByteArray();
    ByteArrayInputStream arrayInputStream = new ByteArrayInputStream(toByteArray);
    CipherInputStream inputStream = SerpentUtil.getInputStream(arrayInputStream,
            new SecretKeySpec("test".getBytes("UTF-8"), "AES"));
    byte[] bytes = new byte[1048576];

    IOUtils.read(inputStream, bytes);

    System.out.println(new String(bytes, "UTF-8").trim());

    //
    //        for (Provider provider : Security.getProviders()) {
    //            System.out.println("");
    //            System.out.println("");
    //            System.out.println("");
    //            System.out.println("-------------------------------");
    //            System.out.println("Name: " + provider.getName());
    //            System.out.println("Info: " + provider.getInfo());
    //            for (Map.Entry<Object, Object> entry : provider.entrySet()) {
    //                System.out.println("Key: Class:" + entry.getKey().getClass() + " String: " + entry.getKey());
    //                System.out.println("Value: Class:" + entry.getValue().getClass() + " String: " + entry.getValue());
    //            }
    //            for (Provider.Service service : provider.getServices()) {
    //                System.out.println("Service: Algorithm:" + service.getAlgorithm()
    //                        + " ClassName:" + service.getClassName()
    //                        + " Type:" + service.getType() + " toString:" + service.toString());
    //            }
    //            for (Object object : provider.values()) {
    //                System.out.println("Value: " + object.getClass() + " toString:" + object.toString());
    //
    //            }
    //            System.out.println("-------------------------------");
    //        }
}

From source file:Main.java

public static byte[] encrypt(byte[] data, byte[] key) throws Exception {
    SecretKey secretKey = new SecretKeySpec(key, "DESede");
    Cipher cipher = Cipher.getInstance("DESede");
    cipher.init(Cipher.ENCRYPT_MODE, secretKey);
    return cipher.doFinal(data);
}

From source file:Main.java

public static byte[] decrypt(byte[] data, byte[] key) throws Exception {
    SecretKey secretKey = new SecretKeySpec(key, "DESede");
    Cipher cipher = Cipher.getInstance("DESede");
    cipher.init(Cipher.DECRYPT_MODE, secretKey);
    return cipher.doFinal(data);
}

From source file:Main.java

public static byte[] decrypt(byte[] data, byte[] key) throws Exception {
    SecretKey secretKey = new SecretKeySpec(key, "DESede");

    Cipher cipher = Cipher.getInstance("DESede/ECB/PKCS5Padding");
    cipher.init(Cipher.DECRYPT_MODE, secretKey);
    byte[] plainBytes = cipher.doFinal(data);
    return plainBytes;
}

From source file:Main.java

public static byte[] decrypt(byte[] data, byte[] key) throws Exception {
    SecretKey secretKey = new SecretKeySpec(key, "AES");

    Cipher cipher = Cipher.getInstance("AES");
    cipher.init(Cipher.DECRYPT_MODE, secretKey);
    byte[] plainBytes = cipher.doFinal(data);
    return plainBytes;
}

From source file:Main.java

public static byte[] encrypt(byte[] data, byte[] key) throws Exception {
    SecretKey secretKey = new SecretKeySpec(key, "AES");
    Cipher cipher = Cipher.getInstance("AES");
    cipher.init(Cipher.ENCRYPT_MODE, secretKey);
    return cipher.doFinal(data);
}

From source file:Main.java

public static String decryptData(String encryptBase64Data, String keyBase64) {
    SecretKeySpec key = new SecretKeySpec(Base64.decode(keyBase64, Base64.DEFAULT), "DES");
    Cipher decipher = null;//  w w  w.  ja v a 2 s.c  om
    try {
        decipher = Cipher.getInstance("DES");
        decipher.init(Cipher.DECRYPT_MODE, key);
        byte[] encryptData = Base64.decode(encryptBase64Data, Base64.DEFAULT);
        return new String(decipher.doFinal(encryptData), "UTF8");
    } catch (Exception e) {
    }
    return null;
}

From source file:Main.java

private static byte[] encrypt(byte[] key, byte[] clear) throws Exception {
    SecretKeySpec skeySpec = new SecretKeySpec(key, "AES");
    Cipher cipher = Cipher.getInstance("AES");
    cipher.init(Cipher.ENCRYPT_MODE, skeySpec, new IvParameterSpec(new byte[cipher.getBlockSize()]));
    return cipher.doFinal(clear);
}