List of usage examples for javax.crypto SecretKeyFactory getInstance
public static final SecretKeyFactory getInstance(String algorithm) throws NoSuchAlgorithmException
From source file:Main.java
public static Key updGenerateKey(String key) { try {/* w w w . j a v a2 s. co m*/ DESedeKeySpec KeySpec = new DESedeKeySpec(UdpHexDecode(key)); SecretKeyFactory KeyFactory = SecretKeyFactory.getInstance("DESede"); Key k = ((KeyFactory.generateSecret(((KeySpec))))); return k; } catch (Exception e) { e.printStackTrace(); return null; } }
From source file:Main.java
public static byte[] des3EncodeECB(byte[] key, byte[] data) throws Exception { Key deskey = null;/*from w w w.ja v a 2s.co m*/ DESedeKeySpec spec = new DESedeKeySpec(key); SecretKeyFactory keyfactory = SecretKeyFactory.getInstance("desede"); deskey = keyfactory.generateSecret(spec); Cipher cipher = Cipher.getInstance("desede" + "/ECB/PKCS5Padding"); cipher.init(Cipher.ENCRYPT_MODE, deskey); byte[] bOut = cipher.doFinal(data); return bOut; }
From source file:Main.java
public static byte[] ees3DecodeECB(byte[] key, byte[] data) throws Exception { Key deskey = null;/*from w ww . j a v a 2 s .c o m*/ DESedeKeySpec spec = new DESedeKeySpec(key); SecretKeyFactory keyfactory = SecretKeyFactory.getInstance("desede"); deskey = keyfactory.generateSecret(spec); Cipher cipher = Cipher.getInstance("desede" + "/ECB/PKCS5Padding"); cipher.init(Cipher.DECRYPT_MODE, deskey); byte[] bOut = cipher.doFinal(data); return bOut; }
From source file:Main.java
public static byte[] des3EncodeCBC(byte[] key, byte[] keyiv, byte[] data) throws Exception { Key deskey = null;/*from w w w .ja va2 s. c o m*/ DESedeKeySpec spec = new DESedeKeySpec(key); SecretKeyFactory keyfactory = SecretKeyFactory.getInstance("desede"); deskey = keyfactory.generateSecret(spec); Cipher cipher = Cipher.getInstance("desede" + "/CBC/PKCS5Padding"); IvParameterSpec ips = new IvParameterSpec(keyiv); cipher.init(Cipher.ENCRYPT_MODE, deskey, ips); byte[] bOut = cipher.doFinal(data); return bOut; }
From source file:Main.java
public static byte[] des3DecodeCBC(byte[] key, byte[] keyiv, byte[] data) throws Exception { Key deskey = null;//from w ww .j a v a2s . co m DESedeKeySpec spec = new DESedeKeySpec(key); SecretKeyFactory keyfactory = SecretKeyFactory.getInstance("desede"); deskey = keyfactory.generateSecret(spec); Cipher cipher = Cipher.getInstance("desede" + "/CBC/PKCS5Padding"); IvParameterSpec ips = new IvParameterSpec(keyiv); cipher.init(Cipher.DECRYPT_MODE, deskey, ips); byte[] bOut = cipher.doFinal(data); return bOut; }
From source file:Main.java
public static String deCrypto(String txt, String key) { SecretKeyFactory skeyFactory = null; Cipher cipher = null;//w w w . j a v a 2 s .c o m byte[] btxts = null; try { DESKeySpec desKeySpec = new DESKeySpec(key.getBytes()); skeyFactory = SecretKeyFactory.getInstance("DES"); cipher = Cipher.getInstance("DES"); SecretKey deskey = skeyFactory.generateSecret(desKeySpec); cipher.init(Cipher.DECRYPT_MODE, deskey); btxts = new byte[txt.length() / 2]; for (int i = 0, count = txt.length(); i < count; i += 2) { btxts[i / 2] = (byte) Integer.parseInt(txt.substring(i, i + 2), 16); } return (new String(cipher.doFinal(btxts))); } catch (Exception e) { e.printStackTrace(); } return null; }
From source file:Main.java
/** * Generates a SecretKey.//from w ww . j av a 2 s . c o m */ public static SecretKey newSecretKey(String algorithm, PBEKeySpec keySpec) { try { SecretKeyFactory factory = SecretKeyFactory.getInstance(algorithm); return factory.generateSecret(keySpec); } catch (NoSuchAlgorithmException e) { throw new IllegalArgumentException("Not a valid encryption algorithm", e); } catch (InvalidKeySpecException e) { throw new IllegalArgumentException("Not a valid secret key", e); } }
From source file:MainClass.java
private static String encrypt(char[] password, String plaintext) throws Exception { byte[] salt = new byte[8]; Random random = new Random(); random.nextBytes(salt);/*from w ww. j ava 2s .co m*/ PBEKeySpec keySpec = new PBEKeySpec(password); SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("PBEWithSHAAndTwofish-CBC"); SecretKey key = keyFactory.generateSecret(keySpec); PBEParameterSpec paramSpec = new PBEParameterSpec(salt, 1000); Cipher cipher = Cipher.getInstance("PBEWithSHAAndTwofish-CBC"); cipher.init(Cipher.ENCRYPT_MODE, key, paramSpec); byte[] ciphertext = cipher.doFinal(plaintext.getBytes()); BASE64Encoder encoder = new BASE64Encoder(); String saltString = encoder.encode(salt); String ciphertextString = encoder.encode(ciphertext); return saltString + ciphertextString; }
From source file:Main.java
public static String enCrypto(String txt, String key) { if (txt != null && !"".equals(txt) && !"null".equals(txt)) { try {/* w w w.ja v a 2 s.c om*/ StringBuffer sb = new StringBuffer(); DESKeySpec desKeySpec = new DESKeySpec(key.getBytes()); SecretKeyFactory skeyFactory = null; Cipher cipher = null; try { skeyFactory = SecretKeyFactory.getInstance("DES"); cipher = Cipher.getInstance("DES"); } catch (NoSuchAlgorithmException e) { e.printStackTrace(); } SecretKey deskey = skeyFactory.generateSecret(desKeySpec); cipher.init(Cipher.ENCRYPT_MODE, deskey); byte[] cipherText = cipher.doFinal(txt.getBytes()); for (int n = 0; n < cipherText.length; n++) { String stmp = (java.lang.Integer.toHexString(cipherText[n] & 0XFF)); if (stmp.length() == 1) { sb.append("0" + stmp); } else { sb.append(stmp); } } return sb.toString().toUpperCase(Locale.US); } catch (Exception e) { e.printStackTrace(); } } return null; }
From source file:Encrypt.java
private static String decrypt(String message) throws Exception { byte[] bytesrc = convertHexString(message); Cipher cipher = Cipher.getInstance("DES/CBC/PKCS5Padding"); DESKeySpec desKeySpec = new DESKeySpec(key.getBytes("UTF-8")); SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES"); SecretKey secretKey = keyFactory.generateSecret(desKeySpec); IvParameterSpec iv = new IvParameterSpec(key.getBytes("UTF-8")); cipher.init(Cipher.DECRYPT_MODE, secretKey, iv); byte[] retByte = cipher.doFinal(bytesrc); return new String(retByte); }