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:net.fender.crypto.CryptoUtil.java

/**
 * @param base64Key/*w  ww. j a va 2s . co m*/
 * @return
 * @throws GeneralSecurityException
 */
public Key getKey(String base64Key) throws GeneralSecurityException {
    byte[] keyBytes = Base64.decodeBase64(base64Key.getBytes());
    Key key = new SecretKeySpec(keyBytes, algorithm);
    return key;
}

From source file:com.amazonaws.tvm.Utilities.java

public static String sign(String content, String key) {
    try {/*  www. j a  va 2  s .co  m*/
        byte[] data = content.getBytes(Constants.ENCODING_FORMAT);
        Mac mac = Mac.getInstance(Constants.SIGNATURE_METHOD);
        mac.init(new SecretKeySpec(key.getBytes(Constants.ENCODING_FORMAT), Constants.SIGNATURE_METHOD));
        char[] signature = Hex.encodeHex(mac.doFinal(data));
        return new String(signature);
    } catch (Exception exception) {
        log.log(Level.SEVERE, "Exception during sign", exception);
    }
    return null;
}

From source file:com.adaptris.security.password.AesCrypto.java

public String decode(String encrypted, String charset) throws PasswordException {

    String encryptedString = encrypted;
    String result;//from w ww  .  j  av  a2 s.co  m

    if (encrypted.startsWith(Password.PORTABLE_PASSWORD)) {
        encryptedString = encrypted.substring(Password.PORTABLE_PASSWORD.length());
    }
    try {
        Input input = new Input(encryptedString);
        input.read();
        SecretKey sessionKey = new SecretKeySpec(input.getSessionKey(), ALG);
        Cipher cipher = Cipher.getInstance(CIPHER);
        if (input.getSessionVector() != null) {
            IvParameterSpec spec = new IvParameterSpec(input.getSessionVector());
            cipher.init(Cipher.DECRYPT_MODE, sessionKey, spec);
        } else {
            cipher.init(Cipher.DECRYPT_MODE, sessionKey);
        }
        byte[] decrypted = cipher.doFinal(input.getEncryptedData());
        result = unseed(decrypted, charset);
    } catch (Exception e) {
        throw new PasswordException(e);
    }
    return result;
}

From source file:it.geosolutions.figis.persistence.dao.util.PwEncoder.java

/**
 * Decode the password on base 64//from ww  w  .j a v a  2 s  . co  m
 * @param msg
 * @return
 */
public static String decode(String msg) {
    try {
        SecretKeySpec keySpec = new SecretKeySpec(KEY, "AES");
        Cipher cipher = Cipher.getInstance("AES");
        cipher.init(Cipher.DECRYPT_MODE, keySpec);

        byte[] de64 = Base64.decodeBase64(msg);
        byte[] decrypted = cipher.doFinal(de64);

        return new String(decrypted);
    } catch (NoSuchAlgorithmException ex) {
        throw new RuntimeException("Error while encoding", ex);
    } catch (NoSuchPaddingException ex) {
        throw new RuntimeException("Error while encoding", ex);
    } catch (IllegalBlockSizeException ex) {
        throw new RuntimeException("Error while encoding", ex);
    } catch (BadPaddingException ex) {
        throw new RuntimeException("Error while encoding", ex);
    } catch (InvalidKeyException ex) {
        throw new RuntimeException("Error while encoding", ex);
    }
}

From source file:io.kahu.hawaii.util.encryption.CryptoUtil.java

private static Cipher initCipher(int mode, String key, String initVector) throws GeneralSecurityException {
    Cipher cipher = Cipher.getInstance("AES/CBC/PKCS7Padding");

    SecretKeySpec secretKeySpec = new SecretKeySpec(hexStringToByteArray(key), "AES");
    IvParameterSpec initVectorSpec = new IvParameterSpec(hexStringToByteArray(initVector));
    cipher.init(mode, secretKeySpec, initVectorSpec);

    return cipher;
}

From source file:org.cloudfoundry.identity.uaa.authentication.RubyUserTokenTests.java

@Before
public void setUp() throws Exception {
    macKey = new SecretKeySpec("5".getBytes("UTF-8"), "HMACSHA1");
}

From source file:edu.vt.middleware.crypt.util.CryptReader.java

/**
 * Reads the raw bytes of a symmetric encryption key from an input stream.
 *
 * @param  keyStream  Stream containing key data.
 * @param  algorithm  Symmetric cipher algorithm for which key is used.
 *
 * @return  Secret key.//from ww  w . j  a  v a 2  s.  c o m
 *
 * @throws  IOException  On IO errors.
 */
public static SecretKey readSecretKey(final InputStream keyStream, final String algorithm) throws IOException {
    return new SecretKeySpec(readData(keyStream), algorithm);
}

From source file:example.DecrypterException.java

public static byte[] decryptSafeWebStringToByte(String webString, String secretKey, String signKey) {
    if (32 != secretKey.getBytes().length)
        return null;
    if (32 != signKey.getBytes().length)
        return null;

    SecretKey encryptionKey = new SecretKeySpec(secretKey.getBytes(), "HmacSHA1");
    SecretKey integrityKey = new SecretKeySpec(signKey.getBytes(), "HmacSHA1");
    byte[] retStr = null;
    try {//from   w  w w. j a v  a2s. c  om
        retStr = decryptSafeWebString(webString, encryptionKey, integrityKey);
    } catch (Exception e) {
        e.printStackTrace();
    }
    return retStr;
}

From source file:net.fender.crypto.CryptoUtil.java

/**
 * Returns a key based on System.getProperties(). Use setSystemPropertyKey
 * and setSystemPropertyAlgorithm to configure, or manually set System
 * properties using SYSTEM_PROPERTY_KEY_PREFIX.
 * /*from  ww w. j ava 2  s . com*/
 * @param keyName
 * @return
 * @throws GeneralSecurityException
 */
public static Key getSystemPropertyKey(String keyName) throws GeneralSecurityException {
    String base64Key = System.getProperty(SYSTEM_PROPERTY_KEY_PREFIX + keyName);
    String algorithm = System.getProperty(SYSTEM_PROPERTY_KEY_PREFIX + keyName + ".algorithm");
    byte[] keyBytes = Base64.decodeBase64(base64Key.getBytes());
    Key key = new SecretKeySpec(keyBytes, algorithm);
    return key;
}

From source file:cipher.UsableCipher.java

protected SecretKey generateSecretKey(String key, String secretKeyAlgorithm) throws Exception {
    return new SecretKeySpec(bytes(key), secretKeyAlgorithm);
}