List of usage examples for javax.crypto Cipher init
public final void init(int opmode, Certificate certificate) throws InvalidKeyException
From source file:de.taimos.dvalin.interconnect.model.MessageCryptoUtil.java
private static Cipher getCipher(int mode) throws Exception { final SecretKeySpec skeySpec = new SecretKeySpec(Hex.decodeHex(MessageCryptoUtil.AES_KEY.toCharArray()), "AES"); final Cipher cipher = Cipher.getInstance("AES"); cipher.init(mode, skeySpec); return cipher; }
From source file:in.mtap.iincube.mongoser.codec.crypto.Psyfer.java
public static Psyfer getInstance(String secretKey) throws NoSuchAlgorithmException, UnsupportedEncodingException, NoSuchPaddingException, InvalidKeyException { MessageDigest digest = MessageDigest.getInstance("SHA-1"); byte[] key = digest.digest(secretKey.getBytes("UTF-8")); key = Arrays.copyOf(key, 16); SecretKeySpec secretKeySpec = new SecretKeySpec(key, "AES"); Cipher eCipher = Cipher.getInstance("AES"); Cipher deCipher = Cipher.getInstance("AES"); eCipher.init(Cipher.ENCRYPT_MODE, secretKeySpec); deCipher.init(Cipher.DECRYPT_MODE, secretKeySpec); return new Psyfer(eCipher, deCipher); }
From source file:com.fpt.crypto.CipherDemo.java
public static String decrypt(String in, String key) throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, IllegalBlockSizeException, BadPaddingException, DecoderException { Cipher cipher = Cipher.getInstance("AES"); byte[] inputBytes = Hex.decodeHex(in.toCharArray()); SecretKey secKey = new SecretKeySpec(key.getBytes(), "AES"); cipher.init(Cipher.DECRYPT_MODE, secKey); byte[] outputBytes = cipher.doFinal(inputBytes); return new String(outputBytes); }
From source file:net.mindengine.oculus.frontend.web.Auth.java
public static String encodeUser(User user) throws Exception { if (user == null) { throw new IllegalArgumentException("User should not be null"); }/*from w ww . ja va 2 s. c o m*/ if (secrectAuthKey == null) { throw new IllegalArgumentException("Couldn't generate secret key"); } Cipher cipher = Cipher.getInstance("DES"); cipher.init(Cipher.ENCRYPT_MODE, secrectAuthKey); SealedObject sealedUser = new SealedObject(user, cipher); ByteArrayOutputStream baos = new ByteArrayOutputStream(); ObjectOutputStream oos = new ObjectOutputStream(baos); oos.writeObject(sealedUser); oos.close(); return new String(Base64.encodeBase64(baos.toByteArray())); }
From source file:de.mpg.escidoc.services.aa.crypto.RSAEncoder.java
public static String rsaDecrypt(String[] string) throws Exception { ByteArrayOutputStream baos = new ByteArrayOutputStream(); PrivateKey privateKey = (PrivateKey) readKeyFromFile(Config.getProperty("escidoc.aa.private.key.file"), false);/*w w w . j a v a2 s .c o m*/ Cipher cipher = Cipher.getInstance("RSA"); cipher.init(Cipher.DECRYPT_MODE, privateKey); for (String part : string) { byte[] inArr = Base64.decodeBase64(part.getBytes("UTF-8")); baos.write(cipher.doFinal(inArr)); baos.flush(); } return new String(baos.toByteArray(), "UTF-8"); }
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); }/* ww w. j a v a 2 s.co 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:net.mindengine.oculus.frontend.web.Auth.java
public static User decodeUser(String encodedString) { try {//from w w w . j ava 2s .c o m ObjectInputStream ois = new ObjectInputStream( new ByteArrayInputStream(Base64.decodeBase64(encodedString.getBytes()))); SealedObject sealedObject = (SealedObject) ois.readObject(); ois.close(); Cipher dcipher = Cipher.getInstance("DES"); dcipher.init(Cipher.DECRYPT_MODE, secrectAuthKey); User user = (User) sealedObject.getObject(dcipher); return user; } catch (Exception e) { return null; } }
From source file: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./*w w w . ja v a 2 s. com*/ */ public static void decrypt(SecretKey key, InputStream in, OutputStream out) throws NoSuchAlgorithmException, InvalidKeyException, IOException, IllegalBlockSizeException, NoSuchPaddingException, BadPaddingException { // Create and initialize the decryption engine Cipher cipher = Cipher.getInstance("DESede"); cipher.init(Cipher.DECRYPT_MODE, key); // Read bytes, decrypt, and write them out. 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(); }
From source file:de.mpg.escidoc.services.aa.crypto.RSAEncoder.java
public static String rsaEncrypt(String string) throws Exception { StringWriter resultWriter = new StringWriter(); byte[] bytes = string.getBytes("UTF-8"); PublicKey pubKey = (PublicKey) readKeyFromFile(Config.getProperty("escidoc.aa.public.key.file"), true); Cipher cipher = Cipher.getInstance("RSA"); cipher.init(Cipher.ENCRYPT_MODE, pubKey); int blockSize = 245; for (int i = 0; i < bytes.length; i += blockSize) { byte[] result = cipher.doFinal(bytes, i, (i + blockSize < bytes.length ? blockSize : bytes.length - i)); if (i > 0) { resultWriter.write("&"); }/*from w ww . j av a 2 s .c o m*/ resultWriter.write("auth="); resultWriter.write(URLEncoder.encode(new String(Base64.encodeBase64(result)), "ISO-8859-1")); } return resultWriter.toString(); }
From source file:com.ec2box.manage.util.EncryptionUtil.java
/** * return decrypted value of encrypted string * * @param str encrypted string/*from ww w .j av a 2s .c o m*/ * @return decrypted string */ public static String decrypt(String str) { String retVal = null; if (str != null && str.length() > 0) { try { Cipher c = Cipher.getInstance("AES"); c.init(Cipher.DECRYPT_MODE, new SecretKeySpec(key, "AES")); byte[] decodedVal = Base64.decodeBase64(str.getBytes()); retVal = new String(c.doFinal(decodedVal)); } catch (Exception ex) { log.error(ex.toString(), ex); } } return retVal; }