Skip to content

Instantly share code, notes, and snippets.

@barncastle
Last active August 1, 2023 14:37
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 barncastle/e9c188f0610e47f6acd0055b5e05b560 to your computer and use it in GitHub Desktop.
Save barncastle/e9c188f0610e47f6acd0055b5e05b560 to your computer and use it in GitHub Desktop.
Encryption logic from Joey For Reddit with all credentials
import javax.crypto.*;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.PBEKeySpec;
import javax.crypto.spec.SecretKeySpec;
import java.io.UnsupportedEncodingException;
import java.security.*;
import java.security.spec.InvalidKeySpecException;
import com.qiniu.util.Base64; // https://github.com/qiniu/java-sdk/blob/master/src/main/java/com/qiniu/util/Base64.java
class Scratch {
private static final Credentials AdStringer = new Credentials(
"902342a@",
"(elwl2344",
new byte[] {-88, -1, 1, 78, 18, -1, 34, 73, 58, -18, 123, -7, -58, 97, 12, 53}
);
private static final Credentials StringStudent = new Credentials(
"qq=23",
"qq#4k",
new byte[] {-88, -1, 17, -8, 86, 10, -31, 30, -5, -11, 67, -7, -8, 5, 12, -53}
);
private static final Credentials UserAccountStringer = new Credentials(
"67454562",
"3485920438",
new byte[] {-88, -21, 14, 8, 61, 6, -1, 90, 58, -24, 57, 17, -18, 15, 2, -8}
);
private static final Credentials BiscuitStringer = new Credentials(
"322328028sk$#^%",
"ueuwkj@490$#^@W",
new byte[] {8, -1, 4, 18, 12, -6, 1, 9, 80, -24, 15, -17, 48, 65, -20, 85}
);
private static final Credentials MoneyStringer = new Credentials(
"eslk09323",
"0439034kld",
new byte[] {-88, -13, 10, -81, 8, -10, 39, 34, -85, 113, 3, -66, -82, 57, 42, -3}
);
private static final Credentials StringMaster = new Credentials(
"8237423409",
"987324043",
new byte[] {-88, -1, 17, -8, 86, 10, -31, 30, -5, -111, 61, -75, -84, 95, 12, -53}
);
public static void main(String[] args) {
var instance = Encryption.getAESInstance(StringStudent.Key, StringStudent.Salt, StringStudent.IV);
assert instance != null;
var result = instance.TryDecrypt("3VINXP0TmY8BUBkN8zCTVvR7iCz31PW47y7McqwGf7tBBteCh2zGs15CAMFMO8b+PjQFIVEB9OSEpHIbIK4elI9Pxy8rBvh+BTUmZfUok6dM/kx9gMf1h5QUS1QzRkMNpgXjr/td9fKE4qvKL5u+Sw08+zFWhT3Ep5Pg572Lzm0PVg49AesSrqHb+05VnuUvlIm/B/h7g+yOClojbZPIZWWqytAp8qRyIRuhpkXi+D4z3blzY7z1zw44yIU/M2kfG7gw4FqksP+NyeG0vzD0EEShykBB8NEIJWxUVmLC/h1RsIdv7dUMaN8evU1dxKKyM3EOcdp4yFcVPnn/egUZoQ==");
}
private static class Credentials {
public final String Key;
public final String Salt;
public final byte[] IV;
public Credentials(String key, String salt, byte[] iv) {
Key = key;
Salt = salt;
IV = iv;
}
}
}
class Encryption {
private final AES instance;
public static class AES {
private byte[] IV;
private int keySize;
private int base64Flags;
private int iterationCount;
private String salt;
private String key;
private String cipherTransform;
private String keySpecAlgorithm;
private String encoding;
private String secretKeyAlgorithm;
private String messageDigestAlgorithm;
private String algorithm;
private SecureRandom secureRandom;
private IvParameterSpec IVParams;
public SecureRandom getSecureRandom() {
return this.secureRandom;
}
private String getAlgorithm() {
return this.algorithm;
}
public String getCipherTransform() {
return this.cipherTransform;
}
public int getBase64Flags() {
return this.base64Flags;
}
public String getEncoding() {
return this.encoding;
}
public static AES createAES(String str, String str2, byte[] bArr) {
return new AES()
.setIV(bArr)
.setKey(str)
.setSalt(str2)
.setKeySize(128)
.setKeySpecAlgorithm("AES")
.setEncoding("UTF8")
.setIterationCount(1)
.setMessageDigestAlgorithm("SHA1")
.setBase64Flags(0)
.setCipherTransform("AES/CBC/PKCS5Padding")
.setAlgorithm("SHA1PRNG")
.setSecretKeyAlgorithm("PBKDF2WithHmacSHA1");
}
public String getMessageDigestAlgorithm() {
return this.messageDigestAlgorithm;
}
public int getIterationCount() {
return this.iterationCount;
}
private byte[] getIV() {
return this.IV;
}
public IvParameterSpec getIVParams() {
return this.IVParams;
}
public String getKey() {
return this.key;
}
public String getKeySpecAlgorithm() {
return this.keySpecAlgorithm;
}
public int getKeySize() {
return this.keySize;
}
public String getSalt() {
return this.salt;
}
public String getSecretKeyAlgorithm() {
return this.secretKeyAlgorithm;
}
public AES setCipherTransform(String str) {
this.cipherTransform = str;
return this;
}
public AES setBase64Flags(int i) {
this.base64Flags = i;
return this;
}
public AES setEncoding(String str) {
this.encoding = str;
return this;
}
public AES setMessageDigestAlgorithm(String str) {
this.messageDigestAlgorithm = str;
return this;
}
public AES setIterationCount(int i) {
this.iterationCount = i;
return this;
}
public AES setIV(byte[] bArr) {
this.IV = bArr;
return this;
}
public AES setIVParams(IvParameterSpec ivParameterSpec) {
this.IVParams = ivParameterSpec;
return this;
}
public AES setKey(String str) {
this.key = str;
return this;
}
public AES setKeySpecAlgorithm(String str) {
this.keySpecAlgorithm = str;
return this;
}
public AES setKeySize(int i) {
this.keySize = i;
return this;
}
public AES setSalt(String str) {
this.salt = str;
return this;
}
public AES setSecretKeyAlgorithm(String str) {
this.secretKeyAlgorithm = str;
return this;
}
public AES setSecureRandom(SecureRandom secureRandom) {
this.secureRandom = secureRandom;
return this;
}
public AES setAlgorithm(String str) {
this.algorithm = str;
return this;
}
public Encryption init() throws NoSuchAlgorithmException {
setSecureRandom(SecureRandom.getInstance(getAlgorithm()));
setIVParams(new IvParameterSpec(getIV()));
return new Encryption(this);
}
}
public static Encryption getAESInstance(String _key, String _salt, byte[] _iv) {
try {
return AES.createAES(_key, _salt, _iv).init();
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
return null;
}
}
private SecretKey getSecretKey(char[] password) throws NoSuchAlgorithmException, UnsupportedEncodingException, InvalidKeySpecException {
var keyFactory = SecretKeyFactory.getInstance(this.instance.getSecretKeyAlgorithm());
var keySpec = new PBEKeySpec(password, this.instance.getSalt().getBytes(this.instance.getEncoding()), this.instance.getIterationCount(), this.instance.getKeySize());
return new SecretKeySpec(keyFactory.generateSecret(keySpec).getEncoded(), this.instance.getKeySpecAlgorithm());
}
private char[] generatePassword(String str) throws UnsupportedEncodingException, NoSuchAlgorithmException {
MessageDigest messageDigest = MessageDigest.getInstance(this.instance.getMessageDigestAlgorithm());
messageDigest.update(str.getBytes(this.instance.getEncoding()));
return Base64.encodeToString(messageDigest.digest(), 1).toCharArray();
}
public String Decrypt(String str) throws UnsupportedEncodingException, NoSuchAlgorithmException, InvalidKeySpecException, NoSuchPaddingException, InvalidAlgorithmParameterException, InvalidKeyException, BadPaddingException, IllegalBlockSizeException {
if (str == null) {
return null;
}
byte[] buffer = Base64.decode(str, this.instance.getBase64Flags());
char[] password = generatePassword(this.instance.getKey());
SecretKey secretKey = getSecretKey(password);
Cipher cipher = Cipher.getInstance(this.instance.getCipherTransform());
cipher.init(2, secretKey, this.instance.getIVParams(), this.instance.getSecureRandom());
return new String(cipher.doFinal(buffer));
}
public String TryDecrypt(String str) {
try {
return Decrypt(str);
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
public String Encrypt(String str) throws UnsupportedEncodingException, NoSuchAlgorithmException, NoSuchPaddingException, InvalidAlgorithmParameterException, InvalidKeyException, InvalidKeySpecException, BadPaddingException, IllegalBlockSizeException {
if (str == null) {
return null;
}
SecretKey secretKey = getSecretKey(generatePassword(this.instance.getKey()));
byte[] bytes = str.getBytes(this.instance.getEncoding());
Cipher cipher = Cipher.getInstance(this.instance.getCipherTransform());
cipher.init(1, secretKey, this.instance.getIVParams(), this.instance.getSecureRandom());
return Base64.encodeToString(cipher.doFinal(bytes), this.instance.getBase64Flags());
}
public String TryEncrypt(String str) {
try {
return Encrypt(str);
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
private Encryption(AES aes) {
this.instance = aes;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment