· 7 years ago · Oct 08, 2018, 12:58 AM
1import java.security.NoSuchAlgorithmException;
2import java.security.Security;
3import java.security.spec.InvalidKeySpecException;
4import java.security.spec.KeySpec;
5
6import javax.crypto.Cipher;
7import javax.crypto.SecretKey;
8import javax.crypto.SecretKeyFactory;
9import javax.crypto.spec.IvParameterSpec;
10import javax.crypto.spec.PBEKeySpec;
11import javax.crypto.spec.SecretKeySpec;
12import org.apache.commons.codec.binary.Base64;
13import cryptix.jce.provider.CryptixCrypto;
14
15public class Cryptographer {
16
17 private final static String passPhrase = "jhJHPiup9[(NMK5760&(%$(jbnpui^^*J;jbl%^(>LM())*&%(HHJglj;o;yut9)&^(&0[9807875954rglb7855485900-86gHY*&_*(&{IN&*^NPUY)&{ojklk]98-7HGHJguiJHUIHP(*&JH)8776-897(*PN)76-89[9ijNP**(&_(8=[9i[i0}{{P{IOU)8760786KJHIUY)P*&78-[89uuhj0p76897[98hPU&**&+)(U{JN)&*%^)&(*+()Jpihp897-98j{KJ{UIH)^_()}{}P{)(&&*TG976g7y89sokdfj9-[8wer9qkfml/DVNP892-349P*&EMFKLWJ[897-59G/M/L;KHH762JJO00Djjasdhf9076";
18
19 private final static String initVector = "{fH8-:huG*^(H&G0"; // must be 16 bytes
20
21 private final static String CRYPT_STRING = "Rijndael/CBC/PKCS#7";
22
23 private static final byte[] SALT = { (byte) 0xc7, (byte) 0x73, (byte) 0x21,
24 (byte) 0x8c, (byte) 0x7e, (byte) 0xc8, (byte) 0xee, (byte) 0x99 };
25
26 public String encrypt(String plainText, String objectID,
27 String contextString) {
28 try {
29 if (plainText.equals("") || contextString.equals("")
30 || objectID.equals("")) {
31 return "ERROR";
32 } else {
33 Security.addProvider(new CryptixCrypto());
34 objectID = objectID.toUpperCase();
35 String passPhraseStr = contextString + passPhrase;
36 Cipher cipher = Cipher.getInstance(CRYPT_STRING);
37 System.out.println("Using provider: "
38 + cipher.getProvider().getName() + " - "
39 + cipher.getProvider().getInfo());
40 IvParameterSpec spec = new IvParameterSpec(
41 initVector.getBytes("UTF8"));
42 SecretKey rijndaelKey = getSecretKey(passPhraseStr);
43 cipher.init(Cipher.ENCRYPT_MODE, rijndaelKey, spec);
44 byte[] encryptedByte = cipher.doFinal((objectID + plainText)
45 .getBytes("UTF8"));
46 return Base64.encodeBase64String(encryptedByte);
47 }
48 } catch (Exception ex) {
49 System.out.println("Exception :" + ex.getMessage() + " class:"
50 + ex.getClass().getName());
51 ex.printStackTrace();
52 return "ERROR";
53 }
54 }
55
56 /**
57 * decrypt a cipherText to plainText.
58 *
59 * @param cipherText
60 * @param objectID
61 * @param contextString
62 * @return decrypted plainText
63 */
64 public String decrypt(String cipherText, String objectID,
65 String contextString) {
66 try {
67 if (cipherText.equals("") || contextString.equals("")
68 || objectID.equals("")) {
69 return "ERROR";
70 } else {
71 Security.addProvider(new CryptixCrypto());
72 objectID = objectID.toUpperCase();
73 String passPhraseStr = contextString + passPhrase;
74 byte[] cipherBytes = Base64.decodeBase64(cipherText);
75 Cipher cipher = Cipher.getInstance(CRYPT_STRING);
76 System.out.println("Using provider: "
77 + cipher.getProvider().getName() + " - "
78 + cipher.getProvider().getInfo());
79 IvParameterSpec spec = new IvParameterSpec(
80 initVector.getBytes("UTF8"));
81 SecretKey rijndaelKey = getSecretKey(passPhraseStr);
82 cipher.init(Cipher.DECRYPT_MODE, rijndaelKey, spec);
83 byte[] decryptedBytes = cipher.doFinal(cipherBytes);
84 String plainText = new String(decryptedBytes, "UTF8");
85 if (plainText.startsWith(objectID)) {
86 return plainText;
87 } else {
88 System.out.println(plainText);
89 return "ERROR: BAD PIN";
90 }
91 }
92 } catch (Exception ex) {
93 ex.printStackTrace();
94 System.out.println("Exception :" + ex.getMessage() + " class:"
95 + ex.getClass().getName());
96 return "ERROR";
97 }
98 }
99
100 private SecretKey getSecretKey(String password) {
101 SecretKey secret = null;
102 try {
103 SecretKeyFactory factory = SecretKeyFactory
104 .getInstance("PBKDF2WithHmacSHA1");
105 KeySpec spec = new PBEKeySpec(password.toCharArray(), SALT, 2, 256);
106 SecretKey tmp = factory.generateSecret(spec);
107 secret = new SecretKeySpec(tmp.getEncoded(), "Rijndael");
108 } catch (NoSuchAlgorithmException e) {
109 System.out.println("Exception :" + e.getMessage());
110 e.printStackTrace();
111 } catch (InvalidKeySpecException e) {
112 System.out.println("Exception :" + e.getMessage());
113 System.out.println(e.getStackTrace());
114 }
115 return secret;
116 }
117
118 public static void main(String args[]) {
119 Cryptographer cryptor = new Cryptographer();
120 String cipherText = cryptor.encrypt("ABCDEFGHI", "001", "12345");
121 System.out.println("CipherText: " + cipherText);
122 String plainText = cryptor.decrypt(cipherText, "001", "12345");
123 System.out.println("plainText: " + plainText);
124 }
125}