List of usage examples for javax.crypto KeyGenerator generateKey
public final SecretKey generateKey()
From source file:MainClass.java
public static void main(String[] args) throws Exception { String alg = "HmacMD5"; Mac mac = Mac.getInstance(alg); KeyGenerator kg = KeyGenerator.getInstance(alg); SecretKey key = kg.generateKey(); mac.init(key);/* ww w . j ava 2 s . c o m*/ mac.update("test".getBytes()); byte[] b = mac.doFinal(); System.out.println(new String(b)); }
From source file:Main.java
public static void main(String[] argv) throws Exception { KeyGenerator keyGen = KeyGenerator.getInstance("HmacMD5"); SecretKey key = keyGen.generateKey(); // Generate a key for the HMAC-SHA1 keyed-hashing algorithm keyGen = KeyGenerator.getInstance("HmacSHA1"); key = keyGen.generateKey();/* w ww . ja v a 2s.c o m*/ }
From source file:Main.java
public static void main(String[] argv) throws Exception { // Generate a DES key KeyGenerator keyGen = KeyGenerator.getInstance("DES"); SecretKey key = keyGen.generateKey(); // Generate a Blowfish key keyGen = KeyGenerator.getInstance("Blowfish"); key = keyGen.generateKey();//w w w . j a v a2 s. c om // Generate a triple DES key keyGen = KeyGenerator.getInstance("DESede"); key = keyGen.generateKey(); }
From source file:Main.java
public static void main(String[] argv) throws Exception { // Generate a key KeyGenerator keyGen = KeyGenerator.getInstance("DESede"); SecretKey key = keyGen.generateKey(); // Get the bytes of the key byte[] keyBytes = key.getEncoded(); int numBytes = keyBytes.length; // The bytes can be converted back to a SecretKey SecretKey key2 = new SecretKeySpec(keyBytes, "DESede"); boolean b = key.equals(key2); // true }
From source file:Main.java
public static void main(String[] argv) throws Exception { KeyGenerator keyGen = KeyGenerator.getInstance("HmacMD5"); SecretKey key = keyGen.generateKey(); Mac mac = Mac.getInstance(key.getAlgorithm()); mac.init(key);/*from w w w .ja v a2 s .c o m*/ String str = "This message will be digested"; byte[] utf8 = str.getBytes("UTF8"); byte[] digest = mac.doFinal(utf8); String digestB64 = new sun.misc.BASE64Encoder().encode(digest); System.out.println(digestB64); }
From source file:MainClass.java
public static void main(String[] args) throws Exception { String creditCard = "1234567890"; KeyGenerator keyGenerator = KeyGenerator.getInstance("DESede"); Key key = keyGenerator.generateKey(); Cipher cipher = Cipher.getInstance("DESede"); cipher.init(Cipher.ENCRYPT_MODE, key); SealedObject so = new SealedObject(creditCard, cipher); String unencryptedCreditCard = (String) so.getObject(key); System.out.println(unencryptedCreditCard); }
From source file:MainClass.java
public static void main(String[] args) throws Exception { KeyGenerator kg = KeyGenerator.getInstance("DESede"); Key sharedKey = kg.generateKey(); String password = "password"; byte[] salt = "salt1234".getBytes(); PBEParameterSpec paramSpec = new PBEParameterSpec(salt, 20); PBEKeySpec keySpec = new PBEKeySpec(password.toCharArray()); SecretKeyFactory kf = SecretKeyFactory.getInstance("PBEWithMD5AndDES"); SecretKey passwordKey = kf.generateSecret(keySpec); Cipher c = Cipher.getInstance("PBEWithMD5AndDES"); c.init(Cipher.WRAP_MODE, passwordKey, paramSpec); byte[] wrappedKey = c.wrap(sharedKey); c = Cipher.getInstance("DESede"); c.init(Cipher.ENCRYPT_MODE, sharedKey); byte[] input = "input".getBytes(); byte[] encrypted = c.doFinal(input); c = Cipher.getInstance("PBEWithMD5AndDES"); c.init(Cipher.UNWRAP_MODE, passwordKey, paramSpec); Key unwrappedKey = c.unwrap(wrappedKey, "DESede", Cipher.SECRET_KEY); c = Cipher.getInstance("DESede"); c.init(Cipher.DECRYPT_MODE, unwrappedKey); System.out.println(new String(c.doFinal(encrypted))); }
From source file:MainClass.java
public static void main(String[] args) throws Exception { KeyGenerator keyGenerator = KeyGenerator.getInstance("Blowfish"); keyGenerator.init(128);/*from ww w . ja v a 2 s . com*/ SecretKey key = keyGenerator.generateKey(); Cipher cipher = Cipher.getInstance("Blowfish/ECB/PKCS5Padding"); cipher.init(Cipher.ENCRYPT_MODE, key); byte[] cipherText = cipher.doFinal("This is a test.".getBytes("UTF8")); System.out.println(new String(cipherText)); }
From source file:MainClass.java
public static void main(String[] args) throws Exception { KeyGenerator keyGenerator = KeyGenerator.getInstance("DESede"); keyGenerator.init(168);//from ww w . j a va 2 s .c o m Key key = keyGenerator.generateKey(); Cipher cipher = Cipher.getInstance("DESede/ECB/PKCS5Padding"); cipher.init(Cipher.ENCRYPT_MODE, key); byte[] ciphertext = cipher.doFinal("text".getBytes("UTF8")); for (int i = 0; i < ciphertext.length; i++) { System.out.print(ciphertext[i] + " "); } cipher.init(Cipher.DECRYPT_MODE, key); byte[] decryptedText = cipher.doFinal(ciphertext); System.out.println(new String(decryptedText, "UTF8")); }
From source file:MainClass.java
public static void main(String args[]) throws Exception { KeyGenerator kg = KeyGenerator.getInstance("DES"); kg.init(new SecureRandom()); SecretKey key = kg.generateKey(); SecretKeyFactory skf = SecretKeyFactory.getInstance("DES"); Class spec = Class.forName("javax.crypto.spec.DESKeySpec"); DESKeySpec ks = (DESKeySpec) skf.getKeySpec(key, spec); ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("keyfile")); oos.writeObject(ks.getKey());/*from w ww .j a v a 2 s . c o m*/ Cipher c = Cipher.getInstance("DES/CFB8/NoPadding"); c.init(Cipher.ENCRYPT_MODE, key); CipherOutputStream cos = new CipherOutputStream(new FileOutputStream("ciphertext"), c); PrintWriter pw = new PrintWriter(new OutputStreamWriter(cos)); pw.println("Stand and unfold yourself"); pw.close(); oos.writeObject(c.getIV()); oos.close(); }