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.drisoftie.cwdroid.util.CredentialUtils.java
private static byte[] encrypt(byte[] key, byte[] clear) throws NoSuchPaddingException, NoSuchAlgorithmException, InvalidKeyException, BadPaddingException, IllegalBlockSizeException { SecretKeySpec skeySpec = new SecretKeySpec(key, AES); Cipher cipher = getDefaultCipher(); cipher.init(Cipher.ENCRYPT_MODE, skeySpec); return cipher.doFinal(clear); }
From source file:com.dragoniade.encrypt.EncryptionHelper.java
private static Cipher getEncrypter(String seed) { byte[] byteKey = getSeed(seed); for (int i = 0; i < algorithmes.length; i++) { String algorithm = algorithmes[i]; try {/*from ww w. j ava 2 s .co m*/ SecretKeySpec keySpec = new SecretKeySpec(byteKey, algorithm); Cipher encrypt = Cipher.getInstance(algorithm); encrypt.init(Cipher.ENCRYPT_MODE, keySpec); return encrypt; } catch (Exception e) { continue; } } return null; }
From source file:com.vmware.o11n.plugin.crypto.service.CryptoRSAService.java
/** * RSA Encryption/*from w w w.ja v a2 s.com*/ * * @param pemKey RSA Key (Public or Private, Public will be derived from Private) * @param dataB64 Data encoded with Base64 to encrypt * @return Encrypted data Base64 encoded * @throws IOException * @throws NoSuchAlgorithmException * @throws InvalidKeySpecException * @throws NoSuchPaddingException * @throws InvalidKeyException * @throws IllegalBlockSizeException * @throws BadPaddingException */ public String encrypt(String pemKey, String dataB64) throws IOException, NoSuchAlgorithmException, InvalidKeySpecException, NoSuchPaddingException, InvalidKeyException, IllegalBlockSizeException, BadPaddingException { String encryptedB64 = null; PublicKey publicKey = null; Key key = null; try { key = CryptoUtil.getKey(pemKey); //can be private or public } catch (IOException e) { //try to fix key: key = CryptoUtil.getKey(CryptoUtil.fixPemString(pemKey)); } if (key instanceof RSAPublicKey) { publicKey = (RSAPublicKey) key; } else if (key instanceof RSAPrivateCrtKey) { RSAPrivateCrtKey privateKey = (RSAPrivateCrtKey) key; publicKey = CryptoUtil.getPublicFromPrivate(privateKey); } else { throw new IllegalArgumentException("Unknown key object type: " + key.getClass().getName()); } Cipher cipher = Cipher.getInstance(CIPHER_TRANSFORMATION); cipher.init(Cipher.ENCRYPT_MODE, publicKey); encryptedB64 = Base64.encodeBase64String(cipher.doFinal(Base64.decodeBase64(dataB64))); return encryptedB64; }
From source file:de.openflorian.crypt.provider.BlowfishCipher.java
@Override public String encrypt(String str) throws GeneralSecurityException { if (key == null || key.isEmpty()) throw new IllegalStateException("The Blowfish Secret is not set or is length=0."); if (str == null) return null; try {/*from w ww . j av a 2 s.com*/ SecretKeySpec keySpec; keySpec = new SecretKeySpec(key.getBytes("UTF8"), "Blowfish"); Cipher cipher = Cipher.getInstance("Blowfish/ECB/PKCS5Padding"); cipher.init(Cipher.ENCRYPT_MODE, keySpec); return new String(new Base64().encode(cipher.doFinal(str.getBytes("UTF8")))).trim(); } catch (Exception e) { log.error(e.getMessage(), e); throw new GeneralSecurityException(e.getMessage(), e); } }
From source file:de.codesourcery.eve.apiclient.utils.PasswordCipherProvider.java
@Override public Cipher createCipher(boolean decrypt) { final char[] password = getPassword(); if (ArrayUtils.isEmpty(password)) { log.warn("createCipher(): No password , returning NULL cipher."); return new NullCipher(); }//from w w w. jav a 2s . c o m try { final int iterations = 20; final byte[] salt = new byte[] { (byte) 0xab, (byte) 0xfe, 0x03, 0x47, (byte) 0xde, (byte) 0x99, (byte) 0xff, 0x1c }; final PBEParameterSpec pbeSpec = new PBEParameterSpec(salt, iterations); final PBEKeySpec spec = new PBEKeySpec(password, salt, iterations); final SecretKeyFactory fac = SecretKeyFactory.getInstance("PBEWithMD5andDES"); final SecretKey secretKey; try { secretKey = fac.generateSecret(spec); } catch (InvalidKeySpecException e) { throw e; } final Cipher cipher = Cipher.getInstance("PBEWithMD5andDES"); if (decrypt) { cipher.init(Cipher.DECRYPT_MODE, secretKey, pbeSpec); } else { cipher.init(Cipher.ENCRYPT_MODE, secretKey, pbeSpec); } return cipher; } catch (Exception e) { throw new RuntimeException(e); } }
From source file:org.edeoliveira.oauth2.dropwizard.oauth2.auth.CookieEncrypter.java
public String encode(String content) throws Exception { Cipher cipher = Cipher.getInstance(CIPHER_ALGORITHM); cipher.init(Cipher.ENCRYPT_MODE, keySpec); byte[] output = cipher.doFinal(content.getBytes(UTF8)); return Base64.encodeBase64String(output); }
From source file:edu.tamu.tcat.crypto.bouncycastle.SecureTokenImpl.java
/** * Create a new token generator/parser using an encryption key. This attempts to fail early by creating a cipher in the constructor. * @param keyBytes The encryption key. ATM, this uses AES, so 128, 194, or 256 bit * @throws TokenException Thrown if the key or IV are not properly base64 encoded or the cipher cannot otherwise be created. *///from w ww . j a v a 2 s .c o m public SecureTokenImpl(byte[] keyBytes) throws TokenException { key = new SecretKeySpec(keyBytes, "AES"); createCipher(Cipher.ENCRYPT_MODE, createIV()); }
From source file:de.teamgrit.grit.util.mailer.EncryptorDecryptor.java
/** * Encrypts a given string./*from w w w .ja v a 2s. co m*/ * * @param stringToEncrypt * : this is the string that shall be encrypted. * @return : the encrypted String or null in case of fail. : when * encrypting fails. * @throws NoSuchPaddingException * @throws NoSuchAlgorithmException * @throws InvalidKeyException * @throws BadPaddingException * @throws IllegalBlockSizeException */ public String encrypt(String stringToEncrypt) throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, IllegalBlockSizeException, BadPaddingException { // get cryptographic cipher with the requested transformation, AES // (advanced encryption standard) algorithm with ECB (electronic // code book) mode and PKCS5Padding (schema to pad cleartext to be // multiples of 8-byte blocks) padding. Cipher cipher; cipher = Cipher.getInstance("AES/ECB/PKCS5Padding"); // use the provided secret key for encryption using AES final SecretKeySpec secretKey = new SecretKeySpec(m_key, "AES"); // initialize cryptographic cipher with encryption mode cipher.init(Cipher.ENCRYPT_MODE, secretKey); // encode the encrypted string in base 64 final String encryptedString = Base64.encodeBase64String(cipher.doFinal(stringToEncrypt.getBytes())); return encryptedString; }
From source file:com.google.feedserver.util.EncryptionUtil.java
/** * Encrypts the given string using a symmetric key algorithm * // w w w . ja va 2 s . c o m * @param input The input * @return The encrypted string with hex representation * @throws InvalidKeyException * @throws BadPaddingException * @throws IllegalBlockSizeException * @throws NoSuchPaddingException * @throws NoSuchAlgorithmException */ public String encrypt(String input) throws InvalidKeyException, BadPaddingException, IllegalBlockSizeException, NoSuchAlgorithmException, NoSuchPaddingException { Cipher cipher = Cipher.getInstance(algorithm); cipher.init(Cipher.ENCRYPT_MODE, key); byte[] inputBytes = input.getBytes(); byte[] encryptedBytes = cipher.doFinal(inputBytes); String encryptedValue = new String(Hex.encodeHex(encryptedBytes)); return encryptedValue; }
From source file:com.doplgangr.secrecy.filesystem.encryption.AES_ECB_Crypter.java
@Override public CipherOutputStream getCipherOutputStream(File file, String outputFileName) throws SecrecyCipherStreamException, FileNotFoundException { Cipher c;// w w w . ja v a2 s .c o m try { c = Cipher.getInstance(mode); } catch (NoSuchAlgorithmException e) { throw new SecrecyCipherStreamException("Encryption algorithm not found!"); } catch (NoSuchPaddingException e) { throw new SecrecyCipherStreamException("Selected padding not found!"); } try { c.init(Cipher.ENCRYPT_MODE, aesKey); } catch (InvalidKeyException e) { throw new SecrecyCipherStreamException("Invalid encryption key!"); } String filename = Base64Coder.encodeString(FilenameUtils.removeExtension(file.getName())) + "." + FilenameUtils.getExtension(file.getName()); File outputFile = new File(vaultPath + "/" + filename); BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(new FileOutputStream(outputFile), Config.BLOCK_SIZE); return new CipherOutputStream(bufferedOutputStream, c); }