List of usage examples for javax.crypto Cipher init
public final void init(int opmode, Certificate certificate, SecureRandom random) throws InvalidKeyException
From source file:net.seleucus.wsp.crypto.FwknopSymmetricCrypto.java
public static String encrypt(byte[] key, String message) throws NoSuchAlgorithmException, IOException, NoSuchPaddingException, InvalidKeyException, InvalidAlgorithmParameterException, IllegalBlockSizeException, BadPaddingException { SecureRandom sr = new SecureRandom(); byte[] salt = new byte[8]; sr.nextBytes(salt);//from www . j a v a 2s . com byte[][] key_and_iv = deriveKeyAndIV(salt, key); SecretKeySpec enc_key; enc_key = new SecretKeySpec(key_and_iv[0], "AES"); Cipher aes = Cipher.getInstance("AES/CBC/PKCS5Padding"); IvParameterSpec iv = new IvParameterSpec(key_and_iv[1]); aes.init(Cipher.ENCRYPT_MODE, enc_key, iv); byte[] salted = "Salted__".getBytes("UTF-8"); byte[] cipher = aes.doFinal(message.getBytes("UTF-8")); byte[] result = new byte[salted.length + salt.length + cipher.length]; // now we need to glue: "Salted__" + salt + cipher System.arraycopy(salted, 0, result, 0, salted.length); System.arraycopy(salt, 0, result, salted.length, salt.length); System.arraycopy(cipher, 0, result, salted.length + salt.length, cipher.length); // remove = and FWKNOP_ENCRYPTION_HEADER return Base64.encodeBase64String(result).replace("=", "").replace(FWKNOP_ENCRYPTION_HEADER, ""); }
From source file:Main.java
static byte[] decryptJWE(String jwe, Key privRsaKey) { // Log.d("","decryptJWE"); try {//from w w w . j a v a 2s . c o m // split jwe string StringTokenizer tokens = new StringTokenizer(jwe, "."); int count = tokens.countTokens(); // Log.d("","parts.length: "+count); if (count != 5) return null; String jweProtectedHeader64 = tokens.nextToken(); String jweEncrypted64 = tokens.nextToken(); String jweInitVector64 = tokens.nextToken(); String cryptedBytes64 = tokens.nextToken(); String auth_tag64 = tokens.nextToken(); // decrypt cek using private rsa key byte[] cek = decryptRsaB64(jweEncrypted64, privRsaKey); // check cek result byte array if (cek == null || cek.length == 0 || (cek.length % 2) != 0) return null; int keySize = cek.length / 2; Log.d("", "Decryption AES: " + keySize * 8); // build aes_key and hmac_key byte aes_key[] = new byte[keySize]; byte hmac_key[] = new byte[keySize]; System.arraycopy(cek, 0, hmac_key, 0, keySize); System.arraycopy(cek, keySize, aes_key, 0, keySize); // decode initialization vector byte[] iv_key = decodeB64(jweInitVector64); Log.d("", "hmac_key: " + bytesToHex(hmac_key)); Log.d("", "aes_key: " + bytesToHex(aes_key)); Log.d("", "iv_key: " + bytesToHex(iv_key)); // decrypt content using aes_key and iv_key byte[] cryptedBytes = decodeB64(cryptedBytes64); Cipher decrypt = Cipher.getInstance("AES/CBC/PKCS5Padding", "SC"); decrypt.init(Cipher.DECRYPT_MODE, new SecretKeySpec(aes_key, "AES"), new IvParameterSpec(iv_key)); byte[] decryptedBytes = decrypt.doFinal(cryptedBytes); Log.d("", "decryptedBytes:"); Log.d("", bytesToHex(decryptedBytes)); // validation verification byte[] aad = jweProtectedHeader64.getBytes(); long al = aad.length * 8; // concatenate aad, iv_key, cryptedBytes and al byte[] hmacData = new byte[aad.length + iv_key.length + cryptedBytes.length + 8]; int offset = 0; System.arraycopy(aad, offset, hmacData, 0, aad.length); offset += aad.length; System.arraycopy(iv_key, 0, hmacData, offset, iv_key.length); offset += iv_key.length; System.arraycopy(cryptedBytes, 0, hmacData, offset, cryptedBytes.length); offset += cryptedBytes.length; ByteBuffer buffer = ByteBuffer.allocate(8); buffer.putLong(al); System.arraycopy(buffer.array(), 0, hmacData, offset, 8); // compute hmac Mac hmac = Mac.getInstance("HmacSHA256", "SC"); hmac.init(new SecretKeySpec(hmac_key, "HmacSHA256")); byte[] hmacValue = hmac.doFinal(hmacData); // pick authentication tag byte[] authTag = Arrays.copyOf(hmacValue, 16); // validate authentication tag byte[] authTagRead = decodeB64(auth_tag64); for (int i = 0; i < 16; i++) { if (authTag[i] != authTagRead[i]) { Log.d("", "validation failed"); return decryptedBytes; } } Log.d("", "validation success"); // validation success return decryptedBytes; } catch (Exception e) { e.printStackTrace(); } return null; }
From source file:com.salesmanager.core.util.EncryptionUtil.java
public static String decryptFromExternal(String key, String value) throws Exception { if (value == null || value.equals("")) return ""; Cipher cipher = Cipher.getInstance("AES/CBC/NoPadding"); SecretKeySpec keySpec = new SecretKeySpec(key.getBytes(), "AES"); IvParameterSpec ivSpec = new IvParameterSpec("fedcba9876543210".getBytes()); cipher.init(Cipher.DECRYPT_MODE, keySpec, ivSpec); byte[] outText; outText = cipher.doFinal(hexToBytes(value)); return new String(outText); }
From source file:com.salesmanager.core.util.EncryptionUtil.java
public static String encrypt(String key, String value) throws Exception { // value = StringUtils.rightPad(value, 16,"*"); // Cipher cipher = Cipher.getInstance("AES/CBC/NoPadding"); // NEED TO UNDERSTAND WHY PKCS5Padding DOES NOT WORK Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding"); SecretKeySpec keySpec = new SecretKeySpec(key.getBytes(), "AES"); IvParameterSpec ivSpec = new IvParameterSpec("fedcba9876543210".getBytes()); cipher.init(Cipher.ENCRYPT_MODE, keySpec, ivSpec); byte[] inpbytes = value.getBytes(); byte[] encrypted = cipher.doFinal(inpbytes); return new String(bytesToHex(encrypted)); }
From source file:com.shenit.commons.codec.DesUtils.java
/** * /*from www . j a v a2 s . c o m*/ * @param rawData * @param rawKey * @param mode */ private static byte[] crypt(byte[] rawData, KeySpec keySpec, int mode) { // ?DESKeySpec byte[] result = null; try { // ?DESKeySpec??SecretKey SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(CODEC_DES); Key key = keyFactory.generateSecret(keySpec); if (key == null) { if (LOG.isWarnEnabled()) LOG.warn("[crypt] No key generated!"); return null; } // Cipher?? Cipher cipher = Cipher.getInstance(CODEC_DES); // DES???? cipher.init(mode, key, new SecureRandom()); // ?? // ?? result = cipher.doFinal(rawData); } catch (Exception ex) { LOG.warn("[crypt] crypt with exceptions", ex); } return result; }
From source file:com.salesmanager.core.util.EncryptionUtil.java
public static String decrypt(String key, String value) throws Exception { if (value == null || value.equals("")) return ""; // NEED TO UNDERSTAND WHY PKCS5Padding DOES NOT WORK // Cipher cipher = Cipher.getInstance("AES/CBC/NoPadding"); Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding"); SecretKeySpec keySpec = new SecretKeySpec(key.getBytes(), "AES"); IvParameterSpec ivSpec = new IvParameterSpec("fedcba9876543210".getBytes()); cipher.init(Cipher.DECRYPT_MODE, keySpec, ivSpec); byte[] outText; outText = cipher.doFinal(hexToBytes(value)); return new String(outText); }
From source file:com.cloudant.sync.datastore.encryption.DPKEncryptionUtil.java
/** * AES Encrypt a byte array/*w w w. j av a2 s . c o m*/ * * @param key The encryption key * @param iv The iv * @param unencryptedBytes The data to encrypt * @return The encrypted data * @throws NoSuchPaddingException * @throws NoSuchAlgorithmException * @throws InvalidAlgorithmParameterException * @throws InvalidKeyException * @throws BadPaddingException * @throws IllegalBlockSizeException */ public static byte[] encryptAES(SecretKey key, byte[] iv, byte[] unencryptedBytes) throws NoSuchPaddingException, NoSuchAlgorithmException, InvalidAlgorithmParameterException, InvalidKeyException, BadPaddingException, IllegalBlockSizeException { Cipher aesCipher = Cipher.getInstance("AES/CBC/PKCS5Padding"); IvParameterSpec ivParameter = new IvParameterSpec(iv); // see http://stackoverflow.com/a/11506343 Key encryptionKey = new SecretKeySpec(key.getEncoded(), "AES"); aesCipher.init(Cipher.ENCRYPT_MODE, encryptionKey, ivParameter); return aesCipher.doFinal(unencryptedBytes); }
From source file:com.cloudant.sync.datastore.encryption.DPKEncryptionUtil.java
/** * Decrypt an AES encrypted byte array/*w ww. j a v a 2 s . c om*/ * * @param key The encryption key * @param iv The iv * @param encryptedBytes The data to decrypt * @return The decrypted data * @throws NoSuchPaddingException * @throws NoSuchAlgorithmException * @throws InvalidAlgorithmParameterException * @throws InvalidKeyException * @throws BadPaddingException * @throws IllegalBlockSizeException */ public static byte[] decryptAES(SecretKey key, byte[] iv, byte[] encryptedBytes) throws NoSuchPaddingException, NoSuchAlgorithmException, InvalidAlgorithmParameterException, InvalidKeyException, BadPaddingException, IllegalBlockSizeException { Cipher aesCipher = Cipher.getInstance("AES/CBC/PKCS5Padding"); IvParameterSpec ivParameter = new IvParameterSpec(iv); // see http://stackoverflow.com/a/11506343 Key encryptionKey = new SecretKeySpec(key.getEncoded(), "AES"); aesCipher.init(Cipher.DECRYPT_MODE, encryptionKey, ivParameter); return aesCipher.doFinal(encryptedBytes); }
From source file:Main.java
/** * Decrypts a encrypted and encoded message given a key. * @param cipherBytes/*from w w w . jav a 2s . c o m*/ * @param key * @return * @throws NoSuchAlgorithmException * @throws NoSuchAlgorithmException * @throws InvalidAlgorithmParameterException * @throws IllegalBlockSizeException * @throws BadPaddingException * @throws InvalidAlgorithmParameterException * @throws NoSuchPaddingException * @throws InvalidKeyException */ public static byte[] decryptMessage(byte[] cipherBytes, SecretKey key) throws NoSuchAlgorithmException, NoSuchAlgorithmException, InvalidAlgorithmParameterException, IllegalBlockSizeException, BadPaddingException, InvalidAlgorithmParameterException, NoSuchPaddingException, InvalidKeyException { Cipher cipher = Cipher.getInstance("AES"); //ipher cipher = Cipher.getInstance("AES/CBC/PKCS7PADDING"); byte[] init = new byte[128 / 8]; SecureRandom secureRandom = new SecureRandom(); //secureRandom.nextBytes(init); for (int i = 0; i < 16; i++) init[i] = 0; cipher.init(Cipher.DECRYPT_MODE, key, new IvParameterSpec(init)); byte[] textBytes = cipher.doFinal(cipherBytes); return textBytes; }
From source file:com.frame.Conf.Utilidades.java
public static String encrypt(String key, String iv, String cleartext) throws Exception { Cipher cipher = Cipher.getInstance(cI); SecretKeySpec skeySpec = new SecretKeySpec(key.getBytes(), alg); IvParameterSpec ivParameterSpec = new IvParameterSpec(iv.getBytes()); cipher.init(Cipher.ENCRYPT_MODE, skeySpec, ivParameterSpec); byte[] encrypted = cipher.doFinal(cleartext.getBytes()); return new String(encodeBase64(encrypted)); }