List of usage examples for javax.crypto SecretKey getEncoded
public byte[] getEncoded();
From source file:com.lling.qiqu.utils.AesUtils.java
/** * Returns an AES nbit Base64 key// w w w . ja v a 2 s. com * * @param keySize Size of the key * @return AES 128bit Base64 key */ public static String generateKey(int keySize) { String key = ""; KeyGenerator kgen = null; try { kgen = KeyGenerator.getInstance("AES"); kgen.init(keySize); SecretKey skey = kgen.generateKey(); byte[] raw = skey.getEncoded(); key = new String(Base64.encodeBase64(raw)); } catch (Exception e) { logger.error(e.getMessage(), e); } return key; }
From source file:com.mb.framework.util.SecurityUtil.java
/** * /*from w w w . j av a 2s . co m*/ * This method is used for encrypt by using Algorithm - AES/CBC/PKCS5Padding * * @param String * @return String * @throws Exception */ public static String encryptAESPBKDF2(String plainText) throws Exception { // get salt salt = generateSaltAESPBKDF2(); byte[] saltBytes = salt.getBytes("UTF-8"); // Derive the key SecretKeyFactory factory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1"); PBEKeySpec spec = new PBEKeySpec(SECRET_KEY.toCharArray(), saltBytes, pswdIterations, keySize); SecretKey secretKey = factory.generateSecret(spec); SecretKeySpec secret = new SecretKeySpec(secretKey.getEncoded(), "AES"); // encrypt the message Cipher cipher = Cipher.getInstance(AES_CBC_PKCS5PADDING_ALGO); cipher.init(Cipher.ENCRYPT_MODE, secret); AlgorithmParameters params = cipher.getParameters(); ivBytes = params.getParameterSpec(IvParameterSpec.class).getIV(); byte[] encryptedTextBytes = cipher.doFinal(plainText.getBytes("UTF-8")); return new Base64().encodeAsString(encryptedTextBytes); }
From source file:pepperim.util.IMCrypt.java
/** * Generates a random string to be used as AES encryption key * @return random AES encryption key/*from w ww .j av a 2 s. c om*/ */ public static String AES_genKey() { try { KeyGenerator kgen = KeyGenerator.getInstance("AES"); kgen.init(128); SecretKey skey = kgen.generateKey(); byte[] raw = skey.getEncoded(); return binToHex(raw); } catch (NoSuchAlgorithmException e) { Main.log(e.getMessage()); return ""; } }
From source file:com.mb.framework.util.SecurityUtil.java
/** * /*from w w w .j a v a 2 s . com*/ * This method is used for decrypt by using Algorithm - AES/CBC/PKCS5Padding * * @param String * @return String * @throws Exception */ @SuppressWarnings("static-access") public static String decryptAESPBKDF2(String encryptedText) throws Exception { byte[] saltBytes = salt.getBytes("UTF-8"); byte[] encryptedTextBytes = new Base64().decodeBase64(encryptedText); // Derive the key SecretKeyFactory factory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1"); PBEKeySpec spec = new PBEKeySpec(SECRET_KEY.toCharArray(), saltBytes, pswdIterations, keySize); SecretKey secretKey = factory.generateSecret(spec); SecretKeySpec secret = new SecretKeySpec(secretKey.getEncoded(), "AES"); // Decrypt the message Cipher cipher = Cipher.getInstance(AES_CBC_PKCS5PADDING_ALGO); cipher.init(Cipher.DECRYPT_MODE, secret, new IvParameterSpec(ivBytes)); byte[] decryptedTextBytes = null; try { decryptedTextBytes = cipher.doFinal(encryptedTextBytes); } catch (IllegalBlockSizeException e) { LOGGER.error("error " + e.getMessage()); } catch (BadPaddingException e) { LOGGER.error("error " + e.getMessage()); } return new String(decryptedTextBytes); }
From source file:org.forgerock.openam.openid.provider.Codec.java
public static String encodeSecretKey(SecretKey value) { if (value == null) { return null; }//from ww w .j av a 2s . c o m return encodeBytes(value.getEncoded()); }
From source file:com.thoughtworks.go.domain.AccessToken.java
static String digestToken(String originalToken, String salt) { try {// w ww. j ava2 s .c o m ACCESS_TOKEN_LOGGER.debug( "Generating secret using algorithm: {} with spec: DEFAULT_ITERATIONS: {}, DESIRED_KEY_LENGTH: {}", KEY_ALGORITHM, DEFAULT_ITERATIONS, DESIRED_KEY_LENGTH); SecretKeyFactory factory = SecretKeyFactory.getInstance(KEY_ALGORITHM); SecretKey key = factory.generateSecret(new PBEKeySpec(originalToken.toCharArray(), salt.getBytes(), DEFAULT_ITERATIONS, DESIRED_KEY_LENGTH)); return Hex.encodeHexString(key.getEncoded()); } catch (NoSuchAlgorithmException | InvalidKeySpecException e) { throw new RuntimeException(e); } }
From source file:org.cryptomator.crypto.aes256.AesSivCipherUtil.java
static byte[] sivEncrypt(SecretKey aesKey, SecretKey macKey, byte[] plaintext, byte[]... additionalData) throws InvalidKeyException { final byte[] aesKeyBytes = aesKey.getEncoded(); final byte[] macKeyBytes = macKey.getEncoded(); if (aesKeyBytes == null || macKeyBytes == null) { throw new IllegalArgumentException("Can't get bytes of given key."); }/* www . j a v a2 s. c o m*/ try { return sivEncrypt(aesKeyBytes, macKeyBytes, plaintext, additionalData); } finally { Arrays.fill(aesKeyBytes, (byte) 0); Arrays.fill(macKeyBytes, (byte) 0); } }
From source file:org.oscarehr.common.hl7.v2.oscar_to_oscar.SendingUtils.java
private static byte[] encryptData(byte[] dataBytes, SecretKey senderSecretKey) throws InvalidKeyException, NoSuchAlgorithmException, NoSuchPaddingException, IllegalBlockSizeException, BadPaddingException { SecretKeySpec secretKeySpec = new SecretKeySpec(senderSecretKey.getEncoded(), "AES"); Cipher cipher = Cipher.getInstance("AES"); cipher.init(Cipher.ENCRYPT_MODE, secretKeySpec); return (cipher.doFinal(dataBytes)); }
From source file:test.SecureSampleApp.java
private static EncryptedMessage encryptMessage(SecretKey sessionKey, byte[] requestString) throws IOException, EncryptionException { PublicKey publicKey = readPublicKey(); byte[] encryptedContent = CipherUtils.encrypt(requestString, sessionKey); byte[] encryptedKey = CipherUtils.encrypt(sessionKey.getEncoded(), publicKey); EncryptedMessage encryptedMessage = new EncryptedMessage(encryptedContent, encryptedKey); return encryptedMessage; }
From source file:org.ow2.proactive.authentication.crypto.HybridEncryptionUtil.java
public static HybridEncryptedData encrypt(PublicKey publicKey, String cipher, byte[] message) throws KeyException { // generate symmetric key SecretKey aesKey = KeyUtil.generateKey(AES_ALGO, AES_KEYSIZE); byte[] encData; byte[] encAes; // encrypt AES key with public RSA key try {/*from w ww . java2 s .c om*/ encAes = KeyPairUtil.encrypt(publicKey, cipher, aesKey.getEncoded()); } catch (KeyException e) { throw new KeyException("Symmetric key encryption failed", e); } // encrypt clear credentials with AES key try { encData = KeyUtil.encrypt(aesKey, AES_CIPHER, message); } catch (KeyException e) { throw new KeyException("Message encryption failed", e); } return new HybridEncryptedData(encAes, encData); }