List of usage examples for javax.crypto SecretKeyFactory generateSecret
public final SecretKey generateSecret(KeySpec keySpec) throws InvalidKeySpecException
From source file:MainClass.java
private static String decrypt(char[] password, String text) throws Exception { String salt = text.substring(0, 12); String ciphertext = text.substring(12, text.length()); BASE64Decoder decoder = new BASE64Decoder(); byte[] saltArray = decoder.decodeBuffer(salt); byte[] ciphertextArray = decoder.decodeBuffer(ciphertext); PBEKeySpec keySpec = new PBEKeySpec(password); SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("PBEWithSHAAndTwofish-CBC"); SecretKey key = keyFactory.generateSecret(keySpec); PBEParameterSpec paramSpec = new PBEParameterSpec(saltArray, 1000); Cipher cipher = Cipher.getInstance("PBEWithSHAAndTwofish-CBC"); cipher.init(Cipher.DECRYPT_MODE, key, paramSpec); return new String(cipher.doFinal(ciphertextArray)); }
From source file:MainClass.java
static Cipher createCipher(int mode) throws Exception { PBEKeySpec keySpec = new PBEKeySpec("test".toCharArray()); SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("PBEWithMD5AndDES"); SecretKey key = keyFactory.generateSecret(keySpec); MessageDigest md = MessageDigest.getInstance("MD5"); md.update("input".getBytes()); byte[] digest = md.digest(); byte[] salt = new byte[8]; for (int i = 0; i < 8; ++i) salt[i] = digest[i];/* www.j a va2 s.c o m*/ PBEParameterSpec paramSpec = new PBEParameterSpec(salt, 20); Cipher cipher = Cipher.getInstance("PBEWithMD5AndDES"); cipher.init(mode, key, paramSpec); return cipher; }
From source file:Main.java
private static byte[] doPBKDF2(char[] password, byte[] salt, int iterations, int bits) throws NoSuchAlgorithmException, InvalidKeySpecException { PBEKeySpec spec = new PBEKeySpec(password, salt, iterations, bits); SecretKeyFactory skf = SecretKeyFactory.getInstance(PBKDF2_ALGORITHM); return skf.generateSecret(spec).getEncoded(); }
From source file:Main.java
public static byte[] des3EncodeECB(byte[] key, byte[] data) throws Exception { Key deskey = null;/* w ww . ja 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.ENCRYPT_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 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" + "/CBC/PKCS5Padding"); IvParameterSpec ips = new IvParameterSpec(keyiv); cipher.init(Cipher.ENCRYPT_MODE, deskey, ips); byte[] bOut = cipher.doFinal(data); return bOut; }
From source file:com.java.demo.DesDemo.java
public static void Init() { try {/* w w w . j a va2 s. co m*/ KeyGenerator keyGenerator = KeyGenerator.getInstance("DES"); keyGenerator.init(56); SecretKey secretKey = keyGenerator.generateKey(); byte[] bytesKey = secretKey.getEncoded(); DESKeySpec dESKeySpec = new DESKeySpec(bytesKey); SecretKeyFactory factory = SecretKeyFactory.getInstance("DES"); convertSecretKey = factory.generateSecret(dESKeySpec); } catch (Exception e) { } }
From source file:Main.java
public static byte[] ees3DecodeECB(byte[] key, byte[] data) throws Exception { Key deskey = null;//w w w. j a v a2 s.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.DECRYPT_MODE, deskey); byte[] bOut = cipher.doFinal(data); return bOut; }
From source file:Main.java
/** * Generates a SecretKey.//from w ww .jav a2s. 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:Main.java
public static byte[] des3DecodeCBC(byte[] key, byte[] keyiv, byte[] data) throws Exception { Key deskey = null;//w w w .j av 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" + "/CBC/PKCS5Padding"); IvParameterSpec ips = new IvParameterSpec(keyiv); cipher.init(Cipher.DECRYPT_MODE, deskey, ips); byte[] bOut = cipher.doFinal(data); return bOut; }
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 ww w . ja v a2 s . c om*/ 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; }