Skip to content

Instantly share code, notes, and snippets.

@mkhoudary
Created April 12, 2022 08:37
Show Gist options
  • Save mkhoudary/feb81482de6ba62d8581b2fc0e550aff to your computer and use it in GitHub Desktop.
Save mkhoudary/feb81482de6ba62d8581b2fc0e550aff to your computer and use it in GitHub Desktop.
Joker Codes Gift Card Decryption Java Implementation
import java.security.InvalidAlgorithmParameterException;
import java.security.InvalidKeyException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Base64;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.SecretKey;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
/**
*
* @author Mohammed
*/
public class JokerCodesSecurityUtil {
public static String decryptGiftCardNumber(String encrypted, String secretKey, String secretIv) {
try {
encrypted = new String(Base64.getDecoder().decode(encrypted));
MessageDigest digest = MessageDigest.getInstance("SHA-256");
String key = bytesToHex(digest.digest(secretKey.getBytes())).substring(0, 32);
String iv = bytesToHex(digest.digest(secretIv.getBytes())).substring(0, 16);
SecretKeySpec secretKeySpec = new SecretKeySpec(key.getBytes(), "AES");
String returnVal = decrypt("AES/CBC/PKCS5Padding", encrypted, secretKeySpec, new IvParameterSpec(iv.getBytes()));
return returnVal;
} catch (Exception ex) {
Logger.getLogger(JokerCodesSecurityUtil.class.getName()).log(Level.SEVERE, null, ex);
throw new RuntimeException("Error while decrypting data");
}
}
private static String bytesToHex(byte[] hash) {
StringBuilder hexString = new StringBuilder(2 * hash.length);
for (int i = 0; i < hash.length; i++) {
String hex = Integer.toHexString(0xff & hash[i]);
if (hex.length() == 1) {
hexString.append('0');
}
hexString.append(hex);
}
return hexString.toString();
}
private static String decrypt(String algorithm, String cipherText, SecretKey key,
IvParameterSpec iv) throws NoSuchPaddingException, NoSuchAlgorithmException,
InvalidAlgorithmParameterException, InvalidKeyException,
BadPaddingException, IllegalBlockSizeException {
Cipher cipher = Cipher.getInstance(algorithm);
cipher.init(Cipher.DECRYPT_MODE, key, iv);
byte[] plainText = cipher.doFinal(Base64.getDecoder()
.decode(cipherText));
return new String(plainText);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment