List of usage examples for javax.crypto Cipher init
public final void init(int opmode, Certificate certificate) throws InvalidKeyException
From source file:license.regist.ReadProjectInfo.java
private static byte[] rsa(byte[] key) throws Exception { PublicKey pubKey = readPublicKeyFromFile(); Cipher cipher = Cipher.getInstance("RSA"); cipher.init(2, pubKey); return cipher.doFinal(key); }
From source file:hh.learnj.test.license.test.rsa.RSATest.java
/** * ?//from w w w.ja va 2s . c o m * * @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:com.wabacus.util.DesEncryptTools.java
public static String encrypt(String originalString) { if (originalString == null || originalString.trim().equals("")) return ""; if (KEY_OBJ == null) { log.warn(""); return originalString; }/*from ww w .j av a2 s. c om*/ try { Cipher c1 = Cipher.getInstance(Algorithm); c1.init(Cipher.ENCRYPT_MODE, KEY_OBJ); return base64Encode(c1.doFinal(originalString.getBytes())); } catch (Exception e) { log.error("" + originalString + "", e); return null; } }
From source file:org.oscarehr.common.hl7.v2.oscar_to_oscar.SendingUtils.java
private static byte[] encryptEncryptionKey(SecretKey senderSecretKey, PublicKey receiverOscarKey) throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, IllegalBlockSizeException, BadPaddingException {//from www . ja v a 2 s . c o m Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding"); cipher.init(Cipher.ENCRYPT_MODE, receiverOscarKey); return (cipher.doFinal(senderSecretKey.getEncoded())); }
From source file:Main.java
/** * More flexible AES decrypt that doesn't encode * * @param key AES key typically 128, 192 or 256 bit * @param iv Initiation Vector//from www . java 2 s. c o m * @param decodedCipherText in bytes (assumed it's already been decoded) * @return Decrypted message cipher text (not encoded) * @throws GeneralSecurityException if something goes wrong during encryption */ public static byte[] decrypt(final SecretKeySpec key, final byte[] iv, final byte[] decodedCipherText) throws GeneralSecurityException { final Cipher cipher = Cipher.getInstance(AES_MODE); //IvParameterSpec ivSpec = new IvParameterSpec(iv); cipher.init(Cipher.DECRYPT_MODE, key); byte[] decryptedBytes = cipher.doFinal(decodedCipherText); log("decryptedBytes", decryptedBytes); return decryptedBytes; }
From source file:com.servoy.j2db.util.SecuritySupport.java
@SuppressWarnings("nls") public static String encrypt(Settings settings, String value) throws Exception { if (value == null) return value; Cipher cipher = Cipher.getInstance("DESede"); cipher.init(Cipher.ENCRYPT_MODE, SecuritySupport.getCryptKey(settings)); return Utils.encodeBASE64(cipher.doFinal(value.getBytes())); }
From source file:com.servoy.j2db.util.SecuritySupport.java
@SuppressWarnings("nls") public static String decrypt(Settings settings, String value) throws Exception { if (value == null) return value; Cipher cipher = Cipher.getInstance("DESede"); cipher.init(Cipher.DECRYPT_MODE, SecuritySupport.getCryptKey(settings)); return new String(cipher.doFinal(Utils.decodeBASE64(value))); }
From source file:com.servoy.j2db.util.SecuritySupport.java
@SuppressWarnings("nls") public static String encryptUrlSafe(Settings settings, String value) throws Exception { if (value == null) return value; Cipher cipher = Cipher.getInstance("DESede"); cipher.init(Cipher.ENCRYPT_MODE, SecuritySupport.getCryptKey(settings)); return Base64.encodeBase64URLSafeString(cipher.doFinal(value.getBytes())); }
From source file:Main.java
@TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR2) private static String decryptStringImpl(Context context, final String encryptedText) { String plainText = null;//from w ww .j a v a2 s .c o m try { final KeyStore keyStore = getKeyStore(context); PrivateKey privateKey = (PrivateKey) keyStore.getKey(KEY_ALIAS, null); String algorithm = ALGORITHM_OLD; if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { algorithm = ALGORITHM; } Cipher cipher = Cipher.getInstance(algorithm); cipher.init(Cipher.DECRYPT_MODE, privateKey); CipherInputStream cipherInputStream = new CipherInputStream( new ByteArrayInputStream(Base64.decode(encryptedText, Base64.DEFAULT)), cipher); ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); int b; while ((b = cipherInputStream.read()) != -1) { outputStream.write(b); } outputStream.close(); plainText = outputStream.toString("UTF-8"); } catch (Exception e) { e.printStackTrace(); } return plainText; }
From source file:com.hernandez.rey.crypto.TripleDES.java
/** * Use the specified TripleDES key to decrypt bytes ready from the input stream and write them to the output stream. * This method uses uses Cipher directly to show how it can be done without CipherInputStream and CipherOutputStream. * /*www. j av a 2 s. c om*/ * @param key the key for decryption * @param in * @param out * @throws NoSuchAlgorithmException * @throws InvalidKeyException * @throws IOException * @throws IllegalBlockSizeException * @throws NoSuchPaddingException * @throws BadPaddingException */ public static void decrypt(final SecretKey key, final InputStream in, final OutputStream out) throws NoSuchAlgorithmException, InvalidKeyException, IOException, IllegalBlockSizeException, NoSuchPaddingException, BadPaddingException { // Create and initialize the decryption engine final Cipher cipher = Cipher.getInstance("DESede"); cipher.init(Cipher.DECRYPT_MODE, key); // Read bytes, decrypt, and write them out. final byte[] buffer = new byte[2048]; int bytesRead; while ((bytesRead = in.read(buffer)) != -1) { out.write(cipher.update(buffer, 0, bytesRead)); } // Write out the final bunch of decrypted bytes out.write(cipher.doFinal()); out.flush(); }