List of usage examples for javax.crypto Cipher ENCRYPT_MODE
int ENCRYPT_MODE
To view the source code for javax.crypto Cipher ENCRYPT_MODE.
Click Source Link
From source file:com.redsqirl.workflow.utils.FileStream.java
private static byte[] encrypt(byte[] plaintext) throws Exception { SecretKey key = generateKey(); PBEParameterSpec pbeParamSpec = new PBEParameterSpec(salt, 42); Cipher cipher = Cipher.getInstance("PBEWithMD5AndDES"); cipher.init(Cipher.ENCRYPT_MODE, key, pbeParamSpec); return cipher.doFinal(plaintext); }
From source file:com.wms.studio.security.utils.Digests.java
public static byte[] desEncrypt(String pwd, byte[] key) throws Exception { // ?DESKeySpec DESKeySpec dks = new DESKeySpec(key); // ?DESKeySpec??SecretKey SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(DES); SecretKey securekey = keyFactory.generateSecret(dks); // Cipher??//from ww w. j a va 2 s.co m Cipher cipher = Cipher.getInstance(DES); // ?Cipher cipher.init(Cipher.ENCRYPT_MODE, securekey, random); return cipher.doFinal(pwd.getBytes()); }
From source file:com.mb.framework.util.SecurityUtil.java
/** * //from w w w . ja v a2 s. co m * This method is used for encrypt by using Algorithm - AES * * @param String * @return String * @throws Exception */ public static String encryptAES(String data) throws Exception { Key key = generateKeyAES(); // step 1 Cipher c = Cipher.getInstance(AES_ALGO); // step 2 c.init(Cipher.ENCRYPT_MODE, key); // step 3 byte[] encVal = c.doFinal(data.getBytes()); // step 4 String encryptedValue = new BASE64Encoder().encode(encVal); // step 5 return encryptedValue; }
From source file:com.aqnote.shared.cryptology.symmetric.AES.java
private static void generateCipher(String rawKey) { try {/*from ww w . j a v a 2s. c o m*/ SecretKeySpec keySpec = new SecretKeySpec(rawKey.getBytes(ENCODE_UTF_8), CIPHER_NAME); encodeCipher = Cipher.getInstance(CIPHER_NAME); encodeCipher.init(Cipher.ENCRYPT_MODE, keySpec); decodeCipher = Cipher.getInstance(CIPHER_NAME); byte iv[] = encodeCipher.getIV(); IvParameterSpec ivParameterSpec = new IvParameterSpec(iv); decodeCipher.init(Cipher.DECRYPT_MODE, keySpec, ivParameterSpec); } catch (InvalidKeyException e) { throw new RuntimeException(e); } catch (NoSuchAlgorithmException e) { throw new RuntimeException(e); } catch (NoSuchPaddingException e) { throw new RuntimeException(e); } catch (UnsupportedEncodingException e) { throw new RuntimeException(e); } catch (InvalidAlgorithmParameterException e) { e.printStackTrace(); } }
From source file:net.navasoft.madcoin.backend.services.security.Encrypter.java
/** * Encrypt.//from w w w. jav a 2s .com * * @param value * the value * @return the string * @since 5/08/2014, 08:03:33 PM */ public String encrypt(String value) { try { Cipher ecipher = Cipher.getInstance("DESede/CBC/PKCS5Padding", "SunJCE"); ecipher.init(Cipher.ENCRYPT_MODE, key, iv); if (value == null) return null; // Encode the string into bytes using utf-8 byte[] utf8 = value.getBytes("UTF8"); // Encrypt byte[] enc = ecipher.doFinal(utf8); // Encode bytes to base64 to get a string return new String(Base64.encodeBase64(enc), "UTF-8"); } catch (Exception e) { e.printStackTrace(); } return null; }
From source file:se.vgregion.portal.cs.util.CryptoUtilImpl.java
/** * Encrypt a value and generate a keyfile. if the keyfile is not found then a new one is created * // w ww . jav a2s. c o m * @param value * - value to be encrypted * @throws GeneralSecurityException * - security exception * @return Encrypted value */ @Override public String encrypt(String value) throws GeneralSecurityException { if (!keyFile.exists()) { KeyGenerator keyGen = KeyGenerator.getInstance(AES); keyGen.init(KEY_SIZE); SecretKey sk = keyGen.generateKey(); FileWriter fw = null; try { fw = new FileWriter(keyFile); fw.write(byteArrayToHexString(sk.getEncoded())); fw.flush(); } catch (IOException e) { e.printStackTrace(); } finally { try { fw.close(); } catch (IOException e) { e.printStackTrace(); } } } SecretKeySpec sks = getSecretKeySpec(); Cipher cipher = Cipher.getInstance(AES); cipher.init(Cipher.ENCRYPT_MODE, sks, cipher.getParameters()); byte[] encrypted = cipher.doFinal(value.getBytes()); return byteArrayToHexString(encrypted); }
From source file:com.adaptris.security.password.AesCrypto.java
public String encode(String plainText, String charset) throws PasswordException { String result = null;/*from www. j a v a 2s . c o m*/ try { KeyGenerator kg = KeyGenerator.getInstance(ALG); kg.init(KEY_LEN, SecurityUtil.getSecureRandom()); SecretKey sessionKey = kg.generateKey(); Cipher dataCipher = Cipher.getInstance(CIPHER); dataCipher.init(Cipher.ENCRYPT_MODE, sessionKey); byte[] encryptedBody = dataCipher.doFinal(seed(plainText, charset)); Output output = new Output(); output.setSessionKey(sessionKey.getEncoded()); output.setSessionVector(dataCipher.getIV()); output.setEncryptedData(encryptedBody); result = Password.PORTABLE_PASSWORD + output.write(); } catch (Exception e) { throw new PasswordException(e); } return result; }
From source file:com.bamboocloud.im.provisioner.json.crypto.simple.SimpleEncryptor.java
/** * Encrypts with a symmetric cipher.//from www . jav a 2 s . c om * * @param value the value to be encrypted. * @return the encrypted value. * @throws GeneralSecurityException if a cryptographic operation failed. * @throws IOException if an I/O exception occurred. */ private Object symmetric(Object object) throws GeneralSecurityException, IOException { Cipher symmetric = Cipher.getInstance(cipher); symmetric.init(Cipher.ENCRYPT_MODE, key); String data = Base64.encodeBase64String(symmetric.doFinal(mapper.writeValueAsBytes(object))); byte[] iv = symmetric.getIV(); HashMap<String, Object> result = new HashMap<String, Object>(); result.put("cipher", this.cipher); result.put("key", this.alias); result.put("data", data); if (iv != null) { result.put("iv", Base64.encodeBase64String(iv)); } return result; }
From source file:com.greenline.hrs.admin.util.encrypt.DESUtil.java
/** * Description ?/* w w w . jav a 2 s .c o m*/ * * @param data * @param key byte * @return * @throws Exception */ private static byte[] encrypt(byte[] data, byte[] key) throws GeneralSecurityException { // ???? SecureRandom sr = new SecureRandom(); // ?DESKeySpec DESKeySpec dks = new DESKeySpec(key); // ?DESKeySpec??SecretKey SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(DES); SecretKey securekey = keyFactory.generateSecret(dks); // Cipher?? Cipher cipher = Cipher.getInstance(DES); // ?Cipher cipher.init(Cipher.ENCRYPT_MODE, securekey, sr); return cipher.doFinal(data); }
From source file:cherry.goods.crypto.RSASignatureTest.java
private RSASignature create2(char[] password) throws Exception { KeyPairGenerator keygen = KeyPairGenerator.getInstance("RSA"); keygen.initialize(2048);/*from ww w . j a v a 2 s .c om*/ KeyPair key = keygen.generateKeyPair(); String pbeAlgName = "PBEWithMD5AndDES"; PBEKeySpec pbeKeySpec = new PBEKeySpec(password); PBEParameterSpec pbeParamSpec = new PBEParameterSpec(RandomUtils.nextBytes(8), 20); SecretKey pbeKey = SecretKeyFactory.getInstance(pbeAlgName).generateSecret(pbeKeySpec); AlgorithmParameters pbeParam = AlgorithmParameters.getInstance(pbeAlgName); pbeParam.init(pbeParamSpec); Cipher cipher = Cipher.getInstance(pbeAlgName); cipher.init(Cipher.ENCRYPT_MODE, pbeKey, pbeParam); EncryptedPrivateKeyInfo encryptedKeyInfo = new EncryptedPrivateKeyInfo(pbeParam, cipher.doFinal(key.getPrivate().getEncoded())); RSASignature impl = new RSASignature(); impl.setAlgorithm("SHA256withRSA"); impl.setPublicKeyBytes(key.getPublic().getEncoded()); impl.setPrivateKeyBytes(encryptedKeyInfo.getEncoded(), password); return impl; }