List of usage examples for javax.crypto Cipher getInstance
public static final Cipher getInstance(String transformation, Provider provider) throws NoSuchAlgorithmException, NoSuchPaddingException
From source file:Main.java
public static void main(String[] args) throws Exception { //Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider()); byte[] input = "ab".getBytes(); Cipher cipher = Cipher.getInstance("ElGamal/None/NoPadding", "BC"); KeyPairGenerator generator = KeyPairGenerator.getInstance("ElGamal", "BC"); SecureRandom random = new SecureRandom(); generator.initialize(128, random);/* w w w.j ava 2s .com*/ KeyPair pair = generator.generateKeyPair(); Key pubKey = pair.getPublic(); Key privKey = pair.getPrivate(); cipher.init(Cipher.ENCRYPT_MODE, pubKey, random); byte[] cipherText = cipher.doFinal(input); System.out.println("cipher: " + new String(cipherText)); cipher.init(Cipher.DECRYPT_MODE, privKey); byte[] plainText = cipher.doFinal(cipherText); System.out.println("plain : " + new String(plainText)); }
From source file:MainClass.java
public static void main(String[] args) throws Exception { Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider()); byte[] input = "ab".getBytes(); Cipher cipher = Cipher.getInstance("ElGamal/None/NoPadding", "BC"); KeyPairGenerator generator = KeyPairGenerator.getInstance("ElGamal", "BC"); SecureRandom random = new SecureRandom(); generator.initialize(128, random);/*from ww w . ja v a 2 s . c o m*/ KeyPair pair = generator.generateKeyPair(); Key pubKey = pair.getPublic(); Key privKey = pair.getPrivate(); cipher.init(Cipher.ENCRYPT_MODE, pubKey, random); byte[] cipherText = cipher.doFinal(input); System.out.println("cipher: " + new String(cipherText)); cipher.init(Cipher.DECRYPT_MODE, privKey); byte[] plainText = cipher.doFinal(cipherText); System.out.println("plain : " + new String(plainText)); }
From source file:MainClass.java
public static void main(String[] args) throws Exception { Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider()); byte[] input = "abc".getBytes(); Cipher cipher = Cipher.getInstance("RSA/None/PKCS1Padding", "BC"); SecureRandom random = new SecureRandom(); KeyPairGenerator generator = KeyPairGenerator.getInstance("RSA", "BC"); generator.initialize(256, random);//from w w w .j a va2 s. co m KeyPair pair = generator.generateKeyPair(); Key pubKey = pair.getPublic(); Key privKey = pair.getPrivate(); cipher.init(Cipher.ENCRYPT_MODE, pubKey, random); byte[] cipherText = cipher.doFinal(input); System.out.println("cipher: " + new String(cipherText)); cipher.init(Cipher.DECRYPT_MODE, privKey); byte[] plainText = cipher.doFinal(cipherText); System.out.println("plain : " + new String(plainText)); }
From source file:MainClass.java
public static void main(String[] args) throws Exception { Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider()); byte[] input = "abc".getBytes(); Cipher cipher = Cipher.getInstance("RSA/None/OAEPWithSHA1AndMGF1Padding", "BC"); SecureRandom random = new SecureRandom(); KeyPairGenerator generator = KeyPairGenerator.getInstance("RSA", "BC"); generator.initialize(386, random);// ww w . j a v a 2 s. co m KeyPair pair = generator.generateKeyPair(); Key pubKey = pair.getPublic(); Key privKey = pair.getPrivate(); cipher.init(Cipher.ENCRYPT_MODE, pubKey, random); byte[] cipherText = cipher.doFinal(input); System.out.println("cipher: " + new String(cipherText)); cipher.init(Cipher.DECRYPT_MODE, privKey); byte[] plainText = cipher.doFinal(cipherText); System.out.println("plain : " + new String(plainText)); }
From source file:Main.java
public static void main(String[] args) throws Exception { //Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider()); byte[] input = "input".getBytes(); byte[] keyBytes = "input123".getBytes(); SecretKeySpec key = new SecretKeySpec(keyBytes, "ARC4"); Cipher cipher = Cipher.getInstance("ARC4", "BC"); byte[] cipherText = new byte[input.length]; cipher.init(Cipher.ENCRYPT_MODE, key); int ctLength = cipher.update(input, 0, input.length, cipherText, 0); ctLength += cipher.doFinal(cipherText, ctLength); System.out.println("cipher text: " + new String(cipherText)); byte[] plainText = new byte[ctLength]; cipher.init(Cipher.DECRYPT_MODE, key); int ptLength = cipher.update(cipherText, 0, ctLength, plainText, 0); ptLength += cipher.doFinal(plainText, ptLength); System.out.println("plain text : " + new String(plainText)); }
From source file:MainClass.java
public static void main(String[] args) throws Exception { Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider()); byte[] input = "input".getBytes(); byte[] keyBytes = "input123".getBytes(); SecretKeySpec key = new SecretKeySpec(keyBytes, "ARC4"); Cipher cipher = Cipher.getInstance("ARC4", "BC"); byte[] cipherText = new byte[input.length]; cipher.init(Cipher.ENCRYPT_MODE, key); int ctLength = cipher.update(input, 0, input.length, cipherText, 0); ctLength += cipher.doFinal(cipherText, ctLength); System.out.println("cipher text: " + new String(cipherText)); byte[] plainText = new byte[ctLength]; cipher.init(Cipher.DECRYPT_MODE, key); int ptLength = cipher.update(cipherText, 0, ctLength, plainText, 0); ptLength += cipher.doFinal(plainText, ptLength); System.out.println("plain text : " + new String(plainText)); }
From source file:Main.java
public static void main(String[] args) throws Exception { // Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider()); byte[] input = "input".getBytes(); byte[] ivBytes = "1234567812345678".getBytes(); Cipher cipher = Cipher.getInstance("AES/CTR/NoPadding", "BC"); KeyGenerator generator = KeyGenerator.getInstance("AES", "BC"); generator.init(128);//from www . j a va2 s . co m Key encryptionKey = generator.generateKey(); System.out.println("key : " + new String(encryptionKey.getEncoded())); 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); 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)); }
From source file:MainClass.java
public static void main(String[] args) throws Exception { Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider()); byte[] input = "input".getBytes(); byte[] keyBytes = new byte[] { 0x01, 0x23, 0x45, 0x67, (byte) 0x89, (byte) 0xab, (byte) 0xcd, (byte) 0xef }; SecretKeySpec key = new SecretKeySpec(keyBytes, "DES"); Cipher cipher = Cipher.getInstance("DES/ECB/PKCS7Padding", "BC"); cipher.init(Cipher.ENCRYPT_MODE, key); byte[] cipherText = new byte[cipher.getOutputSize(input.length)]; int ctLength = cipher.update(input, 0, input.length, cipherText, 0); ctLength += cipher.doFinal(cipherText, ctLength); cipher.init(Cipher.DECRYPT_MODE, key); 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)); }
From source file:MainClass.java
public static void main(String[] args) throws Exception { Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider()); Cipher cipher = Cipher.getInstance("AES/ECB/PKCS7Padding", "BC"); SecureRandom random = new SecureRandom(); KeyPairGenerator fact = KeyPairGenerator.getInstance("RSA", "BC"); fact.initialize(1024, random);//from w w w.jav a2s . c o m KeyPair keyPair = fact.generateKeyPair(); Key wrapKey = createKeyForAES(256, random); cipher.init(Cipher.WRAP_MODE, wrapKey); byte[] wrappedKey = cipher.wrap(keyPair.getPrivate()); cipher.init(Cipher.UNWRAP_MODE, wrapKey); Key key = cipher.unwrap(wrappedKey, "RSA", Cipher.PRIVATE_KEY); System.out.println(keyPair.getPrivate().equals(key)); }
From source file:MainClass.java
public static void main(String[] args) throws Exception { Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider()); byte[] input = "input".getBytes(); byte[] keyBytes = new byte[] { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f };/*from ww w . jav a 2s. c om*/ SecretKeySpec key = new SecretKeySpec(keyBytes, "AES"); Cipher cipher = Cipher.getInstance("AES/ECB/PKCS7Padding", "BC"); cipher.init(Cipher.ENCRYPT_MODE, key); byte[] cipherText = new byte[cipher.getOutputSize(input.length)]; int ctLength = cipher.update(input, 0, input.length, cipherText, 0); ctLength += cipher.doFinal(cipherText, ctLength); cipher.init(Cipher.DECRYPT_MODE, key); 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)); }