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.web.mavenproject6.utility.EncryptionUtil.java

@Autowired
public EncryptionUtil(Environment env) {
    this.env = env;
    String key = Base64.getEncoder().encodeToString(this.env.getProperty("aes.key").getBytes());
    this.skeySpec = new SecretKeySpec(key.getBytes(), "AES");
}

From source file:lib.clases_cripto.java

public static String Desencriptar(String textoEncriptado) throws Exception {

    String secretKey = "qualityinfosolutions"; //llave para desenciptar datos
    String base64EncryptedString = "";

    try {/*from  w w  w .  jav a2  s.  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:ec.edu.uce.medicina.seguimiento.util.EncryptionUtility.java

/**
 *Mtodo que permite la desencriptacin de la contrasea
 * @param encrypted/*w ww . ja  v a 2s.  c om*/
 * @return
 * @throws Exception
 */
public static String decrypt(String encrypted) throws Exception {
    String key = "02AE31B79CCCB2A3"; //llave
    String iv = "0123456789ABCDEF";
    Cipher cipher = Cipher.getInstance(cI);
    SecretKeySpec skeySpec = new SecretKeySpec(key.getBytes(), alg);
    IvParameterSpec ivParameterSpec = new IvParameterSpec(iv.getBytes());
    byte[] enc = decodeBase64(encrypted);
    cipher.init(Cipher.DECRYPT_MODE, skeySpec, ivParameterSpec);
    byte[] decrypted = cipher.doFinal(enc);
    return new String(decrypted);
}

From source file:com.lwr.software.reporter.utils.EncryptionUtil.java

public static String decrypt(String encrypted) {
    try {//from w w w .ja v a  2  s.c  o  m
        IvParameterSpec iv = new IvParameterSpec(
                DashboardConstants.INIT_VECTOR.getBytes(DashboardConstants.ENCODING));
        SecretKeySpec skeySpec = new SecretKeySpec(
                DashboardConstants.INIT_KEY.getBytes(DashboardConstants.ENCODING),
                DashboardConstants.ALGORITHM);
        Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5PADDING");
        cipher.init(Cipher.DECRYPT_MODE, skeySpec, iv);
        byte[] original = cipher.doFinal(Base64.decodeBase64(encrypted));
        return new String(original);
    } catch (Exception ex) {
        ex.printStackTrace();
    }
    return null;
}

From source file:architecture.common.BlowFishTest.java

@Test
public void testBlowfish() throws Exception {
    String Key = "password";
    byte[] KeyData = Key.getBytes();
    SecretKeySpec KS = new SecretKeySpec(KeyData, "Blowfish");
    Cipher cipher = Cipher.getInstance("Blowfish");
    cipher.init(Cipher.ENCRYPT_MODE, KS);

    // encrypt message
    String inputText = "MyTextToEncrypt";
    byte[] encrypted = cipher.doFinal(inputText.getBytes());
    String encryptedString = Hex.encodeHexString(encrypted);
    System.out.println(" : " + encryptedString);

    cipher.init(Cipher.DECRYPT_MODE, KS);
    byte[] decrypt = cipher.doFinal(Hex.decodeHex(encryptedString.toCharArray()));
    System.out.println("   : " + new String(decrypt));

}

From source file:com.js.engine.license.licensegen.AES256.java

public AES256(String key) throws UnsupportedEncodingException {
    // TODO Auto-generated constructor stub

    this.iv = key.substring(0, 16);

    byte[] keyBytes = new byte[16];
    byte[] b = key.getBytes("UTF-8");
    int len = b.length;
    if (len > keyBytes.length)
        len = keyBytes.length;/* w  w  w  .j a  v  a2s.c o m*/
    System.arraycopy(b, 0, keyBytes, 0, len);
    SecretKeySpec keySpec = new SecretKeySpec(keyBytes, "AES");

    this.keySpec = keySpec;
}

From source file:it.latraccia.pkcs11.reader.util.AESUtil.java

public static String encryptString(String clearText, String password)
        throws NoSuchPaddingException, NoSuchAlgorithmException, InvalidAlgorithmParameterException,
        InvalidKeyException, BadPaddingException, IllegalBlockSizeException {
    String encrypted;/*from  w w  w  .  j a v a  2s . com*/
    byte[] key = password.getBytes();
    // Check the length of the password
    if (key.length != 16) {
        throw new IllegalArgumentException("Invalid password length.");
    }
    byte[] value = clearText.getBytes();

    // Encrypt with AES/CBC/PKCS5Padding
    SecretKeySpec skeySpec = new SecretKeySpec(key, "AES");
    Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
    cipher.init(Cipher.ENCRYPT_MODE, skeySpec, new IvParameterSpec(new byte[16]));
    byte[] encryptedBytes = cipher.doFinal(value);

    // Encode the bytes in base64
    encrypted = Base64.encodeBase64String(encryptedBytes);
    return encrypted;
}

From source file:com.fujitsu.dc.common.auth.token.LocalToken.java

/**
 * Key???//from   w ww .  ja  va 2  s.co  m
 * @param keyString .
 */
public static void setKeyString(String keyString) {
    keyBytes = keyString.getBytes(); // 16/24/32????
    aesKey = new SecretKeySpec(keyBytes, "AES");
}

From source file:com.doculibre.constellio.utils.aes.SimpleProtector.java

private static Key generateKey() throws Exception {
    Key key = new SecretKeySpec(keyValue, ALGORITHM);
    // SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(ALGORITHM);
    // key = keyFactory.generateSecret(new DESKeySpec(keyValue));
    return key;//from  w ww  .  ja  va 2 s . c  o  m
}

From source file:info.bonjean.beluga.util.CryptoUtil.java

private static Cipher getCipher(String strKey, boolean encoder) {
    Cipher cipher = null;/*from  w ww  .j  a  va2 s  .c  o  m*/
    if (encoder)
        cipher = encryptCiphers.get(strKey);
    else
        cipher = decryptCiphers.get(strKey);

    if (cipher != null)
        return cipher;

    try {
        SecretKeySpec key = new SecretKeySpec(strKey.getBytes(), "Blowfish");
        cipher = Cipher.getInstance("Blowfish/ECB/NoPadding");
        cipher.init(encoder ? Cipher.ENCRYPT_MODE : Cipher.DECRYPT_MODE, key);

        if (encoder)
            encryptCiphers.put(strKey, cipher);
        else
            decryptCiphers.put(strKey, cipher);
    } catch (Exception e) {
        log.error(e.getMessage(), e);
    }

    return cipher;
}