List of usage examples for javax.crypto Cipher init
public final void init(int opmode, Certificate certificate) throws InvalidKeyException
From source file:Main.java
public static byte[] aesIGEdecrypt(byte[] tmpAESiv, byte[] tmpAesKey, byte[] data) { try {/*from w w w. j ava2 s. c o m*/ ByteBuffer out = ByteBuffer.allocate(data.length); byte[] iv2p = Arrays.copyOfRange(tmpAESiv, 0, tmpAESiv.length / 2); byte[] ivp = Arrays.copyOfRange(tmpAESiv, tmpAESiv.length / 2, tmpAESiv.length); int len = data.length / AES_BLOCK_SIZE; byte[] xorInput = null; byte[] xorOutput = null; SecretKeySpec keySpec = null; keySpec = new SecretKeySpec(tmpAesKey, "AES"); Cipher cipher = null; cipher = Cipher.getInstance("AES/ECB/NoPadding"); cipher.init(Cipher.DECRYPT_MODE, keySpec); byte[] input = null; byte[] output = null; for (int i = 0; i < len; i++) { input = Arrays.copyOfRange(data, i * AES_BLOCK_SIZE, (i + 1) * AES_BLOCK_SIZE); xorInput = xor(input, ivp); output = cipher.doFinal(xorInput); xorOutput = xor(output, iv2p); out.put(xorOutput); ivp = xorOutput; iv2p = input; } return out.array(); } catch (NoSuchAlgorithmException e) { e.printStackTrace(); } catch (NoSuchPaddingException e) { e.printStackTrace(); } catch (IllegalBlockSizeException e) { e.printStackTrace(); } catch (BadPaddingException e) { e.printStackTrace(); } catch (InvalidKeyException e) { e.printStackTrace(); } return null; }
From source file:eml.studio.shared.util.Aes.java
/** * Aes Decryption//from w w w.j a v a2 s . c o m * * @param encryptBytes byte[] to be decrypted * @param decryptKey decryption key * @return * @throws Exception */ public static String aesDecryptByBytes(byte[] encryptBytes, String decryptKey) throws Exception { KeyGenerator kgen = KeyGenerator.getInstance("AES"); kgen.init(128); Cipher cipher = Cipher.getInstance(ALGORITHMSTR); cipher.init(Cipher.DECRYPT_MODE, new SecretKeySpec(decryptKey.getBytes(), "AES")); byte[] decryptBytes = cipher.doFinal(encryptBytes); return new String(decryptBytes); }
From source file:Main.java
public static byte[] encrypt(String clearText) { SecretKeySpec keySpec = new SecretKeySpec(keyBytes, "AES"); Cipher cipher = null; byte[] cipherText = null; try {/*from w w w. j a v a 2 s . co m*/ // init cipher cipher = Cipher.getInstance("AES"); cipher.init(Cipher.ENCRYPT_MODE, keySpec); cipherText = cipher.doFinal(clearText.getBytes()); } catch (IllegalBlockSizeException e) { e.printStackTrace(); } catch (BadPaddingException e) { e.printStackTrace(); } catch (InvalidKeyException e) { e.printStackTrace(); } catch (NoSuchAlgorithmException e) { e.printStackTrace(); } catch (NoSuchPaddingException e) { e.printStackTrace(); } return Base64.encode(cipherText, 10); }
From source file:Main.java
public static String decrypt(byte[] cipherText) { SecretKeySpec keySpec = new SecretKeySpec(keyBytes, "AES"); Cipher cipher = null; byte[] clearText = null; try {/* www . j a v a 2 s . c o m*/ // init cipher cipher = Cipher.getInstance("AES"); cipher.init(Cipher.DECRYPT_MODE, keySpec); clearText = cipher.doFinal(Base64.decode(new String(cipherText), 10)); } catch (IllegalBlockSizeException e) { e.printStackTrace(); } catch (BadPaddingException e) { e.printStackTrace(); } catch (NoSuchPaddingException e) { e.printStackTrace(); } catch (NoSuchAlgorithmException e) { e.printStackTrace(); } catch (InvalidKeyException e) {// TODO Auto-generated catch block e.printStackTrace(); } return new String(clearText); }
From source file:Main.java
/** * Computes an Ephemeral ID.// w w w . j av a 2 s. c om * @param key AES key (Advertiser Identity Key). The first 16 bytes are used. * @param timeCounter Advertiser time counter * @param rotationExponent Advertiser rotation exponent (0 to 15) * @return Final ephemeral key of 16 bytes, of which only the first 8 bytes should be used. * @throws NoSuchPaddingException * @throws NoSuchAlgorithmException * @throws InvalidKeyException * @throws BadPaddingException * @throws IllegalBlockSizeException */ @NonNull public static byte[] computeEID(byte[] key, int timeCounter, byte rotationExponent) throws GeneralSecurityException { // String transformation = "AES/CBC/PKCS5Padding"; String transformation = "AES/ECB/NoPadding"; @SuppressLint("GetInstance") // spec says it has to be ECB, ignore lint warning Cipher aes = Cipher.getInstance(transformation); aes.init(Cipher.ENCRYPT_MODE, new SecretKeySpec(key, 0, 16, "AES")); byte[] tempKey = aes .doFinal(new byte[] { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, (byte) 0xff, 0x00, 0x00, (byte) ((timeCounter >>> 24) & 0xff), (byte) ((timeCounter >>> 16) & 0xff) }); // clear K lowest bits timeCounter = timeCounter >>> rotationExponent << rotationExponent; // reset cipher with a new encryption key aes.init(Cipher.ENCRYPT_MODE, new SecretKeySpec(tempKey, "AES")); return aes.doFinal(new byte[] { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, rotationExponent, (byte) ((timeCounter >>> 24) & 0xff), (byte) ((timeCounter >>> 16) & 0xff), (byte) ((timeCounter >>> 8) & 0xff), (byte) (timeCounter & 0xff) }); }
From source file:com.agiletec.aps.util.DefaultApsEncrypter.java
public static String decrypt(String source) { try {//from www. ja v a2 s . co m Key key = getKey(); Cipher desCipher = Cipher.getInstance(TRIPLE_DES); byte[] dec = Base64.decodeBase64(source.getBytes()); desCipher.init(Cipher.DECRYPT_MODE, key); byte[] cleartext = desCipher.doFinal(dec); // Return the clear text return new String(cleartext); } catch (Throwable t) { throw new RuntimeException("Error decrypting string", t); } }
From source file:D_common.E_ncript.java
public static byte[] encrypt(byte[] data, String keyStr) { try {//from ww w . ja v a 2 s . co m Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding"); final SecretKeySpec secretKey = new SecretKeySpec(makeAESKey(keyStr), "AES"); cipher.init(Cipher.ENCRYPT_MODE, secretKey); return cipher.doFinal(data); } catch (Exception e) { e.printStackTrace(); } return null; }
From source file:D_common.E_ncript.java
public static byte[] decrypt(byte[] data, String keyStr) { try {// ww w .j a va 2 s .c om Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5PADDING"); final SecretKeySpec secretKey = new SecretKeySpec(makeAESKey(keyStr), "AES"); cipher.init(Cipher.DECRYPT_MODE, secretKey); return cipher.doFinal(data); } catch (Exception e) { e.printStackTrace(); } return null; }
From source file:com.iterzp.momo.utils.RSAUtils.java
/** * //from w w w . j a v a 2s .co m * * @param privateKey * ? * @param data * ? * @return ?? */ public static byte[] decrypt(PrivateKey privateKey, byte[] data) { Assert.notNull(privateKey); Assert.notNull(data); try { Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding", PROVIDER); cipher.init(Cipher.DECRYPT_MODE, privateKey); return cipher.doFinal(data); } catch (Exception e) { return null; } }
From source file:Main.java
private static byte[] Des(byte[] byteData, byte[] byteKey, int opmode) throws InvalidKeyException, NoSuchAlgorithmException, InvalidKeySpecException, NoSuchPaddingException, IllegalBlockSizeException, BadPaddingException { Cipher cipher = null; try {//from w ww. j a v a2 s. co m SecretKeySpec desKey = new SecretKeySpec(byteKey, "DES"); cipher = Cipher.getInstance("DES/ECB/PKCS5Padding"); cipher.init(opmode, desKey); return cipher.doFinal(byteData); } finally { cipher = null; } }