Skip to content

Instantly share code, notes, and snippets.

@samuelmale
Created February 12, 2019 22:45
Show Gist options
  • Save samuelmale/0f2885392f368673758df0ca51626df4 to your computer and use it in GitHub Desktop.
Save samuelmale/0f2885392f368673758df0ca51626df4 to your computer and use it in GitHub Desktop.
/**
* This is a utility class that gives convenience methods for cryptography
* I actually wrote this class to help out a friend @clyne
*
* @author Samuel Male
*/
public class EncrptionUtils {
/**
* It does the Encryption. It actually delegates {@link #encrypt(String, String, Operation)} internally.
* @param key
* @param value
* @return
*/
public static String doEncrypt(String key, String value) {
byte [] cipher = encrypt(key, value, Operation.ENCRYPT);
return new String(cipher);
}
/**
* It does the Decryption. It actually delegates {@link #encrypt(String, String, Operation)} internally.
* @param key to use to do decryption
* @param value encrypted value to decrypt
* @return the actual value
*/
public static String doDecrypt(String key, String value) {
// Convert the bytes to the right cipher(integers separated by commas)
String cipher = new String(value.getBytes());
byte [] decryptedValue = encrypt(key, cipher, Operation.DECRYPT);
return new String(decryptedValue);
}
/**
* It does both <code>Encryption</code> and <code>Decryption</code>
* Well with this implementation, we don't need the <a href="https://en.wikipedia.org/wiki/One-time_pad">One Time Pad</a> method.
* For details why see : https://stackoverflow.com/questions/42923469/one-time-pad-xor-cipher
* Encryption is done by <code>XOR<code>ing the key with the value and then encode the result to bits
* @param key the key used for both operations
* @param value value of either the cipher(encrypted value to decrypt) or value to encrypted
* @param operation the {@link Operation} either Decryption or Encryption
* @return byte array of either the cipher or decryption
*
*/
public static byte[] encrypt(String key, String value, Operation operation) {
char [] keyChars = key.toCharArray();
char [] valueChars = value.toCharArray();
StringBuilder cipherBuilder = new StringBuilder();
// If we are decrypting, lets generate write array of characters of the cipher
if (operation.equals(Operation.DECRYPT)) {
// Give it a new reference
cipherBuilder = new StringBuilder();
// convert the decimals to the corresponding characters
String[] rawCharacters = value.split(",");
for (int i = 0; i < rawCharacters.length; i++) {
int rawChar = Integer.valueOf(rawCharacters[i]);
char cipherChar = (char) rawChar;
cipherBuilder.append(cipherChar);
}
valueChars = cipherBuilder.toString().toCharArray();
// Drop the cache
cipherBuilder = new StringBuilder();
}
for (int i = 0; i < valueChars.length; i++) {
// Because some unicodes aren't visually printed on the screen eg. '\0'
// We gonna create a cipher of ints separated by commas to have a full visible cipher
int intForCharacter = (valueChars[i] ^ keyChars[i]);
// if we are encrypting, we need to generate the cipher in away that it contains the
// raw integers for the containing characters which will turned to bits
if (operation.equals(Operation.ENCRYPT)) {
// Check whether it's not the last index
if (i != valueChars.length - 1) {
cipherBuilder.append(intForCharacter + ",");
} else {
// Don't append the ','
cipherBuilder.append(intForCharacter);
}
} else {
cipherBuilder.append((char)intForCharacter);
}
}
if (operation.equals(Operation.ENCRYPT)) {
return cipherBuilder.toString().getBytes();
}
return cipherBuilder.toString().getBytes();
}
public enum Operation {
ENCRYPT, DECRYPT
}
// Test out the program
public static void main(String[] args) {
// First of all let's encrypt
String key = "oko2$_", password = "$clyne";
String cipher = doEncrypt(key, password);
// Log the encrypted string
System.out.println("Cipher : " + cipher);
// Decrypt
String clynezPassword = doDecrypt(key, cipher);
// Log the password
System.out.println("Password : " + clynezPassword);
}
//TODO : I'm pretty tired now but the key should randomly generated
//TODO : Improve #doEncrypt() slightly to return a string of bits instead of the ugly figures
//LUCK
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment