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.axelor.apps.account.ebics.xml.InitializationRequestElement.java
/** * Generates the upload transaction key// w w w . ja v a 2s .c o m * @return the transaction key */ protected byte[] generateTransactionKey() throws AxelorException { try { Cipher cipher; cipher = Cipher.getInstance("RSA/NONE/PKCS1Padding", BouncyCastleProvider.PROVIDER_NAME); cipher.init(Cipher.ENCRYPT_MODE, session.getBankE002Key()); return cipher.doFinal(nonce); } catch (Exception e) { e.printStackTrace(); throw new AxelorException(e.getMessage(), IException.CONFIGURATION_ERROR); } }
From source file:de.petendi.commons.crypto.HybridCrypto.java
private byte[] encryptInternal(byte[] message) { createSymmetricPassphrase();//from ww w .j a va 2 s . com try { Cipher cipher = Cipher.getInstance(SYMMETRIC_CIPHER_ALGORITHM); cipher.init(Cipher.ENCRYPT_MODE, symmetricKey, new IvParameterSpec(iv)); byte[] encryptedBody = cipher.doFinal(message); encryptedMessage.setEncryptedBody(encryptedBody); return encryptedBody; } catch (Exception e) { throw new IllegalArgumentException(e); } }
From source file:hudson.util.SecretTest.java
/** * Secret persisted with Jenkins.getSecretKey() should still decrypt OK. *//*w w w .ja va2s . c o m*/ @Test public void migrationFromLegacyKeyToConfidentialStore() throws Exception { SecretKey legacy = HistoricalSecrets.getLegacyKey(); for (String str : new String[] { "Hello world", "", "\u0000unprintable" }) { Cipher cipher = Secret.getCipher("AES"); cipher.init(Cipher.ENCRYPT_MODE, legacy); String old = new String( Base64.encode(cipher.doFinal((str + HistoricalSecrets.MAGIC).getBytes("UTF-8")))); Secret s = Secret.fromString(old); assertEquals("secret by the old key should decrypt", str, s.getPlainText()); assertNotEquals("but when encrypting, ConfidentialKey should be in use", old, s.getEncryptedValue()); } }
From source file:de.alpharogroup.crypto.key.PublicKeyHexEncryptor.java
/** * Initializes the {@link PublicKeyHexEncryptor} object. * * @throws UnsupportedEncodingException// ww w. j a v a 2s. co m * is thrown by get the byte array of the private key String object fails. * @throws NoSuchAlgorithmException * is thrown if instantiation of the cypher object fails. * @throws NoSuchPaddingException * is thrown if instantiation of the cypher object fails. * @throws InvalidKeyException * the invalid key exception is thrown if initialization of the cypher object fails. * @throws InvalidKeySpecException * is thrown if generation of the SecretKey object fails. */ private void initialize() throws NoSuchAlgorithmException, InvalidKeySpecException, UnsupportedEncodingException, NoSuchPaddingException, InvalidKeyException { if (!isInitialized()) { cipher = Cipher.getInstance( KeyPairWithModeAndPaddingAlgorithm.RSA_ECB_OAEPWithSHA1AndMGF1Padding.getAlgorithm()); cipher.init(Cipher.ENCRYPT_MODE, this.publicKey); } }
From source file:eap.util.EDcodeUtil.java
public static String desEncodeAsHex(String data, String key) { return hexEncode(des(utf8Encode(data), utf8Encode(key), Cipher.ENCRYPT_MODE)); }
From source file:net.sourceforge.jaulp.crypto.aes.HexEncryptor.java
/** * Initializes the {@link HexEncryptor} object. * * @throws UnsupportedEncodingException/* ww w . j av a2s . c o m*/ * is thrown by get the byte array of the private key String object fails. * @throws NoSuchAlgorithmException * is thrown if instantiation of the cypher object fails. * @throws NoSuchPaddingException * is thrown if instantiation of the cypher object fails. * @throws InvalidKeyException * the invalid key exception is thrown if initialization of the cypher object fails. */ private void initialize() throws UnsupportedEncodingException, NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException { if (!isInitialized()) { byte[] key; if (this.getPrivateKey() != null) { key = this.getPrivateKey().getBytes("UTF-8"); } else { key = CryptConst.PRIVATE_KEY.getBytes("UTF-8"); } SecretKeySpec skeySpec = new SecretKeySpec(key, algorithm.getAlgorithm()); this.cipher = Cipher.getInstance(algorithm.getAlgorithm()); cipher.init(Cipher.ENCRYPT_MODE, skeySpec); } }
From source file: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 a va 2s .co m public static void encrypt(SecretKey key, InputStream in, OutputStream out) throws NoSuchAlgorithmException, InvalidKeyException, NoSuchPaddingException, IOException { // Create and initialize the encryption engine Cipher cipher = Cipher.getInstance("DESede"); cipher.init(Cipher.ENCRYPT_MODE, key); // Create a special output stream to do the work for us CipherOutputStream cos = new CipherOutputStream(out, cipher); // Read from the input and write to the encrypting output stream 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.POLIS.licensing.common.license.AbstractSerializationBasedLicense.java
@Override public String getEncryptedLicense(PublicKey targetKey) throws SystemStateException, OperationException { byte[] licenseAsBytes; try (ByteArrayOutputStream bos = new ByteArrayOutputStream(); ObjectOutput out = new ObjectOutputStream(bos)) { out.writeObject(this); licenseAsBytes = bos.toByteArray(); } catch (IOException ex) { throw new OperationException("An error occured while serializing the license", ex); }/*from w w w .j a v a 2 s .c o m*/ SecureRandom random = new SecureRandom(); Cipher aescipher; Cipher rsacipher; KeyGenerator aesgenerator; Key symkey; try { aesgenerator = KeyGenerator.getInstance(symmetricKeyType, provider); aesgenerator.init(128, random); symkey = aesgenerator.generateKey(); } catch (NoSuchAlgorithmException | NoSuchProviderException ex) { throw new SystemStateException("The specified symkey could not be generated.", ex); } try { aescipher = Cipher.getInstance(symmetricEncoding, provider); rsacipher = Cipher.getInstance(asymmetricEncoding, provider); aescipher.init(Cipher.ENCRYPT_MODE, symkey); rsacipher.init(Cipher.ENCRYPT_MODE, targetKey); } catch (NoSuchAlgorithmException | NoSuchProviderException | /*InvalidKeySpecException |*/ NoSuchPaddingException | InvalidKeyException ex) { throw new SystemStateException("The specified encryption provider or algorithm was not found", ex); } String encryptedLicense; try { byte[] encryptedsymkey = rsacipher.doFinal(symkey.getEncoded()); byte[] encryptedlicense = aescipher.doFinal(licenseAsBytes); byte[] licenseWithKey = new byte[encryptedsymkey.length + encryptedlicense.length]; System.arraycopy(encryptedsymkey, 0, licenseWithKey, 0, encryptedsymkey.length); System.arraycopy(encryptedlicense, 0, licenseWithKey, encryptedsymkey.length, encryptedlicense.length); encryptedLicense = Base64.encodeBase64String(licenseWithKey); } catch (IllegalBlockSizeException | BadPaddingException ex) { throw new OperationException("Could not encode to base64", ex); } return encryptedLicense; }
From source file:de.alpharogroup.crypto.aes.HexNewEncryptor.java
@Override protected Cipher newCipher(final String privateKey, final String algorithm, final byte[] salt, final int iterationCount, final int operationMode) throws NoSuchAlgorithmException, InvalidKeySpecException, NoSuchPaddingException, InvalidKeyException, InvalidAlgorithmParameterException, UnsupportedEncodingException { final SecretKeySpec skeySpec = new SecretKeySpec(privateKey.getBytes("UTF-8"), this.algorithm.getAlgorithm()); this.cipher = Cipher.getInstance(this.algorithm.getAlgorithm()); cipher.init(Cipher.ENCRYPT_MODE, skeySpec); return cipher; }
From source file:net.sf.keystore_explorer.crypto.privatekey.Pkcs8Util.java
/** * PKCS #8 encode and encrypt a private key. * * @return The encrypted encoding/* w w w . j a v a 2 s . com*/ * @param privateKey * The private key * @param pbeType * PBE algorithm to use for encryption * @param password * Encryption password * @throws CryptoException * Problem encountered while getting the encoded private key * @throws IOException * If an I/O error occurred */ public static byte[] getEncrypted(PrivateKey privateKey, Pkcs8PbeType pbeType, Password password) throws CryptoException, IOException { try { byte[] pkcs8 = get(privateKey); // Generate PBE secret key from password SecretKeyFactory keyFact = SecretKeyFactory.getInstance(pbeType.jce()); PBEKeySpec pbeKeySpec = new PBEKeySpec(password.toCharArray()); SecretKey pbeKey = keyFact.generateSecret(pbeKeySpec); // Generate random salt and iteration count byte[] salt = generateSalt(); int iterationCount = generateIterationCount(); // Store in algorithm parameters PBEParameterSpec pbeParameterSpec = new PBEParameterSpec(salt, iterationCount); AlgorithmParameters params = AlgorithmParameters.getInstance(pbeType.jce()); params.init(pbeParameterSpec); // Create PBE cipher from key and params Cipher cipher = Cipher.getInstance(pbeType.jce()); cipher.init(Cipher.ENCRYPT_MODE, pbeKey, params); // Encrypt key byte[] encPkcs8 = cipher.doFinal(pkcs8); // Create and return encrypted private key information EncryptedPrivateKeyInfo encPrivateKeyInfo = new EncryptedPrivateKeyInfo(params, encPkcs8); return encPrivateKeyInfo.getEncoded(); } catch (GeneralSecurityException ex) { throw new CryptoException("NoEncryptPkcs8PrivateKey.exception.message", ex); } }