List of usage examples for javax.crypto Cipher init
public final void init(int opmode, Certificate certificate) throws InvalidKeyException
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 { 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:Main.java
public static void main(String[] args) throws Exception { SecretKey key = (SecretKey) readFromFile("secretkey.dat"); SealedObject sealedObject = (SealedObject) readFromFile("sealed.dat"); String algorithmName = sealedObject.getAlgorithm(); Cipher cipher = Cipher.getInstance(algorithmName); cipher.init(Cipher.DECRYPT_MODE, key); String text = (String) sealedObject.getObject(cipher); System.out.println("Text = " + text); }
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 { byte[] data = { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07 }; SecretKey key64 = new SecretKeySpec(new byte[] { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07 }, "Blowfish"); Cipher c = Cipher.getInstance("Blowfish/ECB/NoPadding"); c.init(Cipher.ENCRYPT_MODE, key64); c.doFinal(data);/*from www. j av a 2s. co m*/ }
From source file:MainClass.java
public static void main(String[] args) throws Exception { KeyGenerator keyGenerator = KeyGenerator.getInstance("Blowfish"); keyGenerator.init(128);/* w w w . ja va2 s. c o m*/ 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 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 av a2 s . c om*/ 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:MainClass.java
public static void main(String[] args) throws Exception { KeyGenerator keyGenerator = KeyGenerator.getInstance("DESede"); keyGenerator.init(168);//from w w w.j a v a2s .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: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 . 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 { String text = "java2s"; KeyGenerator keyGenerator = KeyGenerator.getInstance("Blowfish"); keyGenerator.init(128);//ww w .j a v a 2 s .co 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")); }