List of usage examples for javax.crypto KeyGenerator init
public final void init(int keysize)
From source file:MainClass.java
public static void main(String[] args) throws Exception { Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider()); byte[] input = "www.java2s.com".getBytes(); byte[] ivBytes = new byte[] { 0x00, 0x00, 0x00, 0x01, 0x04, 0x05, 0x06, 0x07, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01 };// w ww . j av a2 s. c o m Cipher cipher = Cipher.getInstance("AES/CTR/NoPadding", "BC"); KeyGenerator generator = KeyGenerator.getInstance("AES", "BC"); generator.init(192); Key encryptionKey = generator.generateKey(); System.out.println("key : " + Utils.toHex(encryptionKey.getEncoded())); System.out.println("input : " + new String(input)); // encryption pass 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); // decryption pass 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) + " bytes: " + ptLength); }
From source file:org.apache.juddi.samples.DES.java
public static void main(String[] args) throws Exception { DES des = new DES(); KeyGenerator kgen; try {//ww w . j ava 2 s . c om 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:AESTest.java
public static void main(String[] args) { try {//from ww w . java 2s . c o m 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 {/*w ww . j av a2 s . c o m*/ 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:Main.java
public static byte[] initKey() throws NoSuchAlgorithmException { KeyGenerator keyGen = KeyGenerator.getInstance("AES"); keyGen.init(256); //192 256 SecretKey secretKey = keyGen.generateKey(); return secretKey.getEncoded(); }
From source file:Main.java
public static byte[] initKey(int size) throws NoSuchAlgorithmException { KeyGenerator kg = KeyGenerator.getInstance(KEY_ALGORITHM); kg.init(size); SecretKey secretKey = kg.generateKey(); return secretKey.getEncoded(); }
From source file:Main.java
public static SecretKey generateAesKey() { try {/*from ww w . ja v a2s.c om*/ KeyGenerator kg = KeyGenerator.getInstance("AES"); kg.init(KEY_LENGTH_AES); SecretKey key = kg.generateKey(); return key; } catch (NoSuchAlgorithmException e) { throw new RuntimeException(e); } }
From source file:Main.java
private static byte[] iv() { byte[] iv = null; try {/* w w w. j ava 2 s.c om*/ KeyGenerator kgen = KeyGenerator.getInstance("AES"); kgen.init(128); SecretKey skey = kgen.generateKey(); iv = skey.getEncoded(); } catch (Exception e) { e.printStackTrace(); } return iv; }
From source file:Main.java
private static Key newDesInstance(String strKey) { Key key = null;// w w w . j a v a2s . c o m try { KeyGenerator keyGenerator = KeyGenerator.getInstance("DES"); keyGenerator.init(new SecureRandom(strKey.getBytes())); key = keyGenerator.generateKey(); keyGenerator = null; } catch (Exception e) { e.printStackTrace(); } return key; }
From source file:com.java.demo.DesDemo.java
public static void Init() { try {//from ww w.j ava 2 s . c o 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) { } }