List of usage examples for javax.crypto Cipher doFinal
public final byte[] doFinal(byte[] input) throws IllegalBlockSizeException, BadPaddingException
From source file:io.manasobi.utils.CryptoUtils.java
/** * .//from ww w . ja v a2 s. co m * * @param keyHex generateHexKey ? ? ?? Hex ? ? * @param data ?? byte * * @return ? ?? */ public static byte[] encryptByDES(String keyHex, byte[] data) { SecretKey key = getSecretDESKeyFromHex(keyHex); byte[] encryptedData = null; try { Cipher cipher = Cipher.getInstance(ALGORITHM); cipher.init(Cipher.ENCRYPT_MODE, key); encryptedData = cipher.doFinal(data); } catch (Exception e) { throw new CryptoUtilsException(e.getMessage()); } return encryptedData; }
From source file:io.manasobi.utils.CryptoUtils.java
/** * .//from w w w. ja v a2s.co m * * @param keyHex generateHexKey ? ? ?? Hex ? ? * @param data ? ? * * @return ? ?? byte */ public static byte[] decryptByDES(String keyHex, byte[] data) { SecretKey key = getSecretDESKeyFromHex(keyHex); byte[] decryptedData = null; try { Cipher cipher = Cipher.getInstance(ALGORITHM); cipher.init(Cipher.DECRYPT_MODE, key); decryptedData = cipher.doFinal(data); } catch (Exception e) { throw new CryptoUtilsException(e.getMessage()); } return decryptedData; }
From source file:com.os.util.PasswordDecoderEncoder.java
public static String encrypt(String plainPassword) throws Exception { Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding"); key = convertHexToBytes(keyst);//from w w w . j ava 2 s.com final SecretKeySpec secretKey = new SecretKeySpec(key, "AES"); cipher.init(Cipher.ENCRYPT_MODE, secretKey); final String encryptedString = Base64.encodeBase64String(cipher.doFinal(plainPassword.getBytes("UTF8"))); System.out.println(encryptedString); String passwordEncrypted = encryptedString.trim(); return passwordEncrypted; }
From source file:com.cherong.mock.common.base.util.EncryptionUtil.java
/** * DES bytKey8?//from ww w .j a v a2s. co m * * @param bytP * @param bytKey * @return * @throws Exception */ public static byte[] encryptByDES(byte[] bytP, byte[] bytKey) throws Exception { DESKeySpec dks = new DESKeySpec(bytKey); SecretKeyFactory skf = SecretKeyFactory.getInstance("DES"); SecretKey sk = skf.generateSecret(dks); Cipher cip = Cipher.getInstance(DES_CIPHER_ALGORITHM); cip.init(Cipher.ENCRYPT_MODE, sk); return cip.doFinal(bytP); }
From source file:com.cherong.mock.common.base.util.EncryptionUtil.java
/** * DES bytKey8?//from w ww.jav a 2s.co m * * @param bytE * @param bytKey * @return * @throws Exception */ public static byte[] decryptByDES(byte[] bytE, byte[] bytKey) throws Exception { DESKeySpec desKS = new DESKeySpec(bytKey); SecretKeyFactory skf = SecretKeyFactory.getInstance("DES"); SecretKey sk = skf.generateSecret(desKS); Cipher cip = Cipher.getInstance(DES_CIPHER_ALGORITHM); cip.init(Cipher.DECRYPT_MODE, sk); return cip.doFinal(bytE); }
From source file:com.example.license.DESUtil.java
/** * ?//from ww w.j a va 2 s . c o m * * @param data * ? * @param key * * @return ?? */ public static String encrypt(String data, String key) throws Exception { Key deskey = keyGenerator(key); // Cipher?? Cipher cipher = Cipher.getInstance(CIPHER_ALGORITHM); SecureRandom random = new SecureRandom(); // ?Cipher? cipher.init(Cipher.ENCRYPT_MODE, deskey, random); byte[] results = cipher.doFinal(data.getBytes("UTF-8")); // http://tripledes.online-domain-tools.com/?? for (int i = 0; i < results.length; i++) { System.out.print(results[i] + " "); } System.out.println(); // ??Base64? return Base64.encodeBase64String(results); }
From source file:Logic.security.java
public static String symmetricEncrypt(String text, String secretKey) { byte[] raw;/*from w ww. j av a2 s .c o m*/ String encryptedString; SecretKeySpec skeySpec; byte[] encryptText = text.getBytes(); Cipher cipher; try { raw = Base64.decodeBase64(secretKey); skeySpec = new SecretKeySpec(raw, "AES"); cipher = Cipher.getInstance("AES"); cipher.init(Cipher.ENCRYPT_MODE, skeySpec); encryptedString = Base64.encodeBase64String(cipher.doFinal(encryptText)); } catch (Exception e) { e.printStackTrace(); return "Error"; } return encryptedString; }
From source file:com.ec2box.manage.util.EncryptionUtil.java
/** * return decrypted value of encrypted string * * @param str encrypted string/*from w ww . j a v a2s . c o m*/ * @return decrypted string */ public static String decrypt(String str) { String retVal = null; if (str != null && str.length() > 0) { try { Cipher c = Cipher.getInstance("AES"); c.init(Cipher.DECRYPT_MODE, new SecretKeySpec(key, "AES")); byte[] decodedVal = Base64.decodeBase64(str.getBytes()); retVal = new String(c.doFinal(decodedVal)); } catch (Exception ex) { log.error(ex.toString(), ex); } } return retVal; }
From source file:Main.java
public static String encryptData(String password, String plaintextData) throws Exception { // Thank you Mr. Nelenkov String maybeThisHelps = "http://nelenkov.blogspot.com/2012/04/using-password-based-encryption-on.html"; Log.v(TAG, maybeThisHelps);//from w w w . ja va2 s . c o m int iterationCount = 100; //because Polaroid int keyLength = 256; int saltLength = keyLength; SecureRandom random = new SecureRandom(); byte[] salt = new byte[saltLength]; random.nextBytes(salt); KeySpec keySpec = new PBEKeySpec(password.toCharArray(), salt, iterationCount, keyLength); SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1"); byte[] keyBytes = keyFactory.generateSecret(keySpec).getEncoded(); SecretKey key = new SecretKeySpec(keyBytes, "AES"); Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding"); byte[] iv = new byte[cipher.getBlockSize()]; random.nextBytes(iv); IvParameterSpec ivParams = new IvParameterSpec(iv); cipher.init(Cipher.ENCRYPT_MODE, key, ivParams); byte[] ciphertext = cipher.doFinal(plaintextData.getBytes("UTF-8")); String ivToString = new String(Base64.encode(iv, 0)); String saltToString = new String(Base64.encode(salt, 0)); String ciphertextToString = new String(Base64.encode(ciphertext, 0)); Log.d(TAG, ivToString + "]" + saltToString + "]" + ciphertextToString); return (ivToString + "]" + saltToString + "]" + ciphertextToString).replace("\n", ""); }
From source file:com.servoy.j2db.util.SecuritySupport.java
@SuppressWarnings("nls") public static String encryptUrlSafe(Settings settings, String value) throws Exception { if (value == null) return value; Cipher cipher = Cipher.getInstance("DESede"); cipher.init(Cipher.ENCRYPT_MODE, SecuritySupport.getCryptKey(settings)); return Base64.encodeBase64URLSafeString(cipher.doFinal(value.getBytes())); }