Studyon Minte9.com
Java SE




Encrypt password 1.6




package cryptor; import java.nio.charset.Charset; import java.security.InvalidAlgorithmParameterException; import java.security.InvalidKeyException; import java.security.NoSuchAlgorithmException; import javax.crypto.BadPaddingException; import javax.crypto.Cipher; import javax.crypto.IllegalBlockSizeException; import javax.crypto.NoSuchPaddingException; import javax.crypto.SecretKey; import javax.crypto.spec.IvParameterSpec; import javax.crypto.spec.SecretKeySpec; import javax.xml.bind.DatatypeConverter; public class Cryptor { private Cipher cipher; private static String secretKey = "ABCDEF1234567890"; private String iv = "ABCDEF1234567890"; private final String CIPHER_MODE = "AES/CFB8/NoPadding"; private SecretKey keySpec; private IvParameterSpec ivSpec; private Charset CHARSET = Charset.forName("UTF8"); private static String cmd; private static String original; private static String encrypted; private static String decripted; public static void main(String args[]) { try { Cryptor c = new Cryptor(args); String original = "MiiiMüäöMeeʞ"; System.out.println("Original: " + original); encrypted = c.encrypt(original); System.out.println("Encoded: " + encrypted); System.out.println("Decoded: " + c.decrypt(encrypted)); } catch (Exception e) { e.printStackTrace(); } } public Cryptor(String args[]) throws Exception { if (args.length != 3) throw new Exception("Wrong parameters!"); cmd = args[0]; if (cmd.equals("encrypt")) original = args[1]; if (cmd.equals("decripted")) encrypted = args[1]; secretKey = args[2]; keySpec = new SecretKeySpec(secretKey.getBytes(CHARSET), "AES"); ivSpec = new IvParameterSpec(iv.getBytes(CHARSET)); try { cipher = Cipher.getInstance(CIPHER_MODE); } catch (NoSuchAlgorithmException e) { throw new SecurityException(e); } catch (NoSuchPaddingException e) { throw new SecurityException(e); } } public String getEncrypted() { return encrypted; } public String getDecripted() { return decripted; } /** * @param input A "AES/CFB8/NoPadding" encrypted String * @return The decrypted String * @throws CryptingException */ public String decrypt(String input) { try { cipher.init(Cipher.DECRYPT_MODE, keySpec, ivSpec); return new String(cipher.doFinal(DatatypeConverter.parseBase64Binary(input))); } catch (IllegalBlockSizeException e) { throw new SecurityException(e); } catch (BadPaddingException e) { throw new SecurityException(e); } catch (InvalidKeyException e) { throw new SecurityException(e); } catch (InvalidAlgorithmParameterException e) { throw new SecurityException(e); } } /** * @param input Any String to be encrypted * @return An "AES/CFB8/NoPadding" encrypted String * @throws CryptingException */ public String encrypt(String input) { try { cipher.init(Cipher.ENCRYPT_MODE, keySpec, ivSpec); return DatatypeConverter.printBase64Binary(cipher.doFinal(input.getBytes(CHARSET))).trim(); } catch (InvalidKeyException e) { throw new SecurityException(e); } catch (InvalidAlgorithmParameterException e) { throw new SecurityException(e); } catch (IllegalBlockSizeException e) { throw new SecurityException(e); } catch (BadPaddingException e) { throw new SecurityException(e); } } }
 


http://stackoverflow.com/questions/15906041/java-aes-cfb8-nopadding-encryption-and-charset