List of usage examples for javax.crypto Cipher ENCRYPT_MODE
int ENCRYPT_MODE
To view the source code for javax.crypto Cipher ENCRYPT_MODE.
Click Source Link
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 { 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 ww .ja va2s . com 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: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[] ivBytes = new byte[] { 0x00, 0x00, 0x00, 0x01, 0x04, 0x05, 0x06, 0x07, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01 };/*from w w w . j av a 2 s. c o m*/ Cipher cipher = Cipher.getInstance("AES/CTR/NoPadding", "BC"); KeyGenerator generator = KeyGenerator.getInstance("AES", "BC"); generator.init(192); Key encryptionKey = generator.generateKey(); System.out.println("key : " + Utils.toHex(encryptionKey.getEncoded())); System.out.println("input : " + new String(input)); // encryption pass 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); // decryption pass 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) + " bytes: " + ptLength); }
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 w w w .ja v a 2s .c o 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()); } }
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[] { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17 }; byte[] ivBytes = new byte[] { 0x00, 0x01, 0x02, 0x03, 0x00, 0x01, 0x02, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01 };/*from w ww . ja v a 2s . c om*/ SecretKeySpec key = new SecretKeySpec(keyBytes, "AES"); IvParameterSpec ivSpec = new IvParameterSpec(ivBytes); Cipher cipher = Cipher.getInstance("AES/CTR/NoPadding", "BC"); System.out.println("input : " + new String(input)); // encryption pass cipher.init(Cipher.ENCRYPT_MODE, key, ivSpec); ByteArrayInputStream bIn = new ByteArrayInputStream(input); CipherInputStream cIn = new CipherInputStream(bIn, cipher); ByteArrayOutputStream bOut = new ByteArrayOutputStream(); int ch; while ((ch = cIn.read()) >= 0) { bOut.write(ch); } byte[] cipherText = bOut.toByteArray(); System.out.println("cipher: " + new String(cipherText)); // decryption pass cipher.init(Cipher.DECRYPT_MODE, key, ivSpec); bOut = new ByteArrayOutputStream(); CipherOutputStream cOut = new CipherOutputStream(bOut, cipher); cOut.write(cipherText); cOut.close(); System.out.println("plain : " + new String(bOut.toByteArray())); }
From source file:MainClass.java
public static void main(String[] args) throws Exception { Security.addProvider(new BouncyCastleProvider()); SecureRandom random = new SecureRandom(); IvParameterSpec ivSpec = createCtrIvForAES(1, random); Key key = createKeyForAES(256, random); Cipher cipher = Cipher.getInstance("AES/CTR/NoPadding", "BC"); String input = "www.java2s.com"; Mac mac = Mac.getInstance("DES", "BC"); byte[] macKeyBytes = "12345678".getBytes(); Key macKey = new SecretKeySpec(macKeyBytes, "DES"); System.out.println("input : " + input); // encryption step cipher.init(Cipher.ENCRYPT_MODE, key, ivSpec); byte[] cipherText = new byte[cipher.getOutputSize(input.length() + mac.getMacLength())]; int ctLength = cipher.update(input.getBytes(), 0, input.length(), cipherText, 0); mac.init(macKey);//from w w w. j a v a2s .com mac.update(input.getBytes()); ctLength += cipher.doFinal(mac.doFinal(), 0, mac.getMacLength(), cipherText, ctLength); System.out.println("cipherText : " + new String(cipherText)); // decryption step cipher.init(Cipher.DECRYPT_MODE, key, ivSpec); byte[] plainText = cipher.doFinal(cipherText, 0, ctLength); int messageLength = plainText.length - mac.getMacLength(); mac.init(macKey); mac.update(plainText, 0, messageLength); byte[] messageHash = new byte[mac.getMacLength()]; System.arraycopy(plainText, messageLength, messageHash, 0, messageHash.length); System.out.println("plain : " + new String(plainText) + " verified: " + MessageDigest.isEqual(mac.doFinal(), messageHash)); }
From source file:TestCipher.java
public static void main(String args[]) throws Exception { Set set = new HashSet(); Random random = new Random(); for (int i = 0; i < 10; i++) { Point point = new Point(random.nextInt(1000), random.nextInt(2000)); set.add(point);//from w w w. ja v a 2 s .c o m } int last = random.nextInt(5000); // Create Key byte key[] = password.getBytes(); DESKeySpec desKeySpec = new DESKeySpec(key); SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES"); SecretKey secretKey = keyFactory.generateSecret(desKeySpec); // Create Cipher Cipher desCipher = Cipher.getInstance("DES/ECB/PKCS5Padding"); desCipher.init(Cipher.ENCRYPT_MODE, secretKey); // Create stream FileOutputStream fos = new FileOutputStream("out.des"); BufferedOutputStream bos = new BufferedOutputStream(fos); CipherOutputStream cos = new CipherOutputStream(bos, desCipher); ObjectOutputStream oos = new ObjectOutputStream(cos); // Write objects oos.writeObject(set); oos.writeInt(last); oos.flush(); oos.close(); // Change cipher mode desCipher.init(Cipher.DECRYPT_MODE, secretKey); // Create stream FileInputStream fis = new FileInputStream("out.des"); BufferedInputStream bis = new BufferedInputStream(fis); CipherInputStream cis = new CipherInputStream(bis, desCipher); ObjectInputStream ois = new ObjectInputStream(cis); // Read objects Set set2 = (Set) ois.readObject(); int last2 = ois.readInt(); ois.close(); // Compare original with what was read back int count = 0; if (set.equals(set2)) { System.out.println("Set1: " + set); System.out.println("Set2: " + set2); System.out.println("Sets are okay."); count++; } if (last == last2) { System.out.println("int1: " + last); System.out.println("int2: " + last2); System.out.println("ints are okay."); count++; } if (count != 2) { System.out.println("Problem during encryption/decryption"); } }
From source file:DataStudioCrak.java
public static void main(String[] args) { String company = "labthink"; byte[] companyByteArray = company.getBytes(); byte[] companyByteIntArray = intToByteArray(compute(companyByteArray, companyByteArray.length)); // byte[] ff = "zhulixia".getBytes(); byte[] snByte = new byte[32]; byte[] byte1 = new byte[] { 7, 1 }; byte[] byte2 = "zhaodapengpojiehahahahahaha".getBytes(); byte[] byte3 = new byte[] { 127 }; byte[] snMain = new byte[24]; System.arraycopy(byte1, 0, snMain, 0, 2); System.arraycopy(byte2, 0, snMain, 2, 17); System.arraycopy(companyByteIntArray, 0, snMain, 19, 4); System.arraycopy(byte3, 0, snMain, 23, 1); // 1 - single license,2 - site license ,3 educational license snMain[2] = (byte) 1; int intSn = compute(snMain, snMain.length); System.out.println("intSn=" + intSn); byte[] key1 = "dddd".getBytes(); byte[] key2 = intToByteArray(intSn); byte[] key = new byte[8]; System.arraycopy(key1, 0, key, 0, 4); System.arraycopy(key2, 0, key, 4, 4); byte encodedSnMain[] = new byte[snMain.length]; try {/* w w w . j a va 2 s. com*/ DESKeySpec deskeyspec = new DESKeySpec(key); javax.crypto.SecretKey secretkey = SecretKeyFactory.getInstance("DES").generateSecret(deskeyspec); Cipher cipher = Cipher.getInstance("DES/ECB/NoPadding"); cipher.init(Cipher.ENCRYPT_MODE, secretkey); cipher.update(snMain, 0, snMain.length, encodedSnMain); cipher.doFinal(); } catch (Exception ex) { ex.printStackTrace(); } System.arraycopy(key1, 0, snByte, 0, 4); System.arraycopy(key2, 0, snByte, 28, 4); System.arraycopy(encodedSnMain, 0, snByte, 4, 24); char[] snCharArray = Hex.encodeHex(snByte); String sn = new String(snCharArray); System.out.println("sn=" + sn); }
From source file:net.labthink.run.DataStudioCrak.java
public static void main(String[] args) { String company = "Labthink"; byte[] companyByteArray = company.getBytes(); byte[] companyByteIntArray = intToByteArray(compute(companyByteArray, companyByteArray.length)); // byte[] ff = "zhulixia".getBytes(); byte[] snByte = new byte[32]; byte[] byte1 = new byte[] { 7, 1 }; byte[] byte2 = "zhaodapengpojiehahahahahaha".getBytes(); byte[] byte3 = new byte[] { 127 }; byte[] snMain = new byte[24]; System.arraycopy(byte1, 0, snMain, 0, 2); System.arraycopy(byte2, 0, snMain, 2, 17); System.arraycopy(companyByteIntArray, 0, snMain, 19, 4); System.arraycopy(byte3, 0, snMain, 23, 1); // 1 - single license,2 - site license ,3 educational license snMain[2] = (byte) 1; int intSn = compute(snMain, snMain.length); System.out.println("intSn=" + intSn); byte[] key1 = "dddd".getBytes(); byte[] key2 = intToByteArray(intSn); byte[] key = new byte[8]; System.arraycopy(key1, 0, key, 0, 4); System.arraycopy(key2, 0, key, 4, 4); byte encodedSnMain[] = new byte[snMain.length]; try {// w w w . j a va 2 s . c o m DESKeySpec deskeyspec = new DESKeySpec(key); javax.crypto.SecretKey secretkey = SecretKeyFactory.getInstance("DES").generateSecret(deskeyspec); Cipher cipher = Cipher.getInstance("DES/ECB/NoPadding"); cipher.init(Cipher.ENCRYPT_MODE, secretkey); cipher.update(snMain, 0, snMain.length, encodedSnMain); cipher.doFinal(); } catch (Exception ex) { ex.printStackTrace(); } System.arraycopy(key1, 0, snByte, 0, 4); System.arraycopy(key2, 0, snByte, 28, 4); System.arraycopy(encodedSnMain, 0, snByte, 4, 24); char[] snCharArray = Hex.encodeHex(snByte); String sn = new String(snCharArray); System.out.println("sn=" + sn); }
From source file:AESTest.java
public static void main(String[] args) { try {/*from w w w. j av a 2 s . c o m*/ if (args[0].equals("-genkey")) { KeyGenerator keygen = KeyGenerator.getInstance("AES"); SecureRandom random = new SecureRandom(); keygen.init(random); SecretKey key = keygen.generateKey(); ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream(args[1])); out.writeObject(key); out.close(); } else { int mode; if (args[0].equals("-encrypt")) mode = Cipher.ENCRYPT_MODE; else mode = Cipher.DECRYPT_MODE; ObjectInputStream keyIn = new ObjectInputStream(new FileInputStream(args[3])); Key key = (Key) keyIn.readObject(); keyIn.close(); InputStream in = new FileInputStream(args[1]); OutputStream out = new FileOutputStream(args[2]); Cipher cipher = Cipher.getInstance("AES"); cipher.init(mode, key); crypt(in, out, cipher); in.close(); out.close(); } } catch (IOException e) { e.printStackTrace(); } catch (GeneralSecurityException e) { e.printStackTrace(); } catch (ClassNotFoundException e) { e.printStackTrace(); } }