List of usage examples for javax.crypto Cipher getInstance
public static final Cipher getInstance(String transformation) throws NoSuchAlgorithmException, NoSuchPaddingException
From source file:Main.java
public static byte[] encrypt(byte[] byteArray, PrivateKey privateKey) { Cipher cipher = null;// w w w . j a v a2 s. c o m try { cipher = Cipher.getInstance("RSA/ECB/NoPadding"); /* (define cipher (javax.crypto.Cipher.getInstance "RSA/ECB/NoPadding")) */ } catch (NoSuchAlgorithmException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (NoSuchPaddingException e) { // TODO Auto-generated catch block e.printStackTrace(); } try { cipher.init(Cipher.ENCRYPT_MODE, privateKey); } catch (InvalidKeyException e) { e.printStackTrace(); } ByteArrayInputStream input = new ByteArrayInputStream(byteArray); ByteArrayOutputStream output = new ByteArrayOutputStream(); try { while (input.available() != 0) { byte[] t0 = new byte[100]; input.read(t0); output.write(cipher.doFinal(t0)); } } catch (IllegalBlockSizeException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (BadPaddingException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } return output.toByteArray(); }
From source file:com.sysfore.pos.licensemanagement.LicenceManagementUtil.java
public static String encrypt(String strToEncrypt) { try {//w w w. j a v a2s . co m Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding"); final SecretKeySpec secretKey = new SecretKeySpec(key, "AES"); cipher.init(Cipher.ENCRYPT_MODE, secretKey); final String encryptedString = Base64.encodeBase64String(cipher.doFinal(strToEncrypt.getBytes())); return encryptedString; } catch (Exception e) { log.error("Error while encrypting", e); } return null; }
From source file:Main.java
public static byte[] decrypt(byte[] byteArray, PublicKey publicKey) { Cipher cipher = null;/*w w w . j a v a2 s .c o m*/ try { cipher = Cipher.getInstance("RSA/ECB/NoPadding"); } catch (NoSuchAlgorithmException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (NoSuchPaddingException e) { // TODO Auto-generated catch block e.printStackTrace(); } try { cipher.init(Cipher.DECRYPT_MODE, publicKey); } catch (InvalidKeyException e) { // TODO Auto-generated catch block e.printStackTrace(); } ByteArrayInputStream input = new ByteArrayInputStream(byteArray); ByteArrayOutputStream output = new ByteArrayOutputStream(); try { while (input.available() != 0) { byte[] t0 = new byte[128]; input.read(t0); output.write(cipher.doFinal(t0)); } } catch (IllegalBlockSizeException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (BadPaddingException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } return output.toByteArray(); }
From source file:Main.java
public static byte[] encrypt(byte[] input, byte[] key) throws NoSuchPaddingException, NoSuchAlgorithmException, InvalidKeyException, BadPaddingException, IllegalBlockSizeException { SecretKeySpec keySpec = new SecretKeySpec(key, "AES"); Cipher cipher = Cipher.getInstance("AES"); cipher.init(Cipher.ENCRYPT_MODE, keySpec); return cipher.doFinal(input); }
From source file:Main.java
public static byte[] decrypt(byte[] input, byte[] key) throws InvalidKeyException, NoSuchPaddingException, NoSuchAlgorithmException, BadPaddingException, IllegalBlockSizeException { SecretKeySpec keySpec = new SecretKeySpec(key, "AES"); Cipher cipher = Cipher.getInstance("AES"); cipher.init(Cipher.DECRYPT_MODE, keySpec); return cipher.doFinal(input); }
From source file:encryptdecrypt.util.Security.java
public static String encrypt(String strToEncrypt) { try {// w w w . j a va 2 s. c om Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding"); final SecretKeySpec secretKey = new SecretKeySpec(key, "AES"); cipher.init(Cipher.ENCRYPT_MODE, secretKey); return new String(Base64.encodeBase64(cipher.doFinal(strToEncrypt.getBytes()))); } catch (Exception e) { e.printStackTrace(); } return null; }
From source file:MainClass.java
public static void desEncrypt(String f1, String f2) throws Exception { SecretKey key = null;// ww w. j a v a 2 s . com ObjectInputStream keyFile = new ObjectInputStream(new FileInputStream("DESKey.ser")); key = (SecretKey) keyFile.readObject(); keyFile.close(); Cipher cipher = Cipher.getInstance("DES/ECB/PKCS5Padding"); cipher.init(Cipher.DECRYPT_MODE, key); CipherInputStream in = new CipherInputStream(new BufferedInputStream(new FileInputStream(f1)), cipher); BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(f2)); int i; do { i = in.read(); if (i != -1) out.write(i); } while (i > 0); in.close(); out.close(); }
From source file:MainClass.java
public static void blowfishEncrypt(String f1, String f2) throws Exception { SecretKey key = null;/*from ww w . ja v a2s.c o m*/ ObjectInputStream keyFile = new ObjectInputStream(new FileInputStream("BlowfishKey.ser")); key = (SecretKey) keyFile.readObject(); keyFile.close(); Cipher cipher = Cipher.getInstance("Blowfish/ECB/PKCS5Padding"); cipher.init(Cipher.DECRYPT_MODE, key); CipherInputStream in = new CipherInputStream(new BufferedInputStream(new FileInputStream(f1)), cipher); BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(f2)); int i; do { i = in.read(); if (i != -1) out.write(i); } while (i > 0); in.close(); out.close(); }
From source file:Main.java
private static byte[] decrypt(byte[] raw, byte[] encrypted) throws Exception { SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES"); Cipher cipher = Cipher.getInstance("AES"); cipher.init(Cipher.DECRYPT_MODE, skeySpec); return cipher.doFinal(encrypted); }
From source file:Main.java
private static byte[] decrypt(byte[] raw, byte[] encrypted) throws Exception { SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES"); Cipher cipher = Cipher.getInstance("AES"); cipher.init(Cipher.DECRYPT_MODE, skeySpec); byte[] decrypted = cipher.doFinal(encrypted); return decrypted; }