Skip to content

Instantly share code, notes, and snippets.

@danwatt
Created January 31, 2012 17:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save danwatt/1711766 to your computer and use it in GitHub Desktop.
Save danwatt/1711766 to your computer and use it in GitHub Desktop.
Decrypt Coldfusion UUEncode
import java.io.ByteArrayOutputStream;
import java.io.OutputStream;
import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;
import javax.mail.internet.MimeUtility;
import org.apache.commons.io.IOUtils;
public class ColdFusionUuEncryption {
private static final String UU_DUMMY = "dummy";
private static final String UU_FOOTER = "\n \nend\r\n";
private static final String UU_HEADER = "begin 644 "+UU_DUMMY+"\r\n";
private String encryptionKey;
public void setEncryptionKey(String encryptionKey) {
this.encryptionKey = encryptionKey;
}
private byte[] decodeKey() throws Exception {
return decode(encryptionKey, "base64");
}
public static byte[] doCrypt(byte[] kBytes, byte[] actual, int mode) throws Exception {
SecretKeySpec skeySpec = new SecretKeySpec(kBytes, "AES");
Cipher c = Cipher.getInstance("AES");
c.init(mode, skeySpec);
return c.doFinal(actual);
}
public String encryptCF(String s) throws Exception {
return uuencodeCfStyle(doCrypt(decodeKey(), s.getBytes(), Cipher.ENCRYPT_MODE));
}
public String decryptCF(String s) throws Exception {
return new String(new String(doCrypt(decodeKey(), decode(UU_HEADER + s + UU_FOOTER, "uuencode"), Cipher.DECRYPT_MODE)));
}
private static byte[] decode(String str, String encoding) throws Exception {
return IOUtils.toByteArray(MimeUtility.decode(IOUtils.toInputStream(str), encoding));
}
public static String uuencodeCfStyle(byte[] bytes) throws Exception {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
OutputStream b64 = MimeUtility.encode(baos, "uuencode", UU_DUMMY);
b64.write(bytes);
b64.close();
String s = new String(baos.toByteArray());
return s.substring(UU_HEADER.length(), s.length() - UU_FOOTER.length());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment