Skip to content

Instantly share code, notes, and snippets.

View RichardHJensen's full-sized avatar

Richard Jensen RichardHJensen

View GitHub Profile
@RichardHJensen
RichardHJensen / AClassWithOnlyOnePrimitive.java
Created September 16, 2010 14:44
First Hand Coded Proxy
public class AClassWithOnlyOnePrimitive {
private int intValue;
public int getIntValue() {
return intValue;
}
public void setIntValue(int intValue) {
this.intValue = intValue;
}
@RichardHJensen
RichardHJensen / EntryKeyIsVowel.java
Created March 17, 2011 01:57
Similar loops over data in a map
public class EntryKeyIsVowel implements Predicate<Map.Entry<String,String>> {
public boolean apply(Map.Entry<String, String> input) {
return input.getKey().matches("^[aeiouAEIOU].*");
}
public boolean equals(Object obj) {
return this.getClass() == obj.getClass();
}
}
@RichardHJensen
RichardHJensen / simplest-implementation.java
Created September 22, 2011 00:05
First JCE test compiles
public class JCEExampleTest {
@Test
public void decryptingCiphertextShouldReturnOriginalPlaintext() {
assertEquals("Original Plaintext", decrypt(encrypt("Original Plaintext")));
}
private String decrypt(String cipherText) {
return null;
}
@RichardHJensen
RichardHJensen / simplest-to-green.java
Created September 22, 2011 00:14
First JCE test passes (with no encryption)
public class JCEExampleTest {
@Test
public void decryptingCiphertextShouldReturnOriginalPlaintext() {
assertEquals("Original Plaintext", decrypt(encrypt("Original Plaintext")));
}
private String decrypt(String cipherText) {
return cipherText;
}
@RichardHJensen
RichardHJensen / init-key-and-vector.java
Created September 22, 2011 01:36
Creating an array of bytes for the key
byte[] sessionKey = null;
byte[] iv = new byte[] { 0x7F, 0x6E, 0x5D, 0x4C, 0x3B, 0x2A, 0x19, 0x08 };
byte[] ciphertext = null;
Cipher cipher;
try {
KeyGenerator kGen = KeyGenerator.getInstance("AES");
kGen.init(128);
sessionKey = kGen.generateKey().getEncoded();
cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
//You can use ENCRYPT_MODE or DECRYPT_MODE
@RichardHJensen
RichardHJensen / first-encrypt.java
Created September 22, 2011 01:22
encrypt() using stack overflow sample
private String encrypt(String plainText) {
byte[] sessionKey = null; //Where you get this from is beyond the scope of this post
byte[] iv = null; //Ditto
byte[] ciphertext = null;
Cipher cipher;
try {
cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
//You can use ENCRYPT_MODE or DECRYPT_MODE
cipher.init(Cipher.ENCRYPT_MODE, new SecretKeySpec(sessionKey, "AES"), new IvParameterSpec(iv));
public class JCEExampleTest {
@Test
public void decryptingCiphertextShouldReturnOriginalPlaintext() {
}
}
@RichardHJensen
RichardHJensen / simplest-body.java
Created September 21, 2011 23:47
The first JCE assert
@Test
public void decryptingCiphertextShouldReturnOriginalPlaintext() {
assertEquals("Original Plaintext", decrypt(encrypt("Original Plaintext")));
}
@RichardHJensen
RichardHJensen / decrypt-first-attempt.java
Created September 22, 2011 01:51
Reverse encrypt for decrypt implementation
public class JCEExampleTest {
private byte[] sessionKey = null;
private final byte[] iv = new byte[] { 0x7F, 0x6E, 0x5D, 0x4C, 0x3B, 0x2A, 0x19, 0x08,
0x77, 0x66, 0x55, 0x44, 0x33, 0x22, 0x11, 0x00 };
@Test
public void decryptingCiphertextShouldReturnOriginalPlaintext() {
assertEquals("Original Plaintext", decrypt(encrypt("Original Plaintext")));
}
@RichardHJensen
RichardHJensen / encrypt-decrypt-utf8-base64.java
Created September 22, 2011 02:16
Add utf-8 and base64 encoding.
private String decrypt(String cipherText) {
byte[] plaintext = null;
Cipher cipher;
try {
cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
//You can use ENCRYPT_MODE or DECRYPT_MODE
cipher.init(Cipher.DECRYPT_MODE, new SecretKeySpec(sessionKey, "AES"), new IvParameterSpec(iv));
plaintext = cipher.doFinal(new sun.misc.BASE64Decoder().decodeBuffer(cipherText));
} catch (NoSuchAlgorithmException e) {