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) 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 keyGenerator = KeyGenerator.getInstance("Blowfish"); keyGenerator.init(128);//w ww . j av a 2s . c o m 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:MainClass.java
public static void main(String[] args) throws Exception { KeyGenerator keyGenerator = KeyGenerator.getInstance("Blowfish"); keyGenerator.init(128);//from w w w . j a va 2s . co m 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: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 { Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider()); SecretKey key = KeyGenerator.getInstance("DES").generateKey(); // for CBC; must be 8 bytes byte[] initVector = new byte[] { 0x10, 0x10, 0x01, 0x04, 0x01, 0x01, 0x01, 0x02 }; AlgorithmParameterSpec algParamSpec = new IvParameterSpec(initVector); Cipher m_encrypter = Cipher.getInstance("DES/CBC/PKCS5Padding"); Cipher m_decrypter = Cipher.getInstance("DES/CBC/PKCS5Padding"); m_encrypter.init(Cipher.ENCRYPT_MODE, key, algParamSpec); m_decrypter.init(Cipher.DECRYPT_MODE, key, algParamSpec); byte[] clearText = "www.java2s.com".getBytes(); byte[] encryptedText = m_encrypter.doFinal(clearText); byte[] decryptedText = m_decrypter.doFinal(encryptedText); System.out.println(new String(clearText)); System.out.println(new String(encryptedText)); System.out.println(new String(decryptedText)); }
From source file:MainClass.java
public static void main(String args[]) throws Exception { KeyGenerator keyGen = KeyGenerator.getInstance("DES"); SecureRandom random = SecureRandom.getInstance("SHA1PRNG", "SUN"); random.setSeed(101L);//from www.ja v a 2 s . c om keyGen.init(56, random); SecretKey sKey = keyGen.generateKey(); SecretKeyFactory kfactory = SecretKeyFactory.getInstance("DES"); DESKeySpec kspec = (DESKeySpec) kfactory.getKeySpec(sKey, DESKeySpec.class); System.out.println(sKey); FileOutputStream fos = new FileOutputStream("secretKeys"); ObjectOutputStream oos = new ObjectOutputStream(fos); oos.writeObject(kspec.getKey()); FileInputStream fin = new FileInputStream("secretKeys"); ObjectInputStream ois = new ObjectInputStream(fin); byte[] kMaterial = (byte[]) ois.readObject(); DESKeySpec keyspec = new DESKeySpec(kMaterial); SecretKey newKey = kfactory.generateSecret(keyspec); System.out.println(newKey); System.out.println("Do the keys equal :" + newKey.equals(sKey)); }
From source file:MainClass.java
public static void main(String args[]) throws Exception { Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider()); SecretKey secretKey;// www . ja va 2 s . c o m Cipher encrypter, decrypter; secretKey = KeyGenerator.getInstance("DES").generateKey(); encrypter = Cipher.getInstance("DES"); encrypter.init(Cipher.ENCRYPT_MODE, secretKey); decrypter = Cipher.getInstance("DES"); decrypter.init(Cipher.DECRYPT_MODE, secretKey); MyClass cust, unsealed; SealedObject sealed; cust = new MyClass(); cust.name = "Paul"; cust.password = "password"; // Seal it, storing it in a SealedObject sealed = (new SealedObject(cust, encrypter)); // Try unsealing it String algorithmName = sealed.getAlgorithm(); System.out.println(algorithmName); unsealed = (MyClass) sealed.getObject(decrypter); System.out.println("NAME: " + unsealed.name); System.out.println("PASSWORD: " + unsealed.password); }
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 va 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(); }
From source file:Main.java
public static void main(String[] args) throws Exception { SecretKey key = KeyGenerator.getInstance("DES").generateKey(); writeToFile("secretkey.dat", key); Cipher cipher = Cipher.getInstance("DES"); cipher.init(Cipher.ENCRYPT_MODE, key); SealedObject sealedObject = new SealedObject("THIS IS A SECRET MESSAGE!", cipher); writeToFile("sealed.dat", sealedObject); }
From source file:MainClass.java
public static void main(String args[]) throws Exception { Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider()); SecretKey key = KeyGenerator.getInstance("DES").generateKey(); // for CBC; must be 8 bytes byte[] initVector = new byte[] { 0x10, 0x10, 0x01, 0x04, 0x01, 0x01, 0x01, 0x02 }; AlgorithmParameterSpec algParamSpec = new IvParameterSpec(initVector); Cipher m_encrypter = Cipher.getInstance("DES/CBC/PKCS5Padding"); Cipher m_decrypter = Cipher.getInstance("DES/CBC/PKCS5Padding"); m_encrypter.init(Cipher.ENCRYPT_MODE, key, algParamSpec); m_decrypter.init(Cipher.DECRYPT_MODE, key, algParamSpec); FileInputStream fis = new FileInputStream("cipherTest.in"); FileOutputStream fos = new FileOutputStream("cipherTest.out"); int dataInputSize = fis.available(); byte[] inputBytes = new byte[dataInputSize]; fis.read(inputBytes);/* w w w. j a va2s. co m*/ write(inputBytes, fos); fos.flush(); fis.close(); fos.close(); String inputFileAsString = new String(inputBytes); System.out.println("INPUT FILE CONTENTS\n" + inputFileAsString + "\n"); System.out.println("File encrypted and saved to disk\n"); fis = new FileInputStream("cipherTest.out"); byte[] decrypted = new byte[dataInputSize]; read(decrypted, fis); fis.close(); String decryptedAsString = new String(decrypted); System.out.println("DECRYPTED FILE:\n" + decryptedAsString + "\n"); }