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:Logi.GSeries.Libraries.Encryption.java

public static String decrypt(String encryptedString, String password) {
    try {// w ww  .  ja  v  a2  s  .  com
        byte[] encryptedWithIV = Base64.decodeBase64(encryptedString);
        byte initialVector[] = new byte[16];
        byte[] encrypted = new byte[encryptedWithIV.length - initialVector.length];
        System.arraycopy(encryptedWithIV, 0, encrypted, 0, encrypted.length);
        System.arraycopy(encryptedWithIV, encrypted.length, initialVector, 0, initialVector.length);
        IvParameterSpec ivspec = new IvParameterSpec(initialVector);
        SecretKeySpec skeySpec = new SecretKeySpec(password.getBytes("UTF-8"), "AES");
        Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5PADDING");
        cipher.init(Cipher.DECRYPT_MODE, skeySpec, ivspec);
        byte[] original = cipher.doFinal(encrypted);
        return new String(original);
    } catch (Exception ex) {
        Logger.getLogger(Encryption.class.getName()).log(Level.SEVERE, null, ex);
        return "Error";
    }
}

From source file:com.ec2box.manage.util.EncryptionUtil.java

/**
 * return encrypted value of string//from  www  . ja v  a2  s  . co m
 *
 * @param str unencrypted string
 * @return encrypted string
 */
public static String encrypt(String str) {

    String retVal = null;
    if (str != null && str.length() > 0) {
        try {
            Cipher c = Cipher.getInstance("AES");
            c.init(Cipher.ENCRYPT_MODE, new SecretKeySpec(key, "AES"));
            byte[] encVal = c.doFinal(str.getBytes());
            retVal = new String(Base64.encodeBase64(encVal));
        } catch (Exception ex) {
            log.error(ex.toString(), ex);
        }

    }
    return retVal;
}

From source file:Conexion.newClass.java

public String Decode(String textoEncriptado) throws Exception {

    String secretKey = "mailEncrypted"; //llave para encriptar datos
    String base64EncryptedString = "";

    try {/*from   w  w w  .ja va2s . c o  m*/
        byte[] message = Base64.decodeBase64(textoEncriptado.getBytes("utf-8"));
        MessageDigest md = MessageDigest.getInstance("MD5");
        byte[] digestOfPassword = md.digest(secretKey.getBytes("utf-8"));
        byte[] keyBytes = Arrays.copyOf(digestOfPassword, 24);
        SecretKey key = new SecretKeySpec(keyBytes, "DESede");

        Cipher decipher = Cipher.getInstance("DESede");
        decipher.init(Cipher.DECRYPT_MODE, key);

        byte[] plainText = decipher.doFinal(message);

        base64EncryptedString = new String(plainText, "UTF-8");

    } catch (Exception ex) {
    }
    return base64EncryptedString;
}

From source file:com.quantil.http.HttpProcessor.java

private String createKey() throws Exception {

    SimpleDateFormat formatter;/*from w  w  w. j  a  v a2s .  co  m*/

    formatter = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss z");

    currentDate = formatter.format(new Date());

    SecretKeySpec signingKey = new SecretKeySpec(pass.getBytes(), "HmacSHA1");

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

    // compute the hmac on input data bytes
    byte[] rawHmac = mac.doFinal(currentDate.getBytes());
    Base64 b64 = new Base64();
    String pas = user + ":" + new String(b64.encode(rawHmac), "UTF-8");

    return new String(b64.encode(pas.getBytes()), "UTF-8");
}

From source file:net.duckling.ddl.web.agent.util.AuthUtil.java

private static String decodeAuth(String auth) throws NoSuchAlgorithmException, NoSuchPaddingException,
        InvalidKeyException, IllegalBlockSizeException, BadPaddingException, UnsupportedEncodingException {
    SecretKeySpec spec = new SecretKeySpec(getKey(), "AES");
    Cipher cipher = Cipher.getInstance("AES");
    cipher.init(Cipher.DECRYPT_MODE, spec);
    byte[] result = cipher.doFinal(Base64.decodeBase64(auth));
    return new String(result, "UTF-8");
}

From source file:com.lehman.ic9.common.base64.java

/**
 * Computes RFC 2104-compliant HMAC signature.
 * @param data Is a String with the data to encode.
 * @param key Is a String with the key.//from  w  w w.j a v a 2s .c o  m
 * @return A string with the encoded signature.
 * @throws SignatureException Exception
 */
public static String encodeHmac(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.blackcrowsys.sinscrypto.AesKeyGenerator.java

@Override
public SecretKey generateSecretKey(String password, String salt)
        throws NoSuchAlgorithmException, InvalidKeySpecException, DecoderException {
    SecretKeyFactory factory = SecretKeyFactory.getInstance(ALGORITHM);
    KeySpec spec = new PBEKeySpec(password.toCharArray(), Hex.decodeHex(salt.toCharArray()), ITERATION,
            KEYLENGTH);//from  ww w.j a  v a 2s .  c  o  m
    SecretKey key = factory.generateSecret(spec);
    return new SecretKeySpec(key.getEncoded(), ENCRYPTION);
}

From source file:com.jwm123.loggly.reporter.TripleDesCipher.java

public TripleDesCipher(String keyPath, AppDirectory appDir) throws NoSuchAlgorithmException,
        NoSuchProviderException, NoSuchPaddingException, InvalidKeyException, IOException {
    this.appDir = appDir;
    if (key == null) {
        this.keyPath = keyPath;
        getKey();//from   w w w.  j a  v a  2s .co  m
    }
    SecretKey keySpec = new SecretKeySpec(key, ALGORITHM);
    encrypter = Cipher.getInstance(TRIPLE_DES_TRANSFORMATION);
    encrypter.init(Cipher.ENCRYPT_MODE, keySpec);
    decrypter = Cipher.getInstance(TRIPLE_DES_TRANSFORMATION);
    decrypter.init(Cipher.DECRYPT_MODE, keySpec);
}

From source file:de.openflorian.crypt.provider.BlowfishCipher.java

@Override
public String decrypt(String str) throws GeneralSecurityException {
    if (key == null || key.isEmpty())
        throw new IllegalStateException("The key is not set or is length=0.");

    if (str == null)
        return null;

    try {//w  w w.j a v a2  s. c o  m
        SecretKeySpec keySpec;
        keySpec = new SecretKeySpec(key.getBytes("UTF8"), "Blowfish");

        Cipher cipher = Cipher.getInstance("Blowfish/ECB/PKCS5Padding");
        cipher.init(Cipher.DECRYPT_MODE, keySpec);

        return new String(cipher.doFinal(Base64.decodeBase64(str.getBytes("UTF8"))), "UTF8");
    } catch (Exception e) {
        log.error(e.getMessage(), e);
        throw new GeneralSecurityException(e.getMessage(), e);
    }
}

From source file:be.e_contract.dssp.client.SecurityTokenKeySelector.java

@Override
public Key getKey() {
    Key key = new SecretKeySpec(this.tokenKey, "HMACSHA1");
    return key;
}