List of usage examples for javax.crypto Cipher init
public final void init(int opmode, Certificate certificate) throws InvalidKeyException
From source file:com.liferay.util.Encryptor.java
public static String decrypt(Key key, String encryptedString) throws EncryptorException { try {/*from www. j a va 2 s . c om*/ Security.addProvider(getProvider()); Cipher cipher = Cipher.getInstance(key.getAlgorithm()); cipher.init(Cipher.DECRYPT_MODE, key); byte[] encryptedBytes = Base64.decode(encryptedString); byte[] decryptedBytes = cipher.doFinal(encryptedBytes); String decryptedString = new String(decryptedBytes, ENCODING); return decryptedString; } catch (Exception e) { throw new EncryptorException(e); } }
From source file:com.liferay.util.Encryptor.java
public static String encrypt(Key key, String plainText) throws EncryptorException { try {/*from w w w . j a v a 2 s .co m*/ Security.addProvider(getProvider()); Cipher cipher = Cipher.getInstance(key.getAlgorithm()); cipher.init(Cipher.ENCRYPT_MODE, key); byte[] decryptedBytes = plainText.getBytes(ENCODING); byte[] encryptedBytes = cipher.doFinal(decryptedBytes); String encryptedString = Base64.encode(encryptedBytes); return encryptedString; } catch (Exception e) { throw new EncryptorException(e); } }
From source file:com.hernandez.rey.crypto.TripleDES.java
/** * Use the specified TripleDES key to encrypt bytes from the input stream and write them to the output stream. This * method uses CipherOutputStream to perform the encryption and write bytes at the same time. * /*from w w w. j ava2 s .c om*/ * @param key the key to use for encryption * @param in the input stream to encrypt * @param out the encrypted output stream * @throws NoSuchAlgorithmException * @throws InvalidKeyException * @throws NoSuchPaddingException * @throws IOException */ public static void encrypt(final SecretKey key, final InputStream in, final OutputStream out) throws NoSuchAlgorithmException, InvalidKeyException, NoSuchPaddingException, IOException { // Create and initialize the encryption engine final Cipher cipher = Cipher.getInstance("DESede"); cipher.init(Cipher.ENCRYPT_MODE, key); // Create a special output stream to do the work for us final CipherOutputStream cos = new CipherOutputStream(out, cipher); // Read from the input and write to the encrypting output stream final byte[] buffer = new byte[2048]; int bytesRead; while ((bytesRead = in.read(buffer)) != -1) { cos.write(buffer, 0, bytesRead); } cos.close(); // For extra security, don't leave any plaintext hanging around memory. java.util.Arrays.fill(buffer, (byte) 0); }
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. ja v a2s . c om 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.os.util.PasswordDecoderEncoder.java
public static String decrypt(String encryptPassword) throws Exception { Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding"); key = convertHexToBytes(keyst);/*from www . j a v a 2s . c o m*/ final SecretKeySpec secretKey = new SecretKeySpec(key, "AES"); cipher.init(Cipher.DECRYPT_MODE, secretKey); String encryptedString = new String(cipher.doFinal(Base64.decodeBase64(encryptPassword.getBytes())), "UTF-8"); System.out.println(encryptedString); String passwordDecrypted = encryptedString.trim(); return passwordDecrypted; }
From source file:com.cl.roadshow.crypto.AESCtr.java
/** * Generate encryption key based on provided keystring. * // w w w .j ava 2 s .c om * @param keystring * @param bits * @return Encryption key * @throws Exception */ private static Key generateKey(String keystring, int bits) throws Exception { byte[] keyBytes = new byte[bits]; byte[] key = new byte[bits]; for (int i = 0; i < bits; i++) { //keyBytes[i] = (byte) keystring.codePointAt(i); // ??http://www.movable-type.co.uk/scripts/aes.html ?key keyBytes[i] = (byte) 0; } SecretKey secretKey = new SecretKeySpec(keyBytes, "AES"); Cipher cipher = Cipher.getInstance("AES/ECB/NoPadding"); cipher.init(Cipher.ENCRYPT_MODE, secretKey); key = cipher.doFinal(keyBytes); // Expand key to original length of keybytes for (int i = 0; i < bits - 16; i++) { key[16 + i] = key[i]; } return new SecretKeySpec(key, "AES"); }
From source file:hh.learnj.test.license.test.rsacoder.RSACoder.java
/** * ?/*from w w w . java 2s . com*/ * * @param data * ? * @param key * * @return byte[] ? */ public static byte[] decryptByPrivateKey(byte[] data, byte[] key) throws Exception { // ?? PKCS8EncodedKeySpec pkcs8KeySpec = new PKCS8EncodedKeySpec(key); KeyFactory keyFactory = KeyFactory.getInstance(KEY_ALGORITHM); // ?? PrivateKey privateKey = keyFactory.generatePrivate(pkcs8KeySpec); // ? Cipher cipher = Cipher.getInstance(keyFactory.getAlgorithm()); cipher.init(Cipher.DECRYPT_MODE, privateKey); return cipher.doFinal(data); }
From source file:hh.learnj.test.license.test.rsacoder.RSACoder.java
/** * ?// w w w . j av a 2 s . c o m * * @param data? * @param key * * @return byte[] ? */ public static byte[] encryptByPrivateKey(byte[] data, byte[] key) throws Exception { // ?? PKCS8EncodedKeySpec pkcs8KeySpec = new PKCS8EncodedKeySpec(key); KeyFactory keyFactory = KeyFactory.getInstance(KEY_ALGORITHM); // ?? PrivateKey privateKey = keyFactory.generatePrivate(pkcs8KeySpec); // ? Cipher cipher = Cipher.getInstance(keyFactory.getAlgorithm()); cipher.init(Cipher.ENCRYPT_MODE, privateKey); return cipher.doFinal(data); }
From source file:hh.learnj.test.license.test.rsacoder.RSACoder.java
/** * /*from ww w . j a v a 2 s .c o m*/ * * @param data * ? * @param key * * @return byte[] ? */ public static byte[] decryptByPublicKey(byte[] data, byte[] key) throws Exception { // KeyFactory keyFactory = KeyFactory.getInstance(KEY_ALGORITHM); // ? // ??? X509EncodedKeySpec x509KeySpec = new X509EncodedKeySpec(key); // PublicKey pubKey = keyFactory.generatePublic(x509KeySpec); // ? Cipher cipher = Cipher.getInstance(keyFactory.getAlgorithm()); cipher.init(Cipher.DECRYPT_MODE, pubKey); return cipher.doFinal(data); }
From source file:hh.learnj.test.license.test.rsacoder.RSACoder.java
/** * //from w w w . j a v a2 s . c o m * * @param data? * @param key * * @return byte[] ? */ public static byte[] encryptByPublicKey(byte[] data, byte[] key) throws Exception { // KeyFactory keyFactory = KeyFactory.getInstance(KEY_ALGORITHM); // ? // ??? X509EncodedKeySpec x509KeySpec = new X509EncodedKeySpec(key); // PublicKey pubKey = keyFactory.generatePublic(x509KeySpec); // ? Cipher cipher = Cipher.getInstance(keyFactory.getAlgorithm()); cipher.init(Cipher.ENCRYPT_MODE, pubKey); return cipher.doFinal(data); }