Skip to content

Instantly share code, notes, and snippets.

@jsfeng
Created May 7, 2012 18:31
Show Gist options
  • Save jsfeng/2629502 to your computer and use it in GitHub Desktop.
Save jsfeng/2629502 to your computer and use it in GitHub Desktop.
Cryptographer encryption /decryption
import java.security.NoSuchAlgorithmException;
import java.security.Security;
import java.security.spec.InvalidKeySpecException;
import java.security.spec.KeySpec;
import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.PBEKeySpec;
import javax.crypto.spec.SecretKeySpec;
import org.apache.commons.codec.binary.Base64;
import cryptix.jce.provider.CryptixCrypto;
public class Cryptographer {
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";
private final static String initVector = "{fH8-:huG*^(H&G0"; // must be 16 bytes
private final static String CRYPT_STRING = "Rijndael/CBC/PKCS#7";
private static final byte[] SALT = { (byte) 0xc7, (byte) 0x73, (byte) 0x21,
(byte) 0x8c, (byte) 0x7e, (byte) 0xc8, (byte) 0xee, (byte) 0x99 };
public String encrypt(String plainText, String objectID,
String contextString) {
try {
if (plainText.equals("") || contextString.equals("")
|| objectID.equals("")) {
return "ERROR";
} else {
Security.addProvider(new CryptixCrypto());
objectID = objectID.toUpperCase();
String passPhraseStr = contextString + passPhrase;
Cipher cipher = Cipher.getInstance(CRYPT_STRING);
System.out.println("Using provider: "
+ cipher.getProvider().getName() + " - "
+ cipher.getProvider().getInfo());
IvParameterSpec spec = new IvParameterSpec(
initVector.getBytes("UTF8"));
SecretKey rijndaelKey = getSecretKey(passPhraseStr);
cipher.init(Cipher.ENCRYPT_MODE, rijndaelKey, spec);
byte[] encryptedByte = cipher.doFinal((objectID + plainText)
.getBytes("UTF8"));
return Base64.encodeBase64String(encryptedByte);
}
} catch (Exception ex) {
System.out.println("Exception :" + ex.getMessage() + " class:"
+ ex.getClass().getName());
ex.printStackTrace();
return "ERROR";
}
}
/**
* decrypt a cipherText to plainText.
*
* @param cipherText
* @param objectID
* @param contextString
* @return decrypted plainText
*/
public String decrypt(String cipherText, String objectID,
String contextString) {
try {
if (cipherText.equals("") || contextString.equals("")
|| objectID.equals("")) {
return "ERROR";
} else {
Security.addProvider(new CryptixCrypto());
objectID = objectID.toUpperCase();
String passPhraseStr = contextString + passPhrase;
byte[] cipherBytes = Base64.decodeBase64(cipherText);
Cipher cipher = Cipher.getInstance(CRYPT_STRING);
System.out.println("Using provider: "
+ cipher.getProvider().getName() + " - "
+ cipher.getProvider().getInfo());
IvParameterSpec spec = new IvParameterSpec(
initVector.getBytes("UTF8"));
SecretKey rijndaelKey = getSecretKey(passPhraseStr);
cipher.init(Cipher.DECRYPT_MODE, rijndaelKey, spec);
byte[] decryptedBytes = cipher.doFinal(cipherBytes);
String plainText = new String(decryptedBytes, "UTF8");
if (plainText.startsWith(objectID)) {
return plainText;
} else {
System.out.println(plainText);
return "ERROR: BAD PIN";
}
}
} catch (Exception ex) {
ex.printStackTrace();
System.out.println("Exception :" + ex.getMessage() + " class:"
+ ex.getClass().getName());
return "ERROR";
}
}
private SecretKey getSecretKey(String password) {
SecretKey secret = null;
try {
SecretKeyFactory factory = SecretKeyFactory
.getInstance("PBKDF2WithHmacSHA1");
KeySpec spec = new PBEKeySpec(password.toCharArray(), SALT, 2, 256);
SecretKey tmp = factory.generateSecret(spec);
secret = new SecretKeySpec(tmp.getEncoded(), "Rijndael");
} catch (NoSuchAlgorithmException e) {
System.out.println("Exception :" + e.getMessage());
e.printStackTrace();
} catch (InvalidKeySpecException e) {
System.out.println("Exception :" + e.getMessage());
System.out.println(e.getStackTrace());
}
return secret;
}
public static void main(String args[]) {
Cryptographer cryptor = new Cryptographer();
String cipherText = cryptor.encrypt("ABCDEFGHI", "001", "12345");
System.out.println("CipherText: " + cipherText);
String plainText = cryptor.decrypt(cipherText, "001", "12345");
System.out.println("plainText: " + plainText);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment