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 text = "java2s"; KeyGenerator keyGenerator = KeyGenerator.getInstance("Blowfish"); keyGenerator.init(128);/*from w w w . j a va 2s .c o m*/ 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 { KeyGenerator kg = KeyGenerator.getInstance("DES"); Cipher c = Cipher.getInstance("DES/CBC/PKCS5Padding"); Key key = kg.generateKey(); 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 { Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider()); KeyGenerator generator = KeyGenerator.getInstance("AES", "BC"); generator.init(128);//from www.jav a 2s . c om Key keyToBeWrapped = generator.generateKey(); System.out.println("input : " + new String(keyToBeWrapped.getEncoded())); // create a wrapper and do the wrapping Cipher cipher = Cipher.getInstance("AES/ECB/NoPadding", "BC"); KeyGenerator keyGen = KeyGenerator.getInstance("AES", "BC"); keyGen.init(256); Key wrapKey = keyGen.generateKey(); cipher.init(Cipher.ENCRYPT_MODE, wrapKey); byte[] wrappedKey = cipher.doFinal(keyToBeWrapped.getEncoded()); System.out.println("wrapped : " + new String(wrappedKey)); // unwrap the wrapped key cipher.init(Cipher.DECRYPT_MODE, wrapKey); Key key = new SecretKeySpec(cipher.doFinal(wrappedKey), "AES"); System.out.println("unwrapped: " + new String(key.getEncoded())); }
From source file:MainClass.java
public static void main(String[] args) { if (args.length != 2) { String err = "Usage: KeyGeneratorApp algorithmName keySize"; System.out.println(err);//w ww. j a v a 2 s . c o m System.exit(0); } int keySize = (new Integer(args[1])).intValue(); SecretKey skey = null; KeyPair keys = null; String algorithm = args[0]; try { KeyPairGenerator kpg = KeyPairGenerator.getInstance(algorithm); kpg.initialize(keySize); keys = kpg.genKeyPair(); } catch (NoSuchAlgorithmException ex1) { try { KeyGenerator kg = KeyGenerator.getInstance(algorithm); kg.init(keySize); skey = kg.generateKey(); } catch (NoSuchAlgorithmException ex2) { System.out.println("Algorithm not supported: " + algorithm); System.exit(0); } } }
From source file:MainClass.java
public static void main(String[] args) throws Exception { KeyGenerator keyGenerator = KeyGenerator.getInstance("Blowfish"); keyGenerator.init(128);/* w w w. java 2s. com*/ Key blowfishKey = keyGenerator.generateKey(); KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("RSA"); keyPairGenerator.initialize(1024); KeyPair keyPair = keyPairGenerator.genKeyPair(); Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding"); cipher.init(Cipher.ENCRYPT_MODE, keyPair.getPublic()); byte[] blowfishKeyBytes = blowfishKey.getEncoded(); System.out.println(new String(blowfishKeyBytes)); byte[] cipherText = cipher.doFinal(blowfishKeyBytes); System.out.println(new String(cipherText)); cipher.init(Cipher.DECRYPT_MODE, keyPair.getPrivate()); byte[] decryptedKeyBytes = cipher.doFinal(cipherText); System.out.println(new String(decryptedKeyBytes)); SecretKey newBlowfishKey = new SecretKeySpec(decryptedKeyBytes, "Blowfish"); }
From source file:Main.java
public static void main(String[] args) throws Exception { // Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider()); byte[] input = "input".getBytes(); byte[] ivBytes = "1234567812345678".getBytes(); Cipher cipher = Cipher.getInstance("AES/CTR/NoPadding", "BC"); KeyGenerator generator = KeyGenerator.getInstance("AES", "BC"); generator.init(128);/*from ww w.j ava 2 s . c o m*/ Key encryptionKey = generator.generateKey(); System.out.println("key : " + new String(encryptionKey.getEncoded())); cipher.init(Cipher.ENCRYPT_MODE, encryptionKey, new IvParameterSpec(ivBytes)); byte[] cipherText = new byte[cipher.getOutputSize(input.length)]; int ctLength = cipher.update(input, 0, input.length, cipherText, 0); ctLength += cipher.doFinal(cipherText, ctLength); Key decryptionKey = new SecretKeySpec(encryptionKey.getEncoded(), encryptionKey.getAlgorithm()); cipher.init(Cipher.DECRYPT_MODE, decryptionKey, new IvParameterSpec(ivBytes)); byte[] plainText = new byte[cipher.getOutputSize(ctLength)]; int ptLength = cipher.update(cipherText, 0, ctLength, plainText, 0); ptLength += cipher.doFinal(plainText, ptLength); System.out.println("plain : " + new String(plainText)); }
From source file:MainClass.java
public static void main(String[] args) throws Exception { KeyGenerator keyGenerator = KeyGenerator.getInstance("Blowfish"); keyGenerator.init(128);//from w ww.j a v a2 s .com Key secretKey = keyGenerator.generateKey(); Cipher cipherOut = Cipher.getInstance("Blowfish/CFB/NoPadding"); cipherOut.init(Cipher.ENCRYPT_MODE, secretKey); BASE64Encoder encoder = new BASE64Encoder(); byte iv[] = cipherOut.getIV(); if (iv != null) { System.out.println("Initialization Vector of the Cipher:\n" + encoder.encode(iv)); } FileInputStream fin = new FileInputStream("inputFile.txt"); FileOutputStream fout = new FileOutputStream("outputFile.txt"); CipherOutputStream cout = new CipherOutputStream(fout, cipherOut); int input = 0; while ((input = fin.read()) != -1) { cout.write(input); } fin.close(); cout.close(); }
From source file:backtype.storm.security.serialization.BlowfishTupleSerializer.java
/** * Produce a blowfish key to be used in "Storm jar" command */// w w w. j a va 2 s .c o m public static void main(String[] args) { try { KeyGenerator kgen = KeyGenerator.getInstance("Blowfish"); SecretKey skey = kgen.generateKey(); byte[] raw = skey.getEncoded(); String keyString = new String(Hex.encodeHex(raw)); System.out.println("storm -c " + SECRET_KEY + "=" + keyString + " -c " + Config.TOPOLOGY_TUPLE_SERIALIZER + "=" + BlowfishTupleSerializer.class.getName() + " ..."); } catch (Exception ex) { LOG.error(ex.getMessage()); ex.printStackTrace(); } }
From source file:org.openanzo.security.keystore.TestSecretKeyEncoder.java
/** * Main method used to generate a keystore. Useful for bootstrapping the first time. * // w w w.jav a 2 s. co m * @param args * @throws Exception */ public static void main(String[] args) throws Exception { File file = new File("testKeystore"); System.out.println("Generating new keystore to:" + file.getAbsolutePath()); KeyStore keyStore = KeyStore.getInstance("JCEKS"); keyStore.load(null, TEST_KEYSTORE_PASSWORD); KeyGenerator kgen = KeyGenerator.getInstance(ALGORITHM); Key key = kgen.generateKey(); keyStore.setKeyEntry(KEY_NAME, key, TEST_KEYSTORE_PASSWORD, new Certificate[0]); keyStore.store(FileUtils.openOutputStream(file), TEST_KEYSTORE_PASSWORD); System.out.println("Done generating keystore."); }
From source file:org.apache.storm.security.serialization.BlowfishTupleSerializer.java
/** * Produce a blowfish key to be used in "Storm jar" command *//*from ww w . j a va 2 s. c om*/ public static void main(String[] args) { try { KeyGenerator kgen = KeyGenerator.getInstance("Blowfish"); kgen.init(256); SecretKey skey = kgen.generateKey(); byte[] raw = skey.getEncoded(); String keyString = new String(Hex.encodeHex(raw)); System.out.println("storm -c " + SECRET_KEY + "=" + keyString + " -c " + Config.TOPOLOGY_TUPLE_SERIALIZER + "=" + BlowfishTupleSerializer.class.getName() + " ..."); } catch (Exception ex) { LOG.error(ex.getMessage()); } }