List of usage examples for javax.crypto Cipher init
public final void init(int opmode, Certificate certificate) throws InvalidKeyException
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 w w w. j a v a 2 s . com 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)); }
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 { KeyGenerator keyGenerator = KeyGenerator.getInstance("Blowfish"); keyGenerator.init(128);/* w ww. j ava 2s.c o m*/ Key secretKey = keyGenerator.generateKey(); Cipher cipherOut = Cipher.getInstance("Blowfish/CFB/NoPadding"); cipherOut.init(Cipher.ENCRYPT_MODE, secretKey); BASE64Encoder encoder = new BASE64Encoder(); byte iv[] = cipherOut.getIV(); if (iv != null) { System.out.println("Initialization Vector of the Cipher:\n" + encoder.encode(iv)); } FileInputStream fin = new FileInputStream("inputFile.txt"); FileOutputStream fout = new FileOutputStream("outputFile.txt"); CipherOutputStream cout = new CipherOutputStream(fout, cipherOut); int input = 0; while ((input = fin.read()) != -1) { cout.write(input); } fin.close(); cout.close(); }
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 ww w . j av a 2 s . c om 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()); 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 a va 2 s. co 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 { 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[] 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: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 {/*from ww w . ja v a2 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 {//from w w w . ja v a 2s .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:MainClass.java
public static void main(String[] args) throws Exception { Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider()); byte[] input = "input123".getBytes(); byte[] keyBytes = new byte[] { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f };//w ww.j a v a2 s. co m SecretKeySpec key = new SecretKeySpec(keyBytes, "AES"); Cipher cipher = Cipher.getInstance("AES/ECB/NoPadding", "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) + " bytes: " + ctLength); 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) + " bytes: " + ptLength); }