List of usage examples for javax.crypto Cipher getInstance
public static final Cipher getInstance(String transformation) throws NoSuchAlgorithmException, NoSuchPaddingException
From source file:MainClass.java
public static void main(String args[]) throws Exception { Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider()); SecretKey secretKey;/*from www . j a v a 2s . 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 keyGenerator = KeyGenerator.getInstance("Blowfish"); keyGenerator.init(128);/* w ww . ja v a 2 s . 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: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); 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 { ObjectInputStream ois = new ObjectInputStream(new FileInputStream("keyfile")); DESKeySpec ks = new DESKeySpec((byte[]) ois.readObject()); SecretKeyFactory skf = SecretKeyFactory.getInstance("DES"); SecretKey key = skf.generateSecret(ks); Cipher c = Cipher.getInstance("DES/CFB8/NoPadding"); c.init(Cipher.DECRYPT_MODE, key, new IvParameterSpec((byte[]) ois.readObject())); CipherInputStream cis = new CipherInputStream(new FileInputStream("ciphertext"), c); BufferedReader br = new BufferedReader(new InputStreamReader(cis)); System.out.println(br.readLine()); }
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: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 { 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 ww w . j av a2s. co 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: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);//from w w w . j ava2 s . 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"); }
From source file:com.cfs.util.AESCriptografia.java
public static void main(String[] args) { AESCriptografia aes = new AESCriptografia(); String msg = "Teste4"; String key = "8TScvUZRTmS8V6hd/cZt/A=="; try {/*from www. j a v a 2 s .co m*/ System.out.println(" AES - " + aes.testeCifrar(msg, key)); String teste = "AES/ECB/PKCS5PADDING"; Cipher cipher = Cipher.getInstance(teste); cipher.init(Cipher.ENCRYPT_MODE, aes.gerarChave(key)); byte[] original = msg.getBytes(); byte[] cifrada = cipher.doFinal(original); byte[] retorno = Base64.encodeBase64(cifrada); System.out.println(teste + " - " + new String(retorno)); } catch (Exception e) { System.out.println(Utilities.getInstance().getLineNumber() + e.getLocalizedMessage()); } }