List of usage examples for javax.crypto Cipher doFinal
public final byte[] doFinal(byte[] input) throws IllegalBlockSizeException, BadPaddingException
From source file:com.tesora.dve.common.PECryptoUtils.java
public static String encrypt(String str) throws PEException { if (StringUtils.isBlank(str)) return str; try {//from ww w.j ava 2 s.c o m Cipher cipher = createCrypter(Cipher.ENCRYPT_MODE); byte[] enc = cipher.doFinal(str.getBytes("UTF8")); return new String(Base64.encodeBase64(enc)); } catch (Exception e) { throw new PEException("Failed to encrypt '" + str + "'", e); } }
From source file:Main.java
public static String aesDecryptByBytes(byte[] encryptBytes, String decryptKey) throws Exception { KeyGenerator kgen = KeyGenerator.getInstance("AES"); kgen.init(128, new SecureRandom(decryptKey.getBytes())); Cipher cipher = Cipher.getInstance("AES"); cipher.init(Cipher.DECRYPT_MODE, new SecretKeySpec(kgen.generateKey().getEncoded(), "AES")); byte[] decryptBytes = cipher.doFinal(encryptBytes); return new String(decryptBytes); }
From source file:Main.java
public static byte[] TriDesEncryption(byte[] byteKey, byte[] dec) { try {// w w w.ja v a 2s .c om byte[] en_key = new byte[24]; if (byteKey.length == 16) { System.arraycopy(byteKey, 0, en_key, 0, 16); System.arraycopy(byteKey, 0, en_key, 16, 8); } SecretKeySpec key = new SecretKeySpec(en_key, "DESede"); Cipher ecipher = Cipher.getInstance("DESede/ECB/NoPadding"); ecipher.init(Cipher.ENCRYPT_MODE, key); byte[] en_b = ecipher.doFinal(dec); return en_b; } catch (Exception e) { e.printStackTrace(); } return null; }
From source file:Main.java
public static byte[] encryptMsg(String message) throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, InvalidParameterSpecException, IllegalBlockSizeException, BadPaddingException, UnsupportedEncodingException { if (message == null) { return new byte[0]; }/*w w w . j a v a2 s. c o m*/ /* Encrypt the message. */ Cipher cipher = null; cipher = Cipher.getInstance("AES/ECB/PKCS5Padding"); cipher.init(Cipher.ENCRYPT_MODE, secret); byte[] cipherText = cipher.doFinal(message.getBytes("UTF-8")); return cipherText; }
From source file:Main.java
private static byte[] encrypt(byte[] raw, byte[] clear) throws Exception { SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES"); Cipher cipher = Cipher.getInstance("AES"); cipher.init(Cipher.ENCRYPT_MODE, skeySpec); byte[] encrypted = cipher.doFinal(clear); return encrypted; }
From source file:Main.java
public static byte[] TDesDec(byte[] key, byte[] in) throws Exception { Key deskey = null;/*w ww . ja v a 2s.c o m*/ byte[] tdesKey = new byte[24]; if (key.length % 8 != 0) return null; if (key.length == 8) { System.arraycopy(key, 0, tdesKey, 0, 8); System.arraycopy(key, 0, tdesKey, 8, 8); System.arraycopy(key, 0, tdesKey, 16, 8); } if (key.length == 16) { System.arraycopy(key, 0, tdesKey, 0, 16); System.arraycopy(key, 0, tdesKey, 16, 8); } DESedeKeySpec spec = new DESedeKeySpec(tdesKey); SecretKeyFactory keyfactory = SecretKeyFactory.getInstance("desede"); deskey = keyfactory.generateSecret(spec); Cipher cipher = Cipher.getInstance("desede" + "/ECB/NoPadding"); cipher.init(Cipher.DECRYPT_MODE, deskey); byte[] bOut = cipher.doFinal(in); return bOut; }
From source file:Main.java
public static byte[] TDesEnc(byte[] key, byte[] in) throws Exception { Key deskey = null;/*from w w w. j a v a2 s .co m*/ byte[] tdesKey = new byte[24]; if (key.length % 8 != 0) return null; if (key.length == 8) { System.arraycopy(key, 0, tdesKey, 0, 8); System.arraycopy(key, 0, tdesKey, 8, 8); System.arraycopy(key, 0, tdesKey, 16, 8); } if (key.length == 16) { System.arraycopy(key, 0, tdesKey, 0, 16); System.arraycopy(key, 0, tdesKey, 16, 8); } DESedeKeySpec spec = new DESedeKeySpec(tdesKey); SecretKeyFactory keyfactory = SecretKeyFactory.getInstance("desede"); deskey = keyfactory.generateSecret(spec); Cipher cipher = Cipher.getInstance("desede" + "/ECB/NoPadding"); cipher.init(Cipher.ENCRYPT_MODE, deskey); byte[] bOut = cipher.doFinal(in); return bOut; }
From source file:cn.lynx.emi.license.GenerateLicense.java
private static final String encrypt(String key, String data) { byte[] corekey = Base64.decodeBase64(key); PKCS8EncodedKeySpec pkspec = new PKCS8EncodedKeySpec(corekey); try {//from www . j ava 2 s.c o m KeyFactory keyFactory = KeyFactory.getInstance("RSA"); Key privateKey = keyFactory.generatePrivate(pkspec); Cipher cipher = Cipher.getInstance(keyFactory.getAlgorithm()); cipher.init(Cipher.ENCRYPT_MODE, privateKey); byte[] encData = cipher.doFinal(data.getBytes("UTF-8")); System.out.println("after encrypt, len=" + encData.length); return Base64.encodeBase64String(encData); } catch (Exception e) { e.printStackTrace(); return null; } }
From source file:com.bytecode.util.Crypto.java
private static byte[] encrypt(String keystring, String message, int bits) throws Exception { byte[] encValue = null; SecureRandom random = new SecureRandom(); byte[] nonceBytes = new byte[8]; random.nextBytes(nonceBytes);//from w w w. ja v a 2s .c o m IvParameterSpec nonce = new IvParameterSpec(Arrays.copyOf(nonceBytes, 16)); Key key = generateKey(keystring, bits); Cipher c = Cipher.getInstance(ALGORITHM); c.init(Cipher.ENCRYPT_MODE, key, nonce); byte[] ciphertextWithoutNonce = c.doFinal(message.getBytes("UTF-8")); encValue = Arrays.copyOf(nonceBytes, nonceBytes.length + ciphertextWithoutNonce.length); for (int i = 0; i < ciphertextWithoutNonce.length; i++) { encValue[i + 8] = ciphertextWithoutNonce[i]; } return encValue; }
From source file:com.tesora.dve.common.PECryptoUtils.java
public static String decrypt(String str) throws PEException { if (StringUtils.isBlank(str)) return str; try {/*from ww w .j ava2 s.c o m*/ Cipher cipher = createCrypter(Cipher.DECRYPT_MODE); return new String(cipher.doFinal(Base64.decodeBase64(str)), "UTF8"); } catch (Exception e) { throw new PEException("Failed to decrypt '" + str + "'", e); } }