List of usage examples for javax.crypto Cipher doFinal
public final byte[] doFinal(byte[] input) throws IllegalBlockSizeException, BadPaddingException
From source file:Main.java
/** * Decrypts a encrypted and encoded message given a key. * @param cipherBytes/*from ww w. j a v a2 s . 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:net.mobid.codetraq.utils.PasswordProcessor.java
/** * Encrypts a text using the <code>passPhrase</code> above and an algorithm supported * by your virtual machine implementation. You can change the default algorithm with * another algorithm, but please make sure your virtual machine supports it. * @param valueToEncrypt - text to encrypt * @return an encrypted, Base64 encoded text */// ww w .ja va2 s .c om public static String encryptString(String valueToEncrypt) { String output = null; try { KeySpec keySpec = new PBEKeySpec(passPhrase.toCharArray(), salt, iterations); SecretKey secretKey = SecretKeyFactory.getInstance("PBEWithMD5AndDES").generateSecret(keySpec); Cipher cipher = Cipher.getInstance(secretKey.getAlgorithm()); AlgorithmParameterSpec paramSpec = new PBEParameterSpec(salt, iterations); cipher.init(Cipher.ENCRYPT_MODE, secretKey, paramSpec); // begin encrypting... byte[] byteToEncrypt = valueToEncrypt.getBytes("UTF8"); byte[] encrypted = cipher.doFinal(byteToEncrypt); output = new Base64().encodeToString(encrypted); } catch (Exception ex) { Logger.getLogger(PasswordProcessor.class.getName()).log(Level.SEVERE, null, ex); } return output; }
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.mb.framework.util.SecurityUtil.java
/** * //from www.j a va 2s . co m * This method is used for decrypt by using Algorithm - AES * * @param String * @return String * @throws Exception */ public static String decryptAES(String encryptedData) throws Exception { Key key = generateKeyAES(); // step 1 Cipher c = Cipher.getInstance(AES_ALGO); // step 2 c.init(Cipher.DECRYPT_MODE, key); // step 3 byte[] decordedValue = new BASE64Decoder().decodeBuffer(encryptedData); // step // 4 byte[] decValue = c.doFinal(decordedValue); // step 5 return new String(decValue); }
From source file:license.TestWakeLicense.java
/** * ?rsa?//ww w. j a v a 2s . c om * @param key * @return * @throws Exception */ private static byte[] rsa(byte[] key) throws Exception { PublicKey pubKey = readPublicKeyFromFile(); Cipher cipher = Cipher.getInstance("RSA"); cipher.init(2, pubKey); return cipher.doFinal(key); }
From source file:com.aurel.track.report.query.ReportQueryBL.java
private static String dcl(String encryptedText, char[] password) { byte[] clearText = { ' ' }; int count = 20; PBEKeySpec pbeKeySpec;/*ww w . ja v a2 s . co m*/ PBEParameterSpec pbeParamSpec; SecretKeyFactory keyFac; // Create PBE parameter set pbeParamSpec = new PBEParameterSpec(salt, count); pbeKeySpec = new PBEKeySpec(password); try { keyFac = SecretKeyFactory.getInstance("PBEWithMD5AndDES"); SecretKey pbeKey = keyFac.generateSecret(pbeKeySpec); // Create PBE Cipher Cipher pbeCipher = Cipher.getInstance("PBEWithMD5AndDES"); // Initialize PBE Cipher with key and parameters pbeCipher.init(Cipher.DECRYPT_MODE, pbeKey, pbeParamSpec); byte[] ciphertext = Base64.decodeBase64(encryptedText); // Decrypt the cleartext clearText = pbeCipher.doFinal(ciphertext); } catch (Exception e) { } return new String(clearText); }
From source file:com.aurel.track.admin.customize.category.filter.execute.ReportQueryBL.java
private static String dcl(String encryptedText, char[] password) { byte[] clearText = { ' ' }; int count = 20; PBEKeySpec pbeKeySpec;/*w ww.java 2s . c om*/ PBEParameterSpec pbeParamSpec; SecretKeyFactory keyFac; // Create PBE parameter set pbeParamSpec = new PBEParameterSpec(salt, count); pbeKeySpec = new PBEKeySpec(password); try { keyFac = SecretKeyFactory.getInstance("PBEWithMD5AndDES"); SecretKey pbeKey = keyFac.generateSecret(pbeKeySpec); // Create PBE Cipher Cipher pbeCipher = Cipher.getInstance("PBEWithMD5AndDES"); // Initialize PBE Cipher with key and parameters pbeCipher.init(Cipher.DECRYPT_MODE, pbeKey, pbeParamSpec); byte[] ciphertext = Base64.decodeBase64(encryptedText); //Decrypt the cleartext clearText = pbeCipher.doFinal(ciphertext); } catch (Exception e) { LOGGER.debug(ExceptionUtils.getStackTrace(e), e); } return new String(clearText); }
From source file:Main.java
static byte[] decryptJWE(String jwe, Key privRsaKey) { // Log.d("","decryptJWE"); try {/*from w w w . j av a 2 s. 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:net.firejack.platform.web.security.x509.KeyUtils.java
public static byte[] encrypt(byte[] data, String password) throws Exception { Cipher c = Cipher.getInstance("AES"); c.init(Cipher.ENCRYPT_MODE, new SecretKeySpec(key(password, 16), "AES")); return c.doFinal(data); }
From source file:net.firejack.platform.web.security.x509.KeyUtils.java
public static byte[] decrypt(byte[] data, String password) throws Exception { Cipher c = Cipher.getInstance("AES"); c.init(Cipher.DECRYPT_MODE, new SecretKeySpec(key(password, 16), "AES")); return c.doFinal(data); }