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.glaf.core.security.SecurityUtils.java
/** * ?????,??/*from www . j av a 2 s . c o m*/ * * @param ctx * * @param symmetryKey * * @param pubKey * * @return String(?base64?) */ public static String generateDigitalEnvelope(SecurityContext ctx, Key symmetryKey, byte[] pubKey) { String result = null; InputStream inputStream = null; try { CertificateFactory cf = CertificateFactory.getInstance("X.509"); inputStream = new ByteArrayInputStream(pubKey); java.security.cert.Certificate cert = cf.generateCertificate(inputStream); inputStream.close(); PublicKey publicKey = cert.getPublicKey(); Cipher cipher = Cipher.getInstance(ctx.getAsymmetryAlgorithm()); cipher.init(Cipher.ENCRYPT_MODE, publicKey); result = Base64.encodeBase64String(cipher.doFinal(symmetryKey.getEncoded())); return result; } catch (Exception ex) { throw new SecurityException(ex); } finally { try { if (inputStream != null) { inputStream.close(); inputStream = null; } } catch (IOException ex) { } } }
From source file:com.cedarsoft.crypt.CertTest.java
@Test public void testKey() throws Exception { InputStream inStream = new DataInputStream(getClass().getResource("/test.der").openStream()); byte[] keyBytes = new byte[inStream.available()]; inStream.read(keyBytes);// w w w. j av a 2 s . com inStream.close(); KeyFactory keyFactory = KeyFactory.getInstance("RSA"); // decipher private key PKCS8EncodedKeySpec privSpec = new PKCS8EncodedKeySpec(keyBytes); RSAPrivateKey privKey = (RSAPrivateKey) keyFactory.generatePrivate(privSpec); assertNotNull(privKey); Cipher cipher = Cipher.getInstance("RSA"); cipher.init(Cipher.ENCRYPT_MODE, privKey); byte[] bytes = cipher.doFinal(PLAINTEXT.getBytes()); assertEquals(SCRAMBLED, new String(Base64.encodeBase64(bytes))); }
From source file:hh.learnj.test.license.test.rsa.RSATest.java
/** * ?//from w w w.jav a 2 s . c om * * @param data * @return * @throws Exception */ static String encryptionByPrivateKey(String source) throws Exception { PrivateKey privateKey = getPrivateKey(); Cipher cipher = Cipher.getInstance(privateKey.getAlgorithm()); cipher.init(Cipher.ENCRYPT_MODE, privateKey); cipher.update(source.getBytes("UTF-8")); String target = encodeBase64(cipher.doFinal()); System.out.println("???\r\n" + target); return target; }
From source file:eap.util.EDcodeUtil.java
public static byte[] desEncode(byte[] data, byte[] key) { return des(data, key, Cipher.ENCRYPT_MODE); }
From source file:com.alliander.osgp.oslp.OslpUtils.java
private static byte[] createEncryptedHash(final byte[] message, final PrivateKey privateKey) throws GeneralSecurityException { final byte[] hash = createHash(message); // Encrypt the hash final Cipher cipher = Cipher.getInstance(FALLBACK_CIPHER); cipher.init(Cipher.ENCRYPT_MODE, privateKey); return cipher.doFinal(hash); }
From source file:com.ephesoft.dcma.encryption.core.EncryptorDecryptor.java
/** * This method is used to start the encryption process. * /*w ww .j a va 2 s . co m*/ * @param data byte[] * @param salt byte[] * @param isEncryption boolean * @return byte[] * @throws CryptographyException {@link CryptographyException} */ public byte[] startCrypting(byte[] data, byte[] salt, boolean isEncryption) throws CryptographyException { KeySpec keySpec = new PBEKeySpec(EncryptionConstants.KEY.toCharArray(), salt, EncryptionConstants.ITERATION_COUNT); SecretKey key; byte[] finalBytes = null; try { key = SecretKeyFactory.getInstance(EncryptionConstants.ALGORITHM).generateSecret(keySpec); Cipher ecipher = Cipher.getInstance(key.getAlgorithm()); AlgorithmParameterSpec paramSpec = new PBEParameterSpec(salt, EncryptionConstants.ITERATION_COUNT); if (isEncryption) { ecipher.init(Cipher.ENCRYPT_MODE, key, paramSpec); } else { ecipher.init(Cipher.DECRYPT_MODE, key, paramSpec); } finalBytes = ecipher.doFinal(data); } catch (InvalidKeySpecException e) { if (isEncryption) { LOGGER.error("Encryption : Key used is invalid", e); throw new CryptographyException("Key used is invalid", e); } else { LOGGER.error("Decryption : Key used is invalid", e); throw new CryptographyException("Key used is invalid", e); } } catch (NoSuchAlgorithmException e) { if (isEncryption) { LOGGER.error("Encryption : Algorithm used does not exist", e); throw new CryptographyException("Algorithm used does not exist", e); } else { LOGGER.error("Decryption : Algorithm used does not exist", e); throw new CryptographyException("Algorithm used does not exist", e); } } catch (NoSuchPaddingException e) { if (isEncryption) { LOGGER.error("Encryption : Padding used does not exist", e); throw new CryptographyException("Padding used does not exist", e); } else { LOGGER.error("Decryption : Padding used does not exist", e); throw new CryptographyException("Padding used does not exist", e); } } catch (InvalidKeyException e) { if (isEncryption) { LOGGER.error("Encryption : Key generated is invalid", e); throw new CryptographyException("Key generated is invalid", e); } else { LOGGER.error("Decryption : Key generated is invalid", e); throw new CryptographyException("Key generated is invalid", e); } } catch (InvalidAlgorithmParameterException e) { if (isEncryption) { LOGGER.error("Encryption : Algorithm parameter is invalid", e); throw new CryptographyException("Algorithm parameter is invalid", e); } else { LOGGER.error("Decryption : Algorithm parameter is invalid", e); throw new CryptographyException("Algorithm parameter is invalid", e); } } catch (IllegalBlockSizeException e) { if (isEncryption) { LOGGER.error("Encryption : Block size is illegal", e); throw new CryptographyException("Block size is illegal", e); } else { LOGGER.error("Decryption : Block size is illegal", e); throw new CryptographyException("Block size is illegal", e); } } catch (BadPaddingException e) { if (isEncryption) { LOGGER.error("Encryption : Padding done is invalid", e); throw new CryptographyException("Padding done is invalid", e); } else { LOGGER.error("Decryption : Padding done is invalid", e); throw new CryptographyException("Padding done is invalid", e); } } return finalBytes; }
From source file:com.forsrc.utils.AesUtils.java
/** * Encrypt string.// w w w . ja va 2 s. c o m * * @param src the src * @return String string * @throws AesException the aes exception * @Title: encrypt * @Description: */ public String encrypt(String src) throws AesException { Cipher cipher = null; try { cipher = Cipher.getInstance(CIPHER_KEY); } catch (NoSuchAlgorithmException e) { throw new AesException(e); } catch (NoSuchPaddingException e) { throw new AesException(e); } byte[] raw = KEY.getBytes(); SecretKeySpec secretKeySpec = new SecretKeySpec(raw, SECRET_KEY); IvParameterSpec ivParameterSpec = new IvParameterSpec(IV_PARAMETER.getBytes()); try { cipher.init(Cipher.ENCRYPT_MODE, secretKeySpec, ivParameterSpec); } catch (InvalidKeyException e) { throw new AesException(e); } catch (InvalidAlgorithmParameterException e) { throw new AesException(e); } byte[] encrypted; try { encrypted = cipher.doFinal(src.getBytes(CHARSET_UTF8)); } catch (IllegalBlockSizeException e) { throw new AesException(e); } catch (BadPaddingException e) { throw new AesException(e); } catch (UnsupportedEncodingException e) { throw new AesException(e); } return new String(new Base64().encode(encrypted)); }
From source file:com.security.ch08_rsa.RSACoderTextKey.java
/** * ?//from w w w . j a v a2s . c om * * @param data * ? * @param key * ? * @return byte[] ? * @throws Exception */ private 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:com.frame.Conf.Utilidades.java
public static String encrypt(String key, String iv, String cleartext) throws Exception { Cipher cipher = Cipher.getInstance(cI); SecretKeySpec skeySpec = new SecretKeySpec(key.getBytes(), alg); IvParameterSpec ivParameterSpec = new IvParameterSpec(iv.getBytes()); cipher.init(Cipher.ENCRYPT_MODE, skeySpec, ivParameterSpec); byte[] encrypted = cipher.doFinal(cleartext.getBytes()); return new String(encodeBase64(encrypted)); }
From source file:com.qubit.solution.fenixedu.bennu.webservices.services.client.WebServiceClientHandler.java
public static String cypher(byte[] sessionKey, String informationToCipher) { Cipher cipher;/*ww w. j a v a 2s . c o m*/ try { cipher = Cipher.getInstance("AES/ECB/PKCS5Padding"); cipher.init(Cipher.ENCRYPT_MODE, new SecretKeySpec(sessionKey, "AES")); byte[] cipherData = cipher.doFinal(informationToCipher.getBytes("UTF-8")); return Base64.encodeBase64String(cipherData); } catch (Exception e) { throw new RuntimeException(e); } }