Last active
October 20, 2015 03:02
-
-
Save TikiTDO/a083296431a19f83f69f to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.security.Key; | |
import java.security.PrivateKey; | |
import java.security.PublicKey; | |
import java.security.SecureRandom; | |
import java.security.KeyPair; | |
import java.security.Security; | |
import java.security.Signature; | |
import javax.crypto.Cipher; | |
import javax.crypto.KeyGenerator; | |
import javax.crypto.SecretKey; | |
import javax.crypto.spec.SecretKeySpec; | |
import javax.crypto.spec.IvParameterSpec; | |
import org.apache.commons.codec.binary.Base64; | |
import org.bouncycastle.jce.provider.BouncyCastleProvider; | |
import org.bouncycastle.openssl.PEMKeyPair; | |
import org.bouncycastle.openssl.PEMParser; | |
import org.bouncycastle.openssl.jcajce.JcaPEMKeyConverter; | |
import org.bouncycastle.openssl.PEMException; | |
import java.io.BufferedReader; | |
import java.io.FileReader; | |
import java.io.FileNotFoundException; | |
import java.io.IOException; | |
/* | |
Content of tsys.pem - Private key used to decrypt the symmetric key. | |
-----BEGIN RSA PRIVATE KEY----- | |
MIICXQIBAAKBgQDANuTwwBtMxFIIauvU/X9Gqy0qZIXL+uSMI4IhVshrwzcKz9KM | |
mw4F0Tu7D0ETgYugJRKageTTivZ9rDey7MwTQ5O5M17SyGgTruEsPMX0VOJbX1Ll | |
BgX0St3jwJ4JdwJbZubFCGzEdhsf2RaPXCrednbuPlpQzlIWbI3Ev7qo8wIDAQAB | |
AoGBALGbx65CBLfJi/dlPOcBGfMlxsUuP4d9HGP1h4/gza7tolWmNOphd/KeJLAz | |
ZzFr+aWPvAHy1os2E4+Ka6k0L1heKWIjTeTrkmXNULCmG/Di/GianBqYcPnxlfu2 | |
kwgXg2yIknFJjlAio69C7EUDsgBRmLJVWA729Kwi5jbMSR4hAkEA8VCHvG5C8Nox | |
uWoLy4liFjcZL+cLHEz3Sa1ySIzWbsV5G/vg9WDiTccx9NXPXEjs/OPIcho+GE79 | |
3uwtOw7TAwJBAMvpbUeb/clt12iOCQgeh/5dSimM9rh2V2OHIBFRCOqSYzmNKClI | |
yx5Hr+PBjjrxn2n0U/LNfvd86bTunNAm91ECQQCkqBxWOK44hcmUCIYP1ag2HFZ1 | |
VyM/pky92zm1w1O1tczTiWSQShFgnOC7fQDGqYP7crmdEcb48z1K1MBwIcddAkBz | |
Nke1bzDcr8BG5gG6Ya3LDaDKi0gXf13u8TkwL44PI3kG2ne3o9aIHE8IK6OWMFE3 | |
B4KrLGqxoRVXUn6tqWaRAkATDt6QTq2pFSqSf9ndOlxXeiNkDXmGOV/j7EFjb9Kd | |
D0woG93uLlX6BOwzAAib8fnojJER15fj/KMNyJcGqvEh | |
-----END RSA PRIVATE KEY----- | |
*/ | |
/* | |
Content of rogers.pem - Generates public key used to verify the signature. | |
-----BEGIN RSA PRIVATE KEY----- | |
MIICXQIBAAKBgQDGKLN1YdxSmR1z1u2iE+8+iaTUu/gjk6wwP86Cd8LQOSkNXxxS | |
zTefY/RQonD6Xyvfxm1WBjbcO0Sx1DdL2bZicVhfuQcx/GO6uYNj8ELVu0f1vsov | |
6j0oPsZtvf1VJpJvnjndkXUOS9BHggA+A3ahfKA9tFWNIEPjyto/0MTu+QIDAQAB | |
AoGAGAv+YyNCCl/8W/9cDQwgP1WSxCIfZanY/m3v0JNZ98zH95BwLngq5NPLZTGh | |
RjDgcSJySDsu0yD6IB5hq3m7ERZoQlpwcmCfOjaLPuJk7pDbOdA6r8qIDbne44qc | |
sZ3CglxgP1zeNnY4mYVfw20N9HnIuCcJCzQY9dpTguqQiAECQQDw5+wM60PJrY7N | |
pSKUTCY13L1NvaHiOS8za9gXmdhWfiX7kxAv7XktY0wYZtKYoQPwg3uHm2T5BSVS | |
gqtS3d8BAkEA0pMgkdlmxrrYI/WDpfCq5ItbGDki1aUBtKYWQvLqNYDLEkvHpcjB | |
PHTHt7aOo1ij5Z+874/Bj+L5dqgTw94H+QJAS73PRHyPkpAjzto1lgSfGt4tEo0l | |
oZezFGq6xSjUkNUWohMpyrfSGogWu7FQUzaEo5DjPM6Jn5WTl26H6QTjAQJBAL68 | |
vBzAYsMSDaQMyHGv3Ovlet8nKfGBZFkgtDtyU0mhXtA0yiPWA4ricOFswCkfql3d | |
YkNQ4NCXlMWxGy4o2OkCQQDCRqlyifxEVCTi2wty5d0cq1hWy0ZxSni+dz6+sUp5 | |
qpBaIqAp7HTHsFeJGfXDzeUpYBppiRuKECEHmDAmcKoI | |
-----END RSA PRIVATE KEY----- | |
*/ | |
public class EncTest { | |
// Open up a PEM file, and extract the keypair | |
public static KeyPair getKeyPairFromPEM(String pemFilename) throws Exception { | |
// Open the file | |
BufferedReader br = new BufferedReader(new FileReader(pemFilename)); | |
Security.addProvider(new BouncyCastleProvider()); | |
// Open the key | |
PEMParser pp = new PEMParser(br); | |
PEMKeyPair pemKeyPair = (PEMKeyPair) pp.readObject(); | |
KeyPair kp = new JcaPEMKeyConverter().getKeyPair(pemKeyPair); | |
pp.close(); | |
return kp; | |
} | |
// Decodes a byte array using the private key from a key pair | |
// returns a byte array containing the symmetric key | |
public static byte[] decodeUsingPrivateKey(KeyPair kp, byte[] raw_data) throws Exception { | |
// Extract the private key | |
PrivateKey privateKey = kp.getPrivate(); | |
// Get the cipher | |
Cipher cipher = Cipher.getInstance("RSA"); | |
cipher.init(Cipher.DECRYPT_MODE, privateKey); | |
// Decode the key | |
return cipher.doFinal(raw_data); | |
} | |
// Decodes a byte array using a symmetric key, and a initialization vector of 0 | |
// Returns a byte array containing the request data | |
public static byte[] decodeWithSymmetricKey(byte[] symmetric_key, byte[] raw_data) throws Exception { | |
// Get the cipher | |
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding"); | |
SecretKeySpec key = new SecretKeySpec(symmetric_key, "AES"); | |
byte[] iv = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }; | |
IvParameterSpec ivspec = new IvParameterSpec(iv); | |
System.out.println("Key Length: " + symmetric_key.length); | |
cipher.init(Cipher.DECRYPT_MODE, key, ivspec); | |
// Decode the data | |
System.out.println("To Decode: " + Base64.encodeBase64String(raw_data)); | |
return cipher.doFinal(raw_data); | |
} | |
// Verifies the validity of a byte array using the public key from a key pair, and a pre-calculates signature | |
// returns true if the signature matches, false otherwise | |
public static boolean verifySignatureUsingPublicKey(KeyPair kp, byte[] signed_data, byte[] signature) throws Exception { | |
// Extract the public key | |
PublicKey publicKey = kp.getPublic(); | |
// Initialize the signature algorithm | |
Signature signature_algorithm = Signature.getInstance("SHA384withRSA"); | |
signature_algorithm.initVerify(publicKey); | |
signature_algorithm.update(signed_data); | |
// Verify the result | |
return signature_algorithm.verify(signature); | |
} | |
public static void main (String[] args) { | |
try { | |
// Data to be processed | |
String signature64 = "G1Qem9wxbpZiWIW8cgAExCzFzdhXSmVGYVBnBvYEt1t9HH/Q3rchSMi/m5Iv Eg6ZvVaGGIdm4YuL/7/D8qv63EYEAOEjbE2k4B8yAe54g9xCFVYJCG2j8qtL /nPZNk4hYQLaAq9l7jXiyrwEcjcxr/J+tnvD/ZVJLG4tfGRInQo="; | |
String encData64 = "PL/ac9Nt3kMhk/2GYHGgbr7ML1MkTN9b3A5BhcJ+vgGR45zurFGZvShY2Oyt jzQlNG0K24bG+CDFp4qr3AC4nHhzWcVSWjU35k4mtQosUWRnht563DYmbD71 ArxJRF388ZBpoJpqul+zrQOiEbXKv9f/J60kyKvwPsRFZlSNlQLWGab3OI/5 aEfo2t7JKcjU7HHk5io4BVJO6ubqpASK6tRmBdIAVTI5l3cE1lkcX8ZCg1+F jIF38sxgM/8bVAiCi9o+ZrBYlsrhGCXHBSlVLu6y1U8RDvr8NWMsC6fDV/4Q eEC4DJFKR9li/HmPZvDr673uicgi+4P75dIAPsF41g=="; | |
String encKey64 = "rKxlHQdbgyDoM0tG4Jwh5zZ9KGaw2/+Iq078jLnrdKYloKnVOncoeW8MY9xy jNJE8vIPvjNdI0ZSCe1PnDsi9ByPz3m8yAY/BQGM7YzBEMlPUuf2yu25dw08 4iXatrJ/rxCwqHVE7BwrOCXDht+/ApuRGc3sd2CxqNv9rD/esQU="; | |
byte[] rawSig = Base64.decodeBase64(signature64); | |
byte[] rawData = Base64.decodeBase64(encData64); | |
byte[] rawKey = Base64.decodeBase64(encKey64); | |
// Get both of the keypairs | |
KeyPair keyPairTSYS = getKeyPairFromPEM("tsys.pem"); | |
KeyPair keyPairRogers = getKeyPairFromPEM("rogers.pem"); | |
// Get the symmetric key using the TSYS private key | |
byte[] rawDecodedKey = decodeUsingPrivateKey(keyPairTSYS, rawKey); | |
// Get the raw data using the decoded symmetric key | |
byte[] rawDecodedData = decodeWithSymmetricKey(rawDecodedKey, rawData); | |
// Verify the signature using the public key | |
boolean signature_result = verifySignatureUsingPublicKey(keyPairRogers, rawDecodedData, rawSig); | |
// Print some statistics out to the console | |
System.out.println("Base64 Key: " + Base64.encodeBase64String(rawDecodedKey)); | |
System.out.println("Decrypted Data: " + new String(rawDecodedData)); | |
System.out.println("Signature: " + signature_result); | |
System.out.println("done"); | |
} catch (Exception e) { | |
e.printStackTrace(System.err); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment