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: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 hexKey The encryption key, hex encoded. 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 a2 s. c o m public SecureTokenImpl(String hexKey) throws TokenException { try { byte[] keyBytes = Hex.decodeHex(hexKey.toCharArray()); key = new SecretKeySpec(keyBytes, "AES"); createCipher(Cipher.ENCRYPT_MODE, createIV()); } catch (DecoderException e) { throw new TokenException("Invalid Key or IV", e); } }
From source file:com.cubusmail.server.mail.security.MailPasswordEncryptor.java
public byte[] encryptPassowrd(String password) { Cipher cipher;/*from w w w. ja va 2 s.c om*/ try { cipher = Cipher.getInstance(this.algorithm); cipher.init(Cipher.ENCRYPT_MODE, this.keyPair.getPublic()); ByteArrayOutputStream baosEncryptedData = new ByteArrayOutputStream(); CipherOutputStream cos = new CipherOutputStream(baosEncryptedData, cipher); cos.write(password.getBytes("UTF-8")); cos.flush(); cos.close(); return baosEncryptedData.toByteArray(); } catch (Exception e) { log.error(e.getMessage(), e); throw new IllegalStateException(e.getMessage(), e); } }
From source file:com.telefonica.euro_iaas.sdc.util.RSASignerImpl.java
/** * {@inheritDoc}/*from w w w . j a va 2 s . c o m*/ */ public String sign(String message, File pemFile) { try { Security.addProvider(new BouncyCastleProvider()); PrivateKey privateKey = readKeyPair(pemFile, "".toCharArray()).getPrivate(); Cipher cipher = Cipher.getInstance("RSA"); cipher.init(Cipher.ENCRYPT_MODE, privateKey); byte[] digest = cipher.doFinal(message.getBytes()); return Base64.encodeBase64String(digest); } catch (IOException e) { throw new SdcRuntimeException(e); } catch (NoSuchAlgorithmException e) { throw new SdcRuntimeException(e); } catch (InvalidKeyException e) { throw new SdcRuntimeException(e); } catch (NoSuchPaddingException e) { throw new SdcRuntimeException(e); } catch (IllegalBlockSizeException e) { throw new SdcRuntimeException(e); } catch (BadPaddingException e) { throw new SdcRuntimeException(e); } }
From source file:cipher.UsableCipher.java
public byte[] encrypt(String plainText) throws Exception { initCipher(Cipher.ENCRYPT_MODE); byte[] plainTextBytes = bytes(plainText); return cipher.doFinal(plainTextBytes); }
From source file:de.extra.client.plugins.outputplugin.crypto.ExtraCryptoUtil.java
/** Encrypts the specified string, using the specified secret key. */ private static String encrypt(String sValue, String secretKey) { if (secretKey == null) { secretKey = SYM_KEY_STR;//from w w w . j a v a2 s . co m } if (sValue == null || sValue.equals("")) { return ""; } String textEncode = null; Cipher encryptCipher = null; try { SecretKeySpec skeySpec = decodeKey(secretKey); encryptCipher = Cipher.getInstance(TRANSFORMATION); encryptCipher.init(Cipher.ENCRYPT_MODE, skeySpec); byte[] plainText = sValue.trim().getBytes(CHARSET); // do the actual encryption byte[] cipherText = encryptCipher.doFinal(plainText); // Changed to encode() to avoid <cr> on end of string // textEncode = base64Encoder.encodeBuffer(cipherText); textEncode = new Base64().encodeAsString(cipherText); } catch (Exception e) { e.printStackTrace(System.err); } return textEncode; }
From source file:com.almende.util.EncryptionUtil.java
/** * Encrypt a string.//from w w w. j a v a 2 s . c o m * * @param text * the text * @return encryptedText * @throws InvalidKeyException * the invalid key exception * @throws InvalidAlgorithmParameterException * the invalid algorithm parameter exception * @throws NoSuchAlgorithmException * the no such algorithm exception * @throws InvalidKeySpecException * the invalid key spec exception * @throws NoSuchPaddingException * the no such padding exception * @throws IllegalBlockSizeException * the illegal block size exception * @throws BadPaddingException * the bad padding exception * @throws UnsupportedEncodingException * the unsupported encoding exception */ public static String encrypt(final String text) throws InvalidKeyException, InvalidAlgorithmParameterException, NoSuchAlgorithmException, InvalidKeySpecException, NoSuchPaddingException, IllegalBlockSizeException, BadPaddingException, UnsupportedEncodingException { final PBEParameterSpec pbeParamSpec = new PBEParameterSpec(S, C); final PBEKeySpec pbeKeySpec = new PBEKeySpec(P); final SecretKeyFactory keyFac = SecretKeyFactory.getInstance(ENC); final SecretKey pbeKey = keyFac.generateSecret(pbeKeySpec); final Cipher pbeCipher = Cipher.getInstance(ENC); pbeCipher.init(Cipher.ENCRYPT_MODE, pbeKey, pbeParamSpec); final byte[] encryptedText = pbeCipher.doFinal(text.getBytes("UTF-8")); return Base64.encodeBase64String(encryptedText); }
From source file:gov.nih.nci.security.util.AESEncryption.java
public AESEncryption(String passphrase, boolean isMD5Hash) throws EncryptionException { try {//w w w . j a va2s . com this.provider = new BouncyCastleProvider(); SecretKeySpec skey = getSKeySpec(passphrase, isMD5Hash); encryptCipher = Cipher.getInstance(AES_ENCRYPTION_SCHEME, provider); decryptCipher = Cipher.getInstance(AES_ENCRYPTION_SCHEME, provider); encryptCipher.init(Cipher.ENCRYPT_MODE, skey); AlgorithmParameters ap = encryptCipher.getParameters(); decryptCipher.init(Cipher.DECRYPT_MODE, skey, ap); } catch (NoSuchAlgorithmException e) { throw new EncryptionException(e); } catch (NoSuchPaddingException e) { throw new EncryptionException(e); } catch (InvalidKeyException e) { throw new EncryptionException(e); } catch (InvalidAlgorithmParameterException e) { throw new EncryptionException(e); } }
From source file:Crypt.java
private String encryptByDES(String str) throws Exception { Cipher c;/*from w w w. j av a 2s . c o m*/ try { c = Cipher.getInstance("DES/ECB/PKCS5Padding"); c.init(Cipher.ENCRYPT_MODE, secretKey); byte[] encrypted = c.doFinal(str.getBytes()); // convert into hexadecimal number, and return as character string. String result = ""; for (int i = 0; i < encrypted.length; i++) { result += byte2HexStr(encrypted[i]); } return result; } catch (InvalidKeyException e) { log.error("The information of the private key may be broken.", e); throw e; } catch (IllegalBlockSizeException e) { log.error("The length of data is unjust.", e); throw e; } }
From source file:com.thoughtworks.go.security.AESEncrypter.java
@Override public String encrypt(String plainText) throws CryptoException { try {//w ww . ja v a2 s . co m byte[] initializationVector = getIvProviderInstance().createIV(); Cipher encryptCipher = Cipher.getInstance("AES/CBC/PKCS5Padding"); encryptCipher.init(Cipher.ENCRYPT_MODE, createSecretKeySpec(), new IvParameterSpec(initializationVector)); byte[] bytesToEncrypt = plainText.getBytes(StandardCharsets.UTF_8); byte[] encryptedBytes = encryptCipher.doFinal(bytesToEncrypt); return String.join(":", "AES", ENCODER.encodeToString(initializationVector), ENCODER.encodeToString(encryptedBytes)); } catch (Exception e) { throw new CryptoException(e); } }
From source file:com.bytecode.util.Crypto.java
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); }/* w ww. j ava 2 s . c o m*/ SecretKey secretKey = new SecretKeySpec(keyBytes, "AES"); Cipher cipher = Cipher.getInstance("AES/ECB/NoPadding"); cipher.init(Cipher.ENCRYPT_MODE, secretKey); key = cipher.doFinal(keyBytes); for (int i = 0; i < bits - 16; i++) { key[16 + i] = key[i]; } return new SecretKeySpec(key, "AES"); }