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.thoughtworks.go.server.util.EncryptionHelper.java
public static String encryptUsingAES(SecretKey secretKey, String dataToEncrypt) throws NoSuchPaddingException, NoSuchAlgorithmException, InvalidKeyException, BadPaddingException, IllegalBlockSizeException { Cipher aesCipher = Cipher.getInstance("AES"); aesCipher.init(Cipher.ENCRYPT_MODE, secretKey); byte[] byteCipherText = aesCipher.doFinal(dataToEncrypt.getBytes()); return Base64.getEncoder().encodeToString(byteCipherText); }
From source file:ie.peternagy.jcrypto.algo.AesWrapper.java
/** * Initialize the cipher/*from w w w . j a v a 2s .co m*/ * * @param isEncrypt - true >> encryption */ public void initCipher(boolean isEncrypt) { try { state = isEncrypt; cipher = Cipher.getInstance(CIPHER_ALGORITHM); cipher.init(isEncrypt ? Cipher.ENCRYPT_MODE : Cipher.DECRYPT_MODE, secretKey, new IvParameterSpec(iv)); } catch (GeneralSecurityException e) { System.err.println(e); throw new RuntimeException("Invalid environment, check max key size xx", e); } }
From source file:edu.tamu.tcat.crypto.spongycastle.SecureTokenImpl.java
@Override public String getToken(ByteBuffer content) throws TokenException { try {//www . j a v a 2 s . c o m byte[] token = createToken(content.slice()); byte[] iv = createIV(); Cipher cipher = createCipher(Cipher.ENCRYPT_MODE, iv); int outputSize = cipher.getOutputSize(token.length); byte[] encrypted = new byte[outputSize + (ivSize / 8)]; System.arraycopy(iv, 0, encrypted, 0, iv.length); cipher.doFinal(token, 0, token.length, encrypted, iv.length); String encoded = Base64.encodeBase64URLSafeString(encrypted); return encoded; } catch (NoSuchAlgorithmException e) { throw new TokenException("Missing algorithm", e); } catch (IllegalBlockSizeException e) { throw new TokenException( "Should never happen but is thrown because Sun/Oracle doesn't understand that encryption/decryption modes are different and should really have different APIs. This is a decrypt only problem", e); } catch (BadPaddingException e) { throw new TokenException( "Should never happen but is thrown because Sun/Oracle doesn't understand that encryption/decryption modes are different and should really have different APIs. This is a decrypt only problem", e); } catch (ShortBufferException e) { throw new TokenException("Should never happen", e); } }
From source file:edu.tamu.tcat.crypto.bouncycastle.SecureTokenImpl.java
@Override public String getToken(ByteBuffer content) throws TokenException { try {//w w w .ja v a 2s . c om byte[] token = createToken(content.slice()); byte[] iv = createIV(); Cipher cipher = createCipher(Cipher.ENCRYPT_MODE, iv); int outputSize = cipher.getOutputSize(token.length); // The token value returned contains the IV followed by the encrypted payload byte[] encrypted = new byte[outputSize + (ivSize / 8)]; System.arraycopy(iv, 0, encrypted, 0, iv.length); cipher.doFinal(token, 0, token.length, encrypted, iv.length); String encoded = Base64.encodeBase64URLSafeString(encrypted); return encoded; } catch (NoSuchAlgorithmException e) { throw new TokenException("Missing algorithm", e); } catch (IllegalBlockSizeException e) { throw new TokenException( "Should never happen but is thrown because Sun/Oracle doesn't understand that encryption/decryption modes are different and should really have different APIs. This is a decrypt only problem", e); } catch (BadPaddingException e) { throw new TokenException( "Should never happen but is thrown because Sun/Oracle doesn't understand that encryption/decryption modes are different and should really have different APIs. This is a decrypt only problem", e); } catch (ShortBufferException e) { throw new TokenException("Should never happen", e); } }
From source file:com.nubits.nubot.utils.Utils.java
/** * @param originalString/*from w w w . ja v a 2s. co m*/ * @param passphrase * @param pathToOutput * @return */ public static String encodeToFile(String originalString, String passphrase, String pathToOutput) { String encodedString = ""; MessageDigest digest; try { //System.out.println("Writing " +originalString +" to "+ pathToOutput +" with \npassphrase = "+passphrase); //Encapsule the passphrase in a 16bit SecretKeySpec key digest = MessageDigest.getInstance("SHA"); digest.update(passphrase.getBytes()); SecretKeySpec key = new SecretKeySpec(digest.digest(), 0, 16, "AES"); //Cypher the message Cipher aes = Cipher.getInstance("AES/ECB/PKCS5Padding"); aes.init(Cipher.ENCRYPT_MODE, key); byte[] ciphertext = aes.doFinal(originalString.getBytes()); encodedString = new String(ciphertext); FileUtils.writeByteArrayToFile(new File(pathToOutput), ciphertext); } catch (IOException | NoSuchAlgorithmException | NoSuchPaddingException | InvalidKeyException | IllegalBlockSizeException | BadPaddingException ex) { LOG.error(ex.toString()); } return encodedString; }
From source file:jeffaschenk.tomcat.zuul.util.KeyFileUtils.java
public void InitCiphers() throws NoSuchAlgorithmException, NoSuchProviderException, NoSuchPaddingException, InvalidKeyException, InvalidAlgorithmParameterException { //1. create the cipher using Bouncy Castle Provider encryptCipher = Cipher.getInstance("AES/CBC/PKCS5Padding", new BouncyCastleProvider()); //2. create the key SecretKey keyValue = new SecretKeySpec(key, "AES"); //3. create the IV AlgorithmParameterSpec IVspec = new IvParameterSpec(IV); //4. init the cipher encryptCipher.init(Cipher.ENCRYPT_MODE, keyValue, IVspec); //1 create the cipher decryptCipher = Cipher.getInstance("AES/CBC/PKCS5Padding", new BouncyCastleProvider()); //2. the key is already created //3. the IV is already created //4. init the cipher decryptCipher.init(Cipher.DECRYPT_MODE, keyValue, IVspec); }
From source file:com.springcryptoutils.core.cipher.asymmetric.Base64EncodedCiphererImpl.java
/** * Encrypts/decrypts a message based on the underlying mode of operation. * * @param message if in encryption mode, the clear-text message, otherwise * the base64 encoded message to decrypt * @return if in encryption mode, the base64 encoded encrypted message, * otherwise the decrypted message * @throws AsymmetricEncryptionException on runtime errors * @see #setMode(Mode)// w w w . j ava 2 s . c o m */ public String encrypt(String message) { try { final Cipher cipher = (((provider == null) || (provider.length() == 0)) ? Cipher.getInstance(algorithm) : Cipher.getInstance(algorithm, provider)); switch (mode) { case ENCRYPT: final byte[] messageAsByteArray = message.getBytes(charsetName); cipher.init(Cipher.ENCRYPT_MODE, key); return Base64.encodeBase64String(cipher.doFinal(messageAsByteArray)); case DECRYPT: final byte[] encryptedMessage = Base64.decodeBase64(message); cipher.init(Cipher.DECRYPT_MODE, key); return new String(cipher.doFinal(encryptedMessage), charsetName); default: return null; } } catch (Exception e) { throw new AsymmetricEncryptionException("error encrypting/decrypting message; mode=" + mode, e); } }
From source file:com.github.woki.payments.adyen.action.CSEUtil.java
public static Cipher rsaCipher(final String cseKeyText) throws NoSuchPaddingException, NoSuchAlgorithmException, InvalidKeyException, InvalidKeySpecException, IllegalArgumentException { String[] cseKeyParts = cseKeyText.split("\\|"); if (cseKeyParts.length != 2) { throw new InvalidKeyException("Invalid CSE Key: " + cseKeyText); }// w ww .j ava 2 s . c o m KeyFactory keyFactory = KeyFactory.getInstance("RSA"); BigInteger keyComponent1, keyComponent2; try { keyComponent1 = new BigInteger(cseKeyParts[1].toLowerCase(Locale.getDefault()), 16); keyComponent2 = new BigInteger(cseKeyParts[0].toLowerCase(Locale.getDefault()), 16); } catch (NumberFormatException e) { throw new InvalidKeyException("Invalid CSE Key: " + cseKeyText); } RSAPublicKeySpec pubKeySpec = new RSAPublicKeySpec(keyComponent1, keyComponent2); PublicKey pubKey = keyFactory.generatePublic(pubKeySpec); Cipher result = Cipher.getInstance("RSA/None/PKCS1Padding"); result.init(Cipher.ENCRYPT_MODE, pubKey); return result; }
From source file:com.anteam.demo.codec.cipher.symmetric.DESedeCoder.java
/** * Encodes a byte array and return the encoded data as a byte array. * * @param source Data to be encoded// ww w .ja v a2 s . c om * @return ?byte.source, null * @throws org.apache.commons.codec.EncoderException thrown if the Encoder encounters a failure condition during the encoding process. */ @Override public byte[] encode(byte[] source) throws EncoderException { if (source == null) { return null; } try { SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(ALGORITHM_NAME); SecretKey secretKey = keyFactory.generateSecret(keySpec); Cipher cipher = Cipher.getInstance(CIPHER_NAME); cipher.init(Cipher.ENCRYPT_MODE, secretKey, IvParameters); return cipher.doFinal(source); } catch (Exception e) { LOG.error(":" + key + ":" + source, e); throw new EncoderException(":" + key + ":" + source, e); } }
From source file:jp.primecloud.auto.common.component.PasswordEncryptor.java
/** * ?// ww w . j a va 2 s . c o m * @param originalString * @param keyString * @return */ public String encrypt(String orignalString, String keyString) { byte[] originalBytes = orignalString.getBytes(); byte[] keyBytes = keyString.getBytes(); SecretKey secretKey = new SecretKeySpec(keyBytes, ALGORITHM); try { chipher.init(Cipher.ENCRYPT_MODE, secretKey, ivParameterSpec); } catch (InvalidKeyException e) { throw new RuntimeException(e); } catch (InvalidAlgorithmParameterException e) { throw new RuntimeException(e); } byte[] encryptedBytes = null; try { encryptedBytes = chipher.doFinal(originalBytes); } catch (IllegalBlockSizeException e) { throw new RuntimeException(e); } catch (BadPaddingException e) { throw new RuntimeException(e); } return new String(Base64.encodeBase64(encryptedBytes)); }