Skip to content

Instantly share code, notes, and snippets.

@rickmune
Last active November 21, 2018 13:43
Show Gist options
  • Save rickmune/3888b1e76e2d43deea8db5bd3123c819 to your computer and use it in GitHub Desktop.
Save rickmune/3888b1e76e2d43deea8db5bd3123c819 to your computer and use it in GitHub Desktop.
A util that comes in handy when you want to encrypt any text and latter on decrypt it. Symmetric encryption.
package com.rickmune.sample.api.security.fuzzy;
import java.security.SecureRandom;
import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
public class SecThing {
private static final String ENCODING = "UTF-8";
private static final String CHARSET = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
private static final SecureRandom rnd = new SecureRandom();
public static String encrypt(String cleanKey, String message) throws Exception {
if (message == null || message.isEmpty()) {
throw new Exception("decrypt message cannot be empty");
} else if( cleanKey == null || cleanKey.isEmpty() ){
throw new Exception("decrypt Key cannot be empty");
}
String payload = null;
try {
byte[] randomBytes = initVectorGenerator(16).getBytes();
IvParameterSpec iv = new IvParameterSpec(randomBytes);
SecretKeySpec secretKeySpec = new SecretKeySpec(cleanKey.getBytes(), "AES");
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5PADDING");
cipher.init(Cipher.ENCRYPT_MODE, secretKeySpec, iv);
String messageB64 = Base64.encodeBytes(message.getBytes(ENCODING));
byte[] encrypted = cipher.doFinal(messageB64.getBytes(ENCODING));
payload = Base64.encodeBytes(encrypted) + new String(randomBytes, ENCODING);
} catch (Exception e) {
throw new EchoEncryptionException(e.getMessage());
}
return payload;
}
public static String decrypt(String key, String cleanMessage) throws Exception {
if (cleanMessage == null || cleanMessage.isEmpty()) {
throw new Exception("decrypt message cannot be empty");
} else if( key == null || key.isEmpty() ){
throw new Exception("decrypt Key cannot be empty");
}
String originalMessage = null;
try {
byte[] cleanKey = Base64.decode(key);
SecretKeySpec secretKeySpec = new SecretKeySpec(cleanKey, "AES");
byte[] encryptedPayload = Base64.decode(cleanMessage);
//String randomString = ;Find a way to get this from the other party
byte[] randomBytes = randomString.getBytes(ENCODING);
IvParameterSpec iv = new IvParameterSpec(randomBytes);
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5PADDING");
cipher.init(Cipher.DECRYPT_MODE, secretKeySpec, iv);
byte[] data = cipher.doFinal(encryptedPayload);
byte[] originalMessageB64 = Base64.decode(new String(data, ENCODING));
originalMessage = new String(originalMessageB64, ENCODING);
} catch (Exception e) {
throw new EchoEncryptionException(e.getMessage());
}
return originalMessage;
}
public static String initVectorGenerator(int length) {
if (length < 1) throw new IllegalArgumentException("Length must be greater than 0");
StringBuilder sb = new StringBuilder();
for (int i = 0; i < length; i++) {
sb.append(CHARSET.charAt(rnd.nextInt(CHARSET.length())));
}
return sb.toString();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment