List of usage examples for javax.crypto Cipher doFinal
public final byte[] doFinal(byte[] input) throws IllegalBlockSizeException, BadPaddingException
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 source file:MainClass.java
public static void main(String[] args) throws Exception { KeyGenerator keyGenerator = KeyGenerator.getInstance("Blowfish"); keyGenerator.init(128);//from w w w . java 2s.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 keyGenerator = KeyGenerator.getInstance("DESede"); keyGenerator.init(168);/*from ww w . j a v a2 s. c o 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 { String text = "java2s"; KeyGenerator keyGenerator = KeyGenerator.getInstance("Blowfish"); keyGenerator.init(128);/*from ww w .j a v a 2s. c o 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")); }
From source file:MainClass.java
public static void main(String[] args) throws Exception { Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider()); KeyPairGenerator keyGen = KeyPairGenerator.getInstance("RSA", "BC"); keyGen.initialize(512, new SecureRandom()); KeyPair keyPair = keyGen.generateKeyPair(); Signature signature = Signature.getInstance("SHA256withRSA", "BC"); signature.initSign(keyPair.getPrivate()); byte[] message = "abc".getBytes(); signature.update(message);/*from w ww . j av a 2 s .c o m*/ byte[] sigBytes = signature.sign(); Cipher cipher = Cipher.getInstance("RSA/None/PKCS1Padding", "BC"); cipher.init(Cipher.DECRYPT_MODE, keyPair.getPublic()); byte[] decSig = cipher.doFinal(sigBytes); ASN1InputStream aIn = new ASN1InputStream(decSig); ASN1Sequence seq = (ASN1Sequence) aIn.readObject(); System.out.println(ASN1Dump.dumpAsString(seq)); MessageDigest hash = MessageDigest.getInstance("SHA-256", "BC"); hash.update(message); ASN1OctetString sigHash = (ASN1OctetString) seq.getObjectAt(1); System.out.println(MessageDigest.isEqual(hash.digest(), sigHash.getOctets())); }
From source file:MainClass.java
public static void main(String args[]) throws Exception { KeyGenerator kg = KeyGenerator.getInstance("DES"); Cipher c = Cipher.getInstance("DES/CBC/PKCS5Padding"); Key key = kg.generateKey();//from w w w. j a va 2 s . com c.init(Cipher.ENCRYPT_MODE, key); byte input[] = "Stand and unfold yourself".getBytes(); byte encrypted[] = c.doFinal(input); byte iv[] = c.getIV(); IvParameterSpec dps = new IvParameterSpec(iv); c.init(Cipher.DECRYPT_MODE, key, dps); byte output[] = c.doFinal(encrypted); System.out.println(new String(output)); }
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(); byte[] msgNumber = "input".getBytes(); IvParameterSpec zeroIv = new IvParameterSpec(new byte[8]); SecretKeySpec key = new SecretKeySpec(keyBytes, "DES"); Cipher cipher = Cipher.getInstance("DES/CBC/PKCS7Padding", "BC"); cipher.init(Cipher.ENCRYPT_MODE, key, zeroIv); IvParameterSpec encryptionIv = new IvParameterSpec(cipher.doFinal(msgNumber), 0, 8); cipher.init(Cipher.ENCRYPT_MODE, key, encryptionIv); byte[] cipherText = new byte[cipher.getOutputSize(input.length)]; int ctLength = cipher.update(input, 0, input.length, cipherText, 0); ctLength += cipher.doFinal(cipherText, ctLength); System.out.println("cipher: " + new String(cipherText) + " bytes: " + ctLength); cipher.init(Cipher.ENCRYPT_MODE, key, zeroIv); IvParameterSpec decryptionIv = new IvParameterSpec(cipher.doFinal(msgNumber), 0, 8); cipher.init(Cipher.DECRYPT_MODE, key, decryptionIv); 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, ptLength)); }
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[] keyBytes = new byte[] { 0x73, 0x2f, 0x2d, 0x33, (byte) 0xc8, 0x01, 0x73, 0x2b, 0x72, 0x06, 0x75, 0x6c, (byte) 0xbd, 0x44, (byte) 0xf9, (byte) 0xc1, (byte) 0xc1, 0x03, (byte) 0xdd, (byte) 0xd9, 0x7c, 0x7c, (byte) 0xbe, (byte) 0x8e }; byte[] ivBytes = new byte[] { (byte) 0xb0, 0x7b, (byte) 0xf5, 0x22, (byte) 0xc8, (byte) 0xd6, 0x08, (byte) 0xb8 }; // encrypt the data using precalculated keys Cipher cEnc = Cipher.getInstance("DESede/CBC/PKCS7Padding", "BC"); cEnc.init(Cipher.ENCRYPT_MODE, new SecretKeySpec(keyBytes, "DESede"), new IvParameterSpec(ivBytes)); byte[] out = cEnc.doFinal(input); // decrypt the data using PBE char[] password = "password".toCharArray(); byte[] salt = new byte[] { 0x7d, 0x60, 0x43, 0x5f, 0x02, (byte) 0xe9, (byte) 0xe0, (byte) 0xae }; int iterationCount = 2048; PBEKeySpec pbeSpec = new PBEKeySpec(password, salt, iterationCount); SecretKeyFactory keyFact = SecretKeyFactory.getInstance("PBEWithSHAAnd3KeyTripleDES"); Cipher cDec = Cipher.getInstance("PBEWithSHAAnd3KeyTripleDES"); Key sKey = keyFact.generateSecret(pbeSpec); cDec.init(Cipher.DECRYPT_MODE, sKey); System.out.println("cipher : " + new String(out)); System.out.println("gen key: " + new String(sKey.getEncoded())); System.out.println("gen iv : " + new String(cDec.getIV())); System.out.println("plain : " + new String(cDec.doFinal(out))); }
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[] keyBytes = new byte[] { 0x73, 0x2f, 0x2d, 0x33, (byte) 0xc8, 0x01, 0x73, 0x2b, 0x72, 0x06, 0x75, 0x6c, (byte) 0xbd, 0x44, (byte) 0xf9, (byte) 0xc1, (byte) 0xc1, 0x03, (byte) 0xdd, (byte) 0xd9, 0x7c, 0x7c, (byte) 0xbe, (byte) 0x8e }; byte[] ivBytes = new byte[] { (byte) 0xb0, 0x7b, (byte) 0xf5, 0x22, (byte) 0xc8, (byte) 0xd6, 0x08, (byte) 0xb8 }; // encrypt the data using precalculated keys Cipher cEnc = Cipher.getInstance("DESede/CBC/PKCS7Padding", "BC"); cEnc.init(Cipher.ENCRYPT_MODE, new SecretKeySpec(keyBytes, "DESede"), new IvParameterSpec(ivBytes)); byte[] out = cEnc.doFinal(input); // decrypt the data using PBE char[] password = "password".toCharArray(); byte[] salt = new byte[] { 0x7d, 0x60, 0x43, 0x5f, 0x02, (byte) 0xe9, (byte) 0xe0, (byte) 0xae }; int iterationCount = 2048; PBEKeySpec pbeSpec = new PBEKeySpec(password); SecretKeyFactory keyFact = SecretKeyFactory.getInstance("PBEWithSHAAnd3KeyTripleDES", "BC"); Cipher cDec = Cipher.getInstance("PBEWithSHAAnd3KeyTripleDES", "BC"); Key sKey = keyFact.generateSecret(pbeSpec); cDec.init(Cipher.DECRYPT_MODE, sKey, new PBEParameterSpec(salt, iterationCount)); System.out.println("cipher : " + new String(out)); System.out.println("gen key: " + new String(sKey.getEncoded())); System.out.println("gen iv : " + new String(cDec.getIV())); System.out.println("plain : " + new String(cDec.doFinal(out))); }
From source file:MainClass.java
public static void main(String[] args) throws Exception { Provider p = Security.getProvider("Rot13Provider"); System.out.println("Provider name: " + p.getName()); System.out.println("Provider version: " + p.getVersion()); System.out.println("Provider information: " + p.getInfo()); Cipher cipher = Cipher.getInstance("ROT13", "Rot13Provider"); System.out.println("Cipher: " + cipher.getAlgorithm()); String testString = "This is a test!"; cipher.init(Cipher.ENCRYPT_MODE, (Key) null, new SecureRandom()); byte[] b1 = cipher.doFinal(testString.getBytes()); cipher.init(Cipher.DECRYPT_MODE, (Key) null, new SecureRandom()); byte[] b2 = cipher.doFinal(b1); System.out.println("Decrypted data as a String: " + new String(b2)); }