List of usage examples for javax.crypto KeyGenerator getInstance
public static final KeyGenerator getInstance(String algorithm) throws NoSuchAlgorithmException
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);//from w ww . j av a2s. c om 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:DesEncrypter.java
public static void main(String[] argv) throws Exception { SecretKey key = KeyGenerator.getInstance("DES").generateKey(); DesEncrypter encrypter = new DesEncrypter(key); String encrypted = encrypter.encrypt("Don't tell anybody!"); String decrypted = encrypter.decrypt(encrypted); }
From source file:DesEncrypter.java
public static void main(String[] argv) throws Exception { SecretKey key = KeyGenerator.getInstance("DES").generateKey(); DesEncrypter encrypter = new DesEncrypter(key); encrypter.encrypt(new FileInputStream("cleartext1"), new FileOutputStream("ciphertext")); encrypter.decrypt(new FileInputStream("ciphertext"), new FileOutputStream("cleartext2")); }
From source file:AESTest.java
public static void main(String[] args) { try {/* ww w . j av a 2s .c om*/ if (args[0].equals("-genkey")) { KeyGenerator keygen = KeyGenerator.getInstance("AES"); SecureRandom random = new SecureRandom(); keygen.init(random); SecretKey key = keygen.generateKey(); ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream(args[1])); out.writeObject(key); out.close(); } else { int mode; if (args[0].equals("-encrypt")) mode = Cipher.ENCRYPT_MODE; else mode = Cipher.DECRYPT_MODE; ObjectInputStream keyIn = new ObjectInputStream(new FileInputStream(args[3])); Key key = (Key) keyIn.readObject(); keyIn.close(); InputStream in = new FileInputStream(args[1]); OutputStream out = new FileOutputStream(args[2]); Cipher cipher = Cipher.getInstance("AES"); cipher.init(mode, key); crypt(in, out, cipher); in.close(); out.close(); } } catch (IOException e) { e.printStackTrace(); } catch (GeneralSecurityException e) { e.printStackTrace(); } catch (ClassNotFoundException e) { e.printStackTrace(); } }
From source file:RSATest.java
public static void main(String[] args) { try {/*from w w w .j a va 2 s .com*/ if (args[0].equals("-genkey")) { KeyPairGenerator pairgen = KeyPairGenerator.getInstance("RSA"); SecureRandom random = new SecureRandom(); pairgen.initialize(KEYSIZE, random); KeyPair keyPair = pairgen.generateKeyPair(); ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream(args[1])); out.writeObject(keyPair.getPublic()); out.close(); out = new ObjectOutputStream(new FileOutputStream(args[2])); out.writeObject(keyPair.getPrivate()); out.close(); } else if (args[0].equals("-encrypt")) { KeyGenerator keygen = KeyGenerator.getInstance("AES"); SecureRandom random = new SecureRandom(); keygen.init(random); SecretKey key = keygen.generateKey(); // wrap with RSA public key ObjectInputStream keyIn = new ObjectInputStream(new FileInputStream(args[3])); Key publicKey = (Key) keyIn.readObject(); keyIn.close(); Cipher cipher = Cipher.getInstance("RSA"); cipher.init(Cipher.WRAP_MODE, publicKey); byte[] wrappedKey = cipher.wrap(key); DataOutputStream out = new DataOutputStream(new FileOutputStream(args[2])); out.writeInt(wrappedKey.length); out.write(wrappedKey); InputStream in = new FileInputStream(args[1]); cipher = Cipher.getInstance("AES"); cipher.init(Cipher.ENCRYPT_MODE, key); crypt(in, out, cipher); in.close(); out.close(); } else { DataInputStream in = new DataInputStream(new FileInputStream(args[1])); int length = in.readInt(); byte[] wrappedKey = new byte[length]; in.read(wrappedKey, 0, length); // unwrap with RSA private key ObjectInputStream keyIn = new ObjectInputStream(new FileInputStream(args[3])); Key privateKey = (Key) keyIn.readObject(); keyIn.close(); Cipher cipher = Cipher.getInstance("RSA"); cipher.init(Cipher.UNWRAP_MODE, privateKey); Key key = cipher.unwrap(wrappedKey, "AES", Cipher.SECRET_KEY); OutputStream out = new FileOutputStream(args[2]); cipher = Cipher.getInstance("AES"); cipher.init(Cipher.DECRYPT_MODE, key); crypt(in, out, cipher); in.close(); out.close(); } } catch (IOException e) { e.printStackTrace(); } catch (GeneralSecurityException e) { e.printStackTrace(); } catch (ClassNotFoundException e) { e.printStackTrace(); } }
From source file:org.apache.juddi.samples.DES.java
public static void main(String[] args) throws Exception { DES des = new DES(); KeyGenerator kgen;// www .ja v a 2 s . c om try { kgen = KeyGenerator.getInstance(DESEDE_ENCRYPTION_SCHEME); kgen.init(168); SecretKey skey = kgen.generateKey(); byte[] raw = skey.getEncoded(); myEncryptionKey = new String(Base64.encodeBase64(raw)); myEncryptionScheme = DESEDE_ENCRYPTION_SCHEME; arrayBytes = myEncryptionKey.getBytes(UNICODE_FORMAT); ks = new DESedeKeySpec(arrayBytes); skf = SecretKeyFactory.getInstance(myEncryptionScheme); cipher = Cipher.getInstance(myEncryptionScheme); key = skf.generateSecret(ks); System.out.println(new String(Base64.encodeBase64(raw))); System.out.println(des.encrypt("password")); System.out.println(des.decrypt(des.encrypt("password"))); } catch (Exception ex) { ex.printStackTrace(); ; } }
From source file:backtype.storm.security.serialization.BlowfishTupleSerializer.java
/** * Produce a blowfish key to be used in "Storm jar" command */// w w w . java 2s . com 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.apache.storm.security.serialization.BlowfishTupleSerializer.java
/** * Produce a blowfish key to be used in "Storm jar" command *//*from w w w.j ava 2 s . c o m*/ 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()); } }
From source file:org.openanzo.security.keystore.TestSecretKeyEncoder.java
/** * Main method used to generate a keystore. Useful for bootstrapping the first time. * /*ww w . ja v a2s .c om*/ * @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.metron.dataservices.auth.AuthToken.java
public static void main(String[] args) throws Exception { Options options = new Options(); options.addOption("keystoreFile", true, "Keystore File"); options.addOption("keystorePassword", true, "Keystore Password"); options.addOption("authTokenAlias", true, ""); CommandLineParser parser = new GnuParser(); CommandLine cmd = parser.parse(options, args); try {/*ww w. j ava2 s. c o m*/ KeyStore ks = KeyStore.getInstance("JCEKS"); String keystorePassword = cmd.getOptionValue("keystorePassword"); String keystoreFile = cmd.getOptionValue("keystoreFile"); String authTokenAlias = cmd.getOptionValue("authTokenAlias"); ks.load(null, keystorePassword.toCharArray()); // generate a key and store it in the keystore... KeyGenerator keyGen = KeyGenerator.getInstance("AES"); SecretKey key = keyGen.generateKey(); KeyStore.ProtectionParameter protParam = new KeyStore.PasswordProtection( keystorePassword.toCharArray()); KeyStore.SecretKeyEntry skEntry = new KeyStore.SecretKeyEntry(key); ks.setEntry(authTokenAlias, skEntry, protParam); java.io.FileOutputStream fos = null; try { fos = new java.io.FileOutputStream(keystoreFile); ks.store(fos, keystorePassword.toCharArray()); } finally { if (fos != null) { fos.close(); } } System.out.println("done"); } catch (Exception e) { e.printStackTrace(); } }