Example usage for javax.crypto KeyGenerator init

List of usage examples for javax.crypto KeyGenerator init

Introduction

In this page you can find the example usage for javax.crypto KeyGenerator init.

Prototype

public final void init(int keysize) 

Source Link

Document

Initializes this key generator for a certain keysize.

Usage

From source file:Main.java

private static byte[] generateEncryptionSecret() {
    try {//from w w  w  . j av a2s.  com
        KeyGenerator generator = KeyGenerator.getInstance("AES");
        generator.init(128);

        SecretKey key = generator.generateKey();
        return key.getEncoded();
    } catch (NoSuchAlgorithmException ex) {
        Log.w("keyutil", ex);
        return null;
    }
}

From source file:es.logongas.fpempresa.security.SecureKeyGenerator.java

public static String getSecureKey() {
    try {/*from  w  ww.  j  av a  2s .co  m*/
        Base32 base32 = new Base32();
        KeyGenerator keyGen = KeyGenerator.getInstance("AES");
        keyGen.init(keyLength);
        SecretKey secretKey = keyGen.generateKey();
        byte[] encoded = secretKey.getEncoded();
        return base32.encodeAsString(encoded);
    } catch (Exception ex) {
        throw new RuntimeException(ex);
    }
}

From source file:Main.java

private static Key getKey(Context mContext) {
    if (key == null) {
        try {//from  w  ww. ja  va  2s.  c om
            KeyGenerator _generator = KeyGenerator.getInstance("DES");
            _generator.init(new SecureRandom(mContext.getPackageName().getBytes()));
            key = _generator.generateKey();
            _generator = null;
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    return key;
}

From source file:birch.util.EncryptionKeyFileUtil.java

public static String createKey(String cipher, int keysize) throws InvalidKeyException,
        IllegalBlockSizeException, NoSuchAlgorithmException, NoSuchPaddingException {
    KeyGenerator keyGenerator;
    Key key;//from  w  w  w .j  ava 2s.  c o m

    keyGenerator = KeyGenerator.getInstance(cipher);
    keyGenerator.init(keysize);
    key = keyGenerator.generateKey();

    return new String(Base64.encodeBase64(key.getEncoded()));
}

From source file:com.janrain.backplane.common.HmacHashUtils.java

private static SecretKey generateMacKey(String algorithm, int keySize) throws NoSuchAlgorithmException {
    KeyGenerator keyGen = KeyGenerator.getInstance(algorithm);
    keyGen.init(keySize);
    return keyGen.generateKey();
}

From source file:com.lecaddyfute.utils.security.AESCrypto.java

public static String encrypt1(String Data) throws Exception {
    KeyGenerator keyGen = KeyGenerator.getInstance(ALGO);
    keyGen.init(128);
    SecretKey key = keyGen.generateKey();
    System.out.println("Generated key: " + key);
    Cipher c = Cipher.getInstance(ALGO);
    c.init(Cipher.ENCRYPT_MODE, key);
    byte[] encVal = c.doFinal(Data.getBytes("UTF8"));

    String encryptedValue = ConvertionHelper.bytesToHex(encVal);
    //        String encryptedValue = new String(Base64.encodeBase64(encVal));
    return encryptedValue;
}

From source file:com.lecaddyfute.utils.security.AESCrypto.java

public static String decrypt1(String encryptedData) throws Exception {
    KeyGenerator keyGen = KeyGenerator.getInstance(ALGO);
    keyGen.init(128);
    SecretKey key = keyGen.generateKey();
    Cipher c = Cipher.getInstance(ALGO);

    c.init(Cipher.DECRYPT_MODE, key);
    //        byte[] decordedValue = Base64.decodeBase64(encryptedData.getBytes());
    //        byte[] decValue = c.doFinal(decordedValue);
    byte[] decValue = c.doFinal(encryptedData.getBytes("UTF8"));
    String decryptedValue = new String(decValue);
    return decryptedValue;
}

From source file:org.vosao.utils.CipherUtils.java

public static String generateKey() {
    try {/*  w w w. j  a  v  a 2  s  .  c  o  m*/
        KeyGenerator kgen = KeyGenerator.getInstance("AES");
        kgen.init(128);
        return new Base64().encodeToString(kgen.generateKey().getEncoded());
    } catch (NoSuchAlgorithmException e) {
        logger.error("Error generating secret key", e);
        return "";
    }
}

From source file:com.thoughtworks.go.server.util.EncryptionHelper.java

public static SecretKey generateAESKey() throws NoSuchAlgorithmException {
    KeyGenerator generator = KeyGenerator.getInstance("AES");
    generator.init(128); // The AES key size in number of bits
    return generator.generateKey();
}

From source file:Main.java

public static String encryption(String string) throws Exception {
    // TODO Auto-generated method stub

    //generate symmetric key
    KeyGenerator keygt = KeyGenerator.getInstance("AES");
    keygt.init(128);
    SecretKey symkey = keygt.generateKey();

    //get it encoded
    byte[] aes_ba = symkey.getEncoded();

    //create cipher
    SecretKeySpec skeySpec = new SecretKeySpec(aes_ba, "AES");
    Cipher cipher = Cipher.getInstance("AES");
    cipher.init(Cipher.ENCRYPT_MODE, skeySpec);

    //encryption//from   w w w  . j a  va 2  s .c o m
    byte[] EncSymbyteArray = cipher.doFinal(string.getBytes());

    //encrypt symKey with PublicKey
    //        Key pubKey = getPublicKey();

    Key pubKey = publicKey;

    //RSA cipher
    Cipher cipherAsm = Cipher.getInstance("RSA", "BC");
    cipherAsm.init(Cipher.ENCRYPT_MODE, pubKey);

    //RSA encryption
    byte[] asymEncsymKey = cipherAsm.doFinal(aes_ba);

    //           File f3 = new File(BASE_PATH,"enc.txt");
    //           File f3key = new File(BASE_PATH,"enckey.txt");
    //           File f3file = new File(BASE_PATH,"encfile.txt");
    //           writeToFile2(f3,f3key,f3file, asymEncsymKey, EncSymbyteArray);

    //byte != new String
    //return new String(byteMerger(asymEncsymKey, EncSymbyteArray));
    return Base64.encodeToString(byteMerger(asymEncsymKey, EncSymbyteArray), Base64.DEFAULT);

}