List of usage examples for javax.crypto KeyGenerator getInstance
public static final KeyGenerator getInstance(String algorithm) throws NoSuchAlgorithmException
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 w w . jav a 2 s . com }
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);/* ww w . ja va2s. 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: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 av a2 s. com*/ // Generate a triple DES key keyGen = KeyGenerator.getInstance("DESede"); key = keyGen.generateKey(); }
From source file:MainClass.java
public static void main(String[] args) throws Exception { KeyGenerator keyGenerator = KeyGenerator.getInstance("Blowfish"); keyGenerator.init(128);// w w w .j a v a2 s .c om 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 ava 2 s .co 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: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 { SecretKey key = KeyGenerator.getInstance("DES").generateKey(); Cipher ecipher = Cipher.getInstance("DES"); ecipher.init(Cipher.ENCRYPT_MODE, key); SealedObject so = new SealedObject(new MySecretClass(), ecipher); String algoName = so.getAlgorithm(); // DES Cipher dcipher = Cipher.getInstance("DES"); dcipher.init(Cipher.DECRYPT_MODE, key); MySecretClass o = (MySecretClass) so.getObject(dcipher); }
From source file:MainClass.java
public static void main(String args[]) throws Exception { KeyGenerator kg = KeyGenerator.getInstance("DES"); Cipher c = Cipher.getInstance("DES/CBC/PKCS5Padding"); Key key = kg.generateKey();// w w w . j a v a 2s . co m c.init(Cipher.ENCRYPT_MODE, key); byte input[] = "Stand and unfold yourself".getBytes(); byte encrypted[] = c.doFinal(input); byte iv[] = c.getIV(); IvParameterSpec dps = new IvParameterSpec(iv); c.init(Cipher.DECRYPT_MODE, key, dps); byte output[] = c.doFinal(encrypted); System.out.println(new String(output)); }
From source file:MainClass.java
public static void main(String[] args) throws Exception { String text = "java2s"; KeyGenerator keyGenerator = KeyGenerator.getInstance("Blowfish"); keyGenerator.init(128);//w ww .ja va 2 s . c om Key key = keyGenerator.generateKey(); Cipher cipher = Cipher.getInstance("Blowfish/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 { String alg = "HmacMD5"; Mac mac = Mac.getInstance(alg); KeyGenerator kg = KeyGenerator.getInstance(alg); SecretKey key = kg.generateKey(); mac.init(key);/*from w w w. j a v a 2 s . c o m*/ mac.update("test".getBytes()); byte[] b = mac.doFinal(); System.out.println(new String(b)); }