Skip to content

Instantly share code, notes, and snippets.

@zhelezoglo
Created August 4, 2015 18:10
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 zhelezoglo/3ed96e9352c9fec5198e to your computer and use it in GitHub Desktop.
Save zhelezoglo/3ed96e9352c9fec5198e to your computer and use it in GitHub Desktop.
import java.security.MessageDigest;
import java.util.Arrays;
import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import javax.swing.JOptionPane;
import javax.xml.bind.DatatypeConverter;
import javax.crypto.IllegalBlockSizeException;
public class VpnGroupPasswordDecoder {
public static String decrypt(String cipherText) throws Exception {
byte[] cipherTextBytes;
try {
cipherTextBytes = DatatypeConverter.parseHexBinary(cipherText);
}
catch (IllegalArgumentException e) {
JOptionPane.showMessageDialog(null, "Invalid Encrypted Group Password",
"", JOptionPane.ERROR_MESSAGE);
e.printStackTrace();
return "";
}
if (cipherTextBytes.length < 48) {
JOptionPane.showMessageDialog(null, "Invalid Encrypted Group Password",
"", JOptionPane.ERROR_MESSAGE);
return "";
}
byte[] iv = new byte[8];
System.arraycopy(cipherTextBytes, 0, iv, 0, 8);
byte[] ht = new byte[20];
System.arraycopy(cipherTextBytes, 0, ht, 0, 20);
ht[19]++;
byte[] h4 = new byte[20];
System.arraycopy(cipherTextBytes, 20, h4, 0, 20);
MessageDigest sha1 = MessageDigest.getInstance("SHA1");
byte[] h2 = sha1.digest(ht);
ht[19] += 2;
byte[] h3 = sha1.digest(ht);
byte[] key = new byte[24];
System.arraycopy(h2, 0, key, 0, 20);
System.arraycopy(h3, 0, key, 20, 4);
byte[] ciphertextBytesWithoutHeader = new byte[cipherTextBytes.length - 40];
System.arraycopy(cipherTextBytes, 40, ciphertextBytesWithoutHeader, 0, ciphertextBytesWithoutHeader.length);
ht = sha1.digest(ciphertextBytesWithoutHeader);
if (!Arrays.equals(h4, ht)) {
JOptionPane.showMessageDialog(null, "Invalid Encrypted Group Password",
"", JOptionPane.ERROR_MESSAGE);
return "";
}
SecretKey secretKey = new SecretKeySpec(key, "DESede");
IvParameterSpec ivSpec = new IvParameterSpec(iv);
Cipher decipher = Cipher.getInstance("DESede/CBC/PKCS5Padding");
decipher.init(Cipher.DECRYPT_MODE, secretKey, ivSpec);
byte[] plainTextBytes;
try {
plainTextBytes = decipher.doFinal(ciphertextBytesWithoutHeader);
} catch (IllegalBlockSizeException e) {
JOptionPane.showMessageDialog(null, "Invalid Encrypted Group Password",
"", JOptionPane.ERROR_MESSAGE);
return "";
}
return new String(plainTextBytes, "UTF-8");
}
public static void main(String[] args) throws Exception {
String groupPswd = "";
System.out.println(decrypt(groupPswd));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment