Example usage for javax.crypto SecretKeyFactory getInstance

List of usage examples for javax.crypto SecretKeyFactory getInstance

Introduction

In this page you can find the example usage for javax.crypto SecretKeyFactory getInstance.

Prototype

public static final SecretKeyFactory getInstance(String algorithm) throws NoSuchAlgorithmException 

Source Link

Document

Returns a SecretKeyFactory object that converts secret keys of the specified algorithm.

Usage

From source file:com.sds.acube.ndisc.xnapi.XNApiDesCipher.java

/**
 *   ?<br>/*w  w w  .  j a va  2  s  .  c  o  m*/
 * 3DES ?? 24bit KEY ?<br>
 * DES ??  16bit KEY ?.<br>
 * 
 * @return 
 */
private static Key getKey() {
    String key = "x0134-ad17s658601j56-q75k2we0des-key".substring(0, 24); // ??   .
    try {
        DESedeKeySpec desKeySpec = new DESedeKeySpec(key.getBytes());
        SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DESede");
        return keyFactory.generateSecret(desKeySpec);
    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;
}

From source file:com.soctec.soctec.utils.Encryptor.java

/**
 * Constructs the encryptor./*  w w w  . j  a  va2 s  .c  o m*/
 * Creates the needed keys and cipher for encryption.
 *
 */
public Encryptor() {

    try {
        keySpec = new DESKeySpec(keyString.getBytes("UTF8"));
        keyFactory = SecretKeyFactory.getInstance("DES");
        key = keyFactory.generateSecret(keySpec);

        ecipher = Cipher.getInstance("DES");
        decipher = Cipher.getInstance("DES");
        ecipher.init(Cipher.ENCRYPT_MODE, key);
        decipher.init(Cipher.DECRYPT_MODE, key);

    } catch (InvalidKeyException | UnsupportedEncodingException | NoSuchAlgorithmException
            | InvalidKeySpecException | NoSuchPaddingException e) {
        e.printStackTrace();
    }
}

From source file:com.arwantech.docjavaui.utils.TripleDES.java

public TripleDES(String secretKey) throws Exception {
    myEncryptionKey = secretKey;/*w w w. j  ava  2  s.c  om*/
    myEncryptionScheme = DESEDE_ENCRYPTION_SCHEME;
    arrayBytes = myEncryptionKey.getBytes(UNICODE_FORMAT);
    ks = new DESedeKeySpec(arrayBytes);
    skf = SecretKeyFactory.getInstance(myEncryptionScheme);
    cipher = Cipher.getInstance(myEncryptionScheme);
    key = skf.generateSecret(ks);
}

From source file:ch.helmchen.camlapse.user.control.Encryption.java

public static String decrypt(String aBase64String) throws GeneralSecurityException, IOException {
    SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("PBEWithMD5AndDES");
    SecretKey key = keyFactory.generateSecret(new PBEKeySpec(PASSWORD));
    Cipher pbeCipher = Cipher.getInstance("PBEWithMD5AndDES");
    pbeCipher.init(Cipher.DECRYPT_MODE, key, new PBEParameterSpec(SALT, 20));
    return new String(pbeCipher.doFinal(base64Decode(aBase64String)));
}

From source file:Main.java

public static byte[] generateHash(char[] pass, byte[] salt)
        throws NoSuchAlgorithmException, InvalidKeySpecException {
    PBEKeySpec keySpec = new PBEKeySpec(pass, salt, ITERATION_COUNT, 128);
    SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(KEY_FACTORY);
    SecretKey key = keyFactory.generateSecret(keySpec);

    return key.getEncoded();
}

From source file:adminpassword.Decryption.java

@SuppressWarnings("static-access")
public String decrypt(String encryptedText, String idKey) throws Exception {
    String password = idKey;/*from   w w w .  j  a  v a2  s .  c  om*/

    Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");

    //strip off the salt and iv
    ByteBuffer buffer = ByteBuffer.wrap(new Base64().decode(encryptedText));
    byte[] saltBytes = new byte[20];
    buffer.get(saltBytes, 0, saltBytes.length);
    byte[] ivBytes1 = new byte[cipher.getBlockSize()];
    buffer.get(ivBytes1, 0, ivBytes1.length);
    byte[] encryptedTextBytes = new byte[buffer.capacity() - saltBytes.length - ivBytes1.length];
    buffer.get(encryptedTextBytes);

    // Deriving the key
    SecretKeyFactory factory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1");
    PBEKeySpec spec = new PBEKeySpec(password.toCharArray(), saltBytes, 65556, 256);

    SecretKey secretKey = factory.generateSecret(spec);
    SecretKeySpec secret = new SecretKeySpec(secretKey.getEncoded(), "AES");
    cipher.init(Cipher.DECRYPT_MODE, secret, new IvParameterSpec(ivBytes1));

    byte[] decryptedTextBytes = null;
    try {
        decryptedTextBytes = cipher.doFinal(encryptedTextBytes);

    } catch (IllegalBlockSizeException e) {
        e.printStackTrace();
    } catch (BadPaddingException e) {
        e.printStackTrace();
    }

    return new String(decryptedTextBytes);
}

From source file:com.comcast.cdn.traffic_control.traffic_router.core.util.StringProtector.java

public StringProtector(final String passwd) throws GeneralSecurityException {
    final SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("PBEWithMD5AndDES");
    final SecretKey key = keyFactory.generateSecret(new PBEKeySpec(passwd.toCharArray()));
    encryptor = Cipher.getInstance("PBEWithMD5AndDES");
    encryptor.init(Cipher.ENCRYPT_MODE, key, new PBEParameterSpec(SALT, 20));

    decryptor = Cipher.getInstance("PBEWithMD5AndDES");
    decryptor.init(Cipher.DECRYPT_MODE, key, new PBEParameterSpec(SALT, 20));
}

From source file:com.aerohive.nms.engine.admin.task.licensemgr.common.AerohiveEncryptTool.java

public AerohiveEncryptTool(String arg_Key) {
    if (arg_Key != null && !arg_Key.trim().equals(""))
        m_Str_Key = arg_Key;

    try {//from  w w  w.  jav  a  2  s  .  c  o m
        // Create the key
        KeySpec keySpec = new PBEKeySpec(m_Str_Key.toCharArray(), m_Byte_Salt, m_Int_IterationCount);
        SecretKey key = SecretKeyFactory.getInstance("PBEWithMD5AndDES").generateSecret(keySpec);
        m_Cipher_Ecipher = Cipher.getInstance(key.getAlgorithm());
        m_Cipher_Dcipher = Cipher.getInstance(key.getAlgorithm());

        // Prepare the parameter to the ciphers
        AlgorithmParameterSpec paramSpec = new PBEParameterSpec(m_Byte_Salt, m_Int_IterationCount);

        // Create the ciphers
        m_Cipher_Ecipher.init(Cipher.ENCRYPT_MODE, key, paramSpec);
        m_Cipher_Dcipher.init(Cipher.DECRYPT_MODE, key, paramSpec);
    } catch (Exception e) {
        //DebugUtil.commonDebugWarn(e.getMessage());
    }
}

From source file:com.agiletec.aps.util.DefaultApsEncrypter.java

public static Key getKey() {
    try {//  w  w  w .j a v a  2s. c o  m
        byte[] bytes = KEY_STRING.getBytes();
        DESedeKeySpec pass = new DESedeKeySpec(bytes);
        SecretKeyFactory skf = SecretKeyFactory.getInstance(TRIPLE_DES_KEY_SPEC);
        SecretKey s = skf.generateSecret(pass);
        return s;
    } catch (Throwable t) {
        throw new RuntimeException("Error creating key", t);
    }
}

From source file:net.sf.hajdbc.codec.crypto.CipherCodecTest.java

@Before
public void before() throws Exception {
    SecretKeyFactory factory = SecretKeyFactory.getInstance(ALGORITHM);
    Key key = factory.generateSecret(new DESKeySpec(Base64.decodeBase64(KEY.getBytes())));

    this.codec = new CipherCodec(key);
}