Skip to content

Instantly share code, notes, and snippets.

@jpralves
Created April 28, 2018 21:09
Show Gist options
  • Save jpralves/93d82ca594351d2cfd257d361a4c3cbe to your computer and use it in GitHub Desktop.
Save jpralves/93d82ca594351d2cfd257d361a4c3cbe to your computer and use it in GitHub Desktop.
package testencrypt;
import java.security.Key;
import java.security.SecureRandom;
import java.util.Arrays;
import javax.crypto.*;
import javax.crypto.spec.*;
import java.util.Base64;
/**
*
* @author jpralves
*/
public class TestEncrypt {
// public static String src; // = "pbe test";
public static String bytesToHex(byte[] bytes) {
StringBuilder result = new StringBuilder();
for (byte byt : bytes) {
result.append(Integer.toString((byt & 0xff) + 0x100, 16).substring(1));
}
return result.toString();
}
public static String jdkPBE_cipher(String ciphername, String password, String src) {
byte[] salt;
byte[] cipheredResult;
byte[] result = null;
try {
PBEKeySpec pbeKeySpec = new PBEKeySpec(password.toCharArray());
SecretKeyFactory factory = SecretKeyFactory.getInstance(ciphername);
Key key = factory.generateSecret(pbeKeySpec);
int saltBlockLen = Cipher.getInstance(factory.getAlgorithm()).getBlockSize();
SecureRandom random = new SecureRandom();
salt = random.generateSeed(saltBlockLen);
PBEParameterSpec pbeParameterSpac = new PBEParameterSpec(salt, 1000);
Cipher cipher = Cipher.getInstance(ciphername);
cipher.init(Cipher.ENCRYPT_MODE, key, pbeParameterSpac);
cipheredResult = cipher.doFinal(src.getBytes());
result = new byte[salt.length + cipheredResult.length];
// put salt at the beggining
System.arraycopy(salt, 0, result, 0, salt.length);
// append the ciphered result
System.arraycopy(cipheredResult, 0, result, salt.length, cipheredResult.length);
} catch (Exception e) {
e.printStackTrace();
}
return new String(Base64.getEncoder().encode(result));
}
public static String jdkPBE_decipher(String ciphername, String password, String src) {
byte[] srcbytes;
byte[] result = null;
try {
srcbytes = Base64.getDecoder().decode(src);
PBEKeySpec pbeKeySpec = new PBEKeySpec(password.toCharArray());
SecretKeyFactory factory = SecretKeyFactory.getInstance(ciphername);
Key key = factory.generateSecret(pbeKeySpec);
// Salt is at the beggining
int saltBlockLen = Cipher.getInstance(factory.getAlgorithm()).getBlockSize();
byte[] salt = Arrays.copyOfRange(srcbytes, 0, saltBlockLen);
byte[] bytestodecode = Arrays.copyOfRange(srcbytes, saltBlockLen, srcbytes.length);
PBEParameterSpec pbeParameterSpac = new PBEParameterSpec(salt, 1000);
Cipher cipher = Cipher.getInstance(ciphername);
cipher.init(Cipher.DECRYPT_MODE, key, pbeParameterSpac);
byte[] iv = cipher.getIV();
System.out.println(iv);
result = cipher.doFinal(bytestodecode);
} catch (Exception e) {
e.printStackTrace();
}
return new String(result);
}
/**
* @param args the command line arguments
*
* e|d PBEWITHMD5andDES password valor
*/
public static void main(String[] args) {
// TODO code application logic here
if (args.length > 3 && args[0].equals("e")) {
//...
String ciphername = args[1];
String password = args[2];
String codetocipher = args[3];
System.out.println("encrypted msg:[" + jdkPBE_cipher(ciphername, password, codetocipher) + "]");
} else if (args.length > 3 && args[0].equals("d")) {
String ciphername = args[1];
String password = args[2];
String codetodecipher = args[3];
System.out.println("decrypted msg:[" + jdkPBE_decipher(ciphername, password, codetodecipher) + "]");
} else {
System.out.println("Args: operation ciphername password msg");
System.out.println("Ex. to encrypt: e PBEWITHMD5andDES strangepassword \"uma mensagem\"");
System.out.println("Ex. to decrypt: d PBEWITHMD5andDES strangepassword ZrpxVeCnVvbHGdWgGTtZdv6Jg+IMfbla");
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment