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:com.philosophy.LicenseGen.java

public LicenseGen(String text) {
    license = text;// w  w  w.j ava  2 s.  c o m
    byte[] keyBytes = { 88, 42, 94, 52, 56, 81, 82, 98 };
    System.out.println("Original License : " + license);
    try {
        SecretKey key = new SecretKeySpec(keyBytes, "DES");
        Cipher cipher = Cipher.getInstance("DES");
        byte[] data = license.getBytes();
        cipher.init(Cipher.ENCRYPT_MODE, key);
        byte[] encrypted = cipher.doFinal(data);
        System.out.println("Encrypted license : " + new String(encrypted));
        //now convert to base64
        byte[] base64Enc = Base64.encodeBase64(encrypted);
        String encodedLicense = new String(base64Enc);
        System.out.println("Encoded license is : " + encodedLicense);
        System.out.println("Encoding string length is : " + encodedLicense.length());
    } catch (NoSuchAlgorithmException e) {
    } catch (NoSuchPaddingException e) {
    } catch (InvalidKeyException e) {
    } catch (IllegalStateException e) {
    } catch (IllegalBlockSizeException e) {
    } catch (BadPaddingException e) {
    }
}

From source file:com.philosophy.LicenseDecoder.java

public LicenseDecoder(String text) {
    license = text;//from  w ww . j  a v  a 2s  .c o  m
    byte[] keyBytes = { 88, 42, 94, 52, 56, 81, 82, 98 };
    System.out.println("Original License : " + license);
    try {
        SecretKey key = new SecretKeySpec(keyBytes, "DES");
        Cipher cipher = Cipher.getInstance("DES");
        byte[] decoded = Base64.decodeBase64(license.getBytes());
        cipher.init(Cipher.DECRYPT_MODE, key);
        byte[] decrypted = cipher.doFinal(decoded);
        System.out.println("Decrypted license : " + new String(decrypted));
    } catch (NoSuchAlgorithmException e) {
    } catch (NoSuchPaddingException e) {
    } catch (InvalidKeyException e) {
    } catch (IllegalStateException e) {
    } catch (IllegalBlockSizeException e) {
    } catch (BadPaddingException e) {
    }
}

From source file:info.fcrp.keepitsafe.util.Crypt.java

private static void init() {
    if (cryptCipher == null || decryptCipher == null) {
        try {//  w  w  w.ja  v  a 2s .  c o m
            SecretKeySpec keySpec = new SecretKeySpec(password.getBytes(), "AES");
            cryptCipher = Cipher.getInstance("AES");
            cryptCipher.init(Cipher.ENCRYPT_MODE, keySpec);

            decryptCipher = Cipher.getInstance("AES");
            decryptCipher.init(Cipher.DECRYPT_MODE, keySpec);

        } catch (NoSuchAlgorithmException e) {
            throw new RuntimeException(e);
        } catch (NoSuchPaddingException e) {
            throw new RuntimeException(e);
        }
        // output = cipher.doFinal(input)
        //
        // Cipher c = Cipher.getInstance("AES");
        // String plain = "plain";
        // byte[] plainBytes = plain.getBytes();
        //
        // c.init(Cipher.ENCRYPT_MODE, k);
        // c.update(plainBytes);
        //
        // byte[] encBytes = c.doFinal();
        // String enc = Base64.encodeBase64String(encBytes);
        // assertNotSame(plain, enc);
        //
        // c.init(Cipher.DECRYPT_MODE, k);
        // c.update(encBytes);
        // byte[] decBytes = c.doFinal();
        // String dec = new String(decBytes);
        catch (InvalidKeyException e) {
            throw new RuntimeException(e);
        }
    }
}

From source file:uploadProcess.java

public static boolean encrypt(String path, FileItemStream item, String patientID) {
    try {/*from   ww  w  .  j  av a2s .c  o m*/
        File mainFolder = new File(path + File.separator + "Encrypt");
        if (!mainFolder.exists()) {
            mainFolder.mkdir();
        }
        File patientFolder = new File(mainFolder + File.separator + patientID);
        if (!patientFolder.exists()) {
            patientFolder.mkdir();
        }
        String ukey = GetKey.getPatientKey(patientID);
        InputStream fis = item.openStream();

        FileOutputStream fos = new FileOutputStream(
                patientFolder.getAbsolutePath() + File.separator + item.getName());
        byte[] k = ukey.getBytes();
        SecretKeySpec key = new SecretKeySpec(k, "AES");
        System.out.println(key);
        Cipher enc = Cipher.getInstance("AES");
        enc.init(Cipher.ENCRYPT_MODE, key);
        CipherOutputStream cos = new CipherOutputStream(fos, enc);
        byte[] buf = new byte[1024];
        int read;
        while ((read = fis.read(buf)) != -1) {
            cos.write(buf, 0, read);
        }
        fis.close();
        fos.flush();
        cos.close();

        //Upload File to cloud
        DropboxUpload upload = new DropboxUpload();
        upload.uploadFile(patientFolder, item.getName(), StoragePath.getDropboxDir() + patientID);
        DeleteDirectory.delete(patientFolder);

        return true;
    } catch (Exception e) {
        System.out.println("Error: " + e);
    }
    return false;
}

From source file:model.Encryption.java

public void setSecret(String key) {
    this.secret = new SecretKeySpec(key.getBytes(), "AES");
}

From source file:com.myapp.common.AES4MEncrypt.java

/**
 * /*from ww w.  j  ava2  s  .  c  o m*/
 * 
 * @param sSrc 
 * @param sKey KEY
 * @return
 * @throws Exception
 * @author cdduqiang
 * @date 201443
 */
public static String encrypt(String sSrc, String sKey) throws Exception {
    if (sKey == null) {
        log.error("Encrypt Key ??");
        throw new Exception("Encrypt Key ??");
    }

    byte[] raw = hex2byte(sKey);
    SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
    Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
    IvParameterSpec iv = new IvParameterSpec("0102030405060708".getBytes());
    cipher.init(Cipher.ENCRYPT_MODE, skeySpec, iv);
    byte[] anslBytes = sSrc.getBytes(ENCODING);// string2AnslBytes(sSrc);
    byte[] encrypted = cipher.doFinal(anslBytes);
    return byte2hex(encrypted).toUpperCase();
}

From source file:com.miyue.util.Cryptos.java

/**
 * HMAC-SHA1???, ,20.// w  ww  .j  a v a2 s.c  om
 * 
 * @param input 
 * @param key HMAC-SHA1
 */
public static byte[] hmacSha1(byte[] input, byte[] key) {
    try {
        SecretKey secretKey = new SecretKeySpec(key, HMACSHA1);
        Mac mac = Mac.getInstance(HMACSHA1);
        mac.init(secretKey);
        return mac.doFinal(input);
    } catch (GeneralSecurityException e) {
        throw Exceptions.unchecked(e);
    }
}

From source file:de.desy.dcache.s3.Signature.java

/**
* Computes RFC 2104-compliant HMAC signature.
* * @param data//from  ww w.j a  v  a2s  .c om
* The data to be signed.
* @param key
* The signing key.
* @return
* The Base64-encoded RFC 2104-compliant HMAC signature.
* @throws
* java.security.SignatureException when signature generation fails
*/
public static String calculateRFC2104HMAC(String data, String key) throws java.security.SignatureException {
    String result;
    try {
        // get an hmac_sha1 key from the raw key bytes
        SecretKeySpec signingKey = new SecretKeySpec(key.getBytes(), HMAC_SHA1_ALGORITHM);

        // get an hmac_sha1 Mac instance and initialize with the signing key
        Mac mac = Mac.getInstance(HMAC_SHA1_ALGORITHM);
        mac.init(signingKey);

        // compute the hmac on input data bytes
        byte[] rawHmac = mac.doFinal(data.getBytes());

        // base64-encode the hmac
        result = new String(Base64.encodeBase64(rawHmac));

    } catch (Exception e) {
        throw new SignatureException("Failed to generate HMAC: " + e.getMessage());
    }
    return result;
}

From source file:com.aast.encrypt.EncryptManager.java

public static String encryptAES(String key, String value) {
    try {/*www  .j  a  v  a 2s  .c om*/
        byte[] keyBytes = getKeyFromString(key);
        IvParameterSpec iv = new IvParameterSpec(keyBytes);
        SecretKeySpec skeySpec = new SecretKeySpec(keyBytes, "AES");

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

        byte[] encrypted = cipher.doFinal(value.getBytes());
        //            System.out.println("encrypted string: "
        //                    + Base64.encodeBase64String(encrypted));

        return Base64.encodeBase64String(encrypted);
    } catch (Exception ex) {
        ex.printStackTrace();
    }

    return null;
}

From source file:io.personium.core.model.file.DataCryptor.java

/**
 * Set secret key.//from  w w  w.j ava 2  s.co  m
 * @param keyString key string
 */
public static void setKeyString(String keyString) {
    aesKey = new SecretKeySpec(keyString.getBytes(), "AES");
}