Example usage for javax.crypto SecretKey getEncoded

List of usage examples for javax.crypto SecretKey getEncoded

Introduction

In this page you can find the example usage for javax.crypto SecretKey getEncoded.

Prototype

public byte[] getEncoded();

Source Link

Document

Returns the key in its primary encoding format, or null if this key does not support encoding.

Usage

From source file:de.taimos.dvalin.interconnect.model.MessageCryptoUtil.java

private static void generateKey() {
    try {/*from www.ja  v a2 s. c o  m*/
        final KeyGenerator kgen = KeyGenerator.getInstance("AES");
        kgen.init(128);
        final SecretKey skey = kgen.generateKey();
        System.out.println("Key: " + Hex.encodeHexString(skey.getEncoded()));
    } catch (final NoSuchAlgorithmException e) {
        e.printStackTrace();
    }
}

From source file:org.apache.juddi.adminconsole.AES.java

/**
 * generates an AES based off of the selected key size
 *
 * @param keysize//from   www  . j  av  a 2 s. c om
 * @return may return null if the key is not of a supported size by the
 * current jdk
 */
public static String GEN(int keysize) {
    KeyGenerator kgen;
    try {
        kgen = KeyGenerator.getInstance("AES");
        kgen.init(keysize);
        SecretKey skey = kgen.generateKey();
        byte[] raw = skey.getEncoded();
        return new String(Base64.encodeBase64(raw), Charset.defaultCharset());
    } catch (Exception ex) {
        log.fatal("error generating key", ex);
    }
    return null;
}

From source file:models.logic.CipherDecipher.java

public static void keyToFile(SecretKey key, String keyPath) throws IOException {
    File file = new File(keyPath);
    char[] hex = encodeHex(key.getEncoded());
    FileUtils.writeStringToFile(file, String.valueOf(hex));
}

From source file:com.miyue.util.Cryptos.java

/**
 * ?HMAC-SHA1,,160?(20)./*from  ww w.  j a v  a  2 s .  c  om*/
 * HMAC-SHA1?, RFC2401160?(20).
 */
public static byte[] generateHmacSha1Key() {
    try {
        KeyGenerator keyGenerator = KeyGenerator.getInstance(HMACSHA1);
        keyGenerator.init(DEFAULT_HMACSHA1_KEYSIZE);
        SecretKey secretKey = keyGenerator.generateKey();
        return secretKey.getEncoded();
    } catch (GeneralSecurityException e) {
        throw Exceptions.unchecked(e);
    }
}

From source file:com.miyue.util.Cryptos.java

/**
 * ?AES,?128,192,256?./*from  ww w . j  a  v a 2 s.  c om*/
 */
public static byte[] generateAesKey(int keysize) {
    try {
        KeyGenerator keyGenerator = KeyGenerator.getInstance(AES);
        keyGenerator.init(keysize);
        SecretKey secretKey = keyGenerator.generateKey();
        return secretKey.getEncoded();
    } catch (GeneralSecurityException e) {
        throw Exceptions.unchecked(e);
    }
}

From source file:CipherProvider.java

/**
 * Create cipher for encrypt or decrypt backup content
 *
 * @param passwd passwd for encryption//from  www  .j  av  a 2s  .  c o  m
 * @param mode   encrypt/decrypt mode
 *
 * @return instance of cipher
 */
private static Cipher getCipher(final String passwd, final int mode) {
    /* Derive the key, given password and salt. */
    Cipher cipher = null;
    try {
        SecretKeyFactory factory = null;
        factory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1");
        String salt = "slNadZlato#%^^&(&(5?@#5166?1561?#%^^*^&54431"; // only pseudorandom salt
        KeySpec spec = new PBEKeySpec(passwd.toCharArray(), salt.getBytes(), 65536, 128);
        SecretKey tmp = factory.generateSecret(spec);
        SecretKey secret = new SecretKeySpec(tmp.getEncoded(), CIPHER_TYPE);

        // initialization vector
        byte[] iv = Arrays.copyOfRange(DigestUtils.md5(passwd), 0, 16);
        AlgorithmParameterSpec paramSpec = new IvParameterSpec(iv);

        // Cipher for encryption
        cipher = Cipher.getInstance(CIPHER_TYPE + "/CBC/PKCS5Padding");
        cipher.init(mode, secret, paramSpec);
    } catch (Exception e) {
        e.printStackTrace(); //Todo implementovat
    }
    return cipher;
}

From source file:com.the_incognito.darry.incognitochatmessengertest.BouncyCastleImplementation.java

private static Key generateKeySpec(String key) throws NoSuchAlgorithmException, InvalidKeySpecException {
    SecretKeyFactory factory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA256", new BouncyCastleProvider());
    char password[] = key.toCharArray();
    byte salt[] = "salt".getBytes();
    KeySpec spec = new PBEKeySpec(password, salt, 65536, 128);
    SecretKey tmp = factory.generateSecret(spec);
    SecretKey secret = new SecretKeySpec(tmp.getEncoded(), "AES");
    return secret;
    //To change body of generated methods, choose Tools | Templates.
}

From source file:org.sakuli.services.cipher.AesCbcCipher.java

private static void checkCipherParameters(SecretKey aesKey, byte[] plaintext) throws SakuliCipherException {
    if (aesKey == null || aesKey.getEncoded() == null || aesKey.getEncoded().length == 0) {
        throw new SakuliCipherException("Provided AES key is null or empty");
    }// www  .j a  va 2s .  co m
    if (plaintext == null || plaintext.length == 0) {
        throw new SakuliCipherException("Empty secret can not en-/decrypted!");
    }
}

From source file:com.ro.ssc.app.client.licensing.TrialKeyGenerator.java

public static String generateKey(String toEncode) {

    String encoded = "";
    try {//from   w ww  .j a  va2  s .  c  om
        byte[] saltEncrypt = SALT_ENCRYPT.getBytes();
        SecretKeyFactory factoryKeyEncrypt = SecretKeyFactory.getInstance(SECRET_KEY_FACTORY);
        SecretKey tmp = factoryKeyEncrypt.generateSecret(
                new PBEKeySpec(PASS_ENCRYPT.toCharArray(), saltEncrypt, ITERATIONS_ENCRYPT, KEY_LENGTH));
        SecretKeySpec encryptKey = new SecretKeySpec(tmp.getEncoded(), ALGORITHM);
        Cipher aesCipherEncrypt = Cipher.getInstance(CIPHER);
        aesCipherEncrypt.init(Cipher.ENCRYPT_MODE, encryptKey);
        byte[] bytes = StringUtils.getBytesUtf8(toEncode);
        byte[] encryptBytes = aesCipherEncrypt.doFinal(bytes);
        encoded = Base64.encodeBase64URLSafeString(encryptBytes);
    } catch (Exception e) {
        e.printStackTrace();
    }
    return encoded;
}

From source file:net.arccotangent.pacchat.crypto.MsgCrypto.java

public static String encryptAndSignMessage(String msg, PublicKey publicKey, PrivateKey privateKey) {
    mc_log.i("Encrypting and signing message.");
    SecretKey aes = AES.generateAESKey();

    assert aes != null;

    byte[] aesCryptedText = AES.encryptBytes(msg.getBytes(), aes);
    byte[] cryptedAesKey = RSA.encryptBytes(aes.getEncoded(), publicKey);

    byte[] signature = RSA.signBytes(cryptedAesKey, privateKey);

    String cryptedTextB64 = Base64.encodeBase64String(aesCryptedText);
    String cryptedKeyB64 = Base64.encodeBase64String(cryptedAesKey);
    String signatureB64 = Base64.encodeBase64String(signature);

    return cryptedKeyB64 + "\n" + cryptedTextB64 + "\n" + signatureB64;
}