· 6 years ago · Apr 30, 2019, 01:56 PM
1 public static class KeyUtils {
2 public static String encrypt(Cipher encryptCipher, String inputData) throws Exception {
3 return new String(org.apache.commons.codec.binary.Base64.encodeBase64(encryptCipher.doFinal(inputData.toString().getBytes("UTF-8"))), "UTF-8");
4 }
5
6 public static String decrypt(Cipher decryptCipher, String outputData) throws Exception {
7 return new String(decryptCipher.doFinal(org.apache.commons.codec.binary.Base64.decodeBase64(outputData.getBytes("UTF-8"))), "UTF-8");
8 }
9
10 public static byte[] decrypt(Cipher decryptCipher, byte[] outputData) throws Exception {
11 byte[] array = decryptCipher.doFinal(org.apache.commons.codec.binary.Base64.decodeBase64(outputData));
12 return array;
13 }
14
15 public static String sign(String envelope, PrivateKey privateKey) throws Exception {
16 Signature privateSignature = Signature.getInstance("SHA256withRSA");
17 privateSignature.initSign(privateKey);
18 privateSignature.update(envelope.getBytes("UTF-8"));
19
20 return new String(org.apache.commons.codec.binary.Base64.encodeBase64(privateSignature.sign()), "UTF-8");
21 }
22
23 public static String generaSecureRandom() {
24 SecureRandom random = new SecureRandom();
25 byte[] ivBytes = new byte[16];
26 random.nextBytes(ivBytes);
27 return new String(Hex.encodeHex(ivBytes));
28 }
29
30 public static String calculateRFC2104HMAC(String envelope, String symmetricKey) throws Exception {
31 Mac mac = Mac.getInstance("HmacSHA256");
32 mac.init(new SecretKeySpec(symmetricKey.getBytes(), "HmacSHA256"));
33
34 byte[] rawHmac = mac.doFinal(envelope.getBytes());
35 return new String(org.apache.commons.codec.binary.Base64.encodeBase64(rawHmac));
36 }
37
38 public static Cipher generaCipher(int mode, Key key)
39 throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException {
40 Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding");
41 cipher.init(mode, key);
42 return cipher;
43 }
44
45 public static Cipher generaCipher(String salt, String iv, int mode, String symmetricKey) throws Exception {
46 byte[] saltBytesCrypt = Hex.decodeHex(salt.toCharArray());
47 byte[] ivBytesCrypt = Hex.decodeHex(iv.toCharArray());
48 IvParameterSpec ipsCrypt = new IvParameterSpec(ivBytesCrypt);
49 KeySpec keySpecCrypt = new PBEKeySpec(symmetricKey.toCharArray(), saltBytesCrypt, 100, 128);
50 SecretKeyFactory keyFactoryCrypt = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1");
51 SecretKey keyCrypt = new SecretKeySpec(keyFactoryCrypt.generateSecret(keySpecCrypt).getEncoded(), "AES");
52 Cipher cipherCrypt = Cipher.getInstance("AES/CBC/PKCS5Padding");
53 cipherCrypt.init(mode, keyCrypt, ipsCrypt);
54 return cipherCrypt;
55 }
56
57 public static PrivateKey loadPrivateKey(String key64) throws GeneralSecurityException {
58 byte[] clear = org.apache.commons.codec.binary.Base64.decodeBase64(key64);
59 PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(clear);
60 KeyFactory fact = KeyFactory.getInstance("RSA");
61 PrivateKey priv = fact.generatePrivate(keySpec);
62 Arrays.fill(clear, (byte) 0);
63 return priv;
64 }
65
66 public static PublicKey loadPublicKey(String stored) throws GeneralSecurityException {
67 byte[] data = org.apache.commons.codec.binary.Base64.decodeBase64(stored);
68 X509EncodedKeySpec spec = new X509EncodedKeySpec(data);
69 KeyFactory fact = KeyFactory.getInstance("RSA");
70 return fact.generatePublic(spec);
71 }
72
73 public static String savePrivateKey(PrivateKey priv) throws GeneralSecurityException {
74 KeyFactory fact = KeyFactory.getInstance("RSA");
75 PKCS8EncodedKeySpec spec = fact.getKeySpec(priv, PKCS8EncodedKeySpec.class);
76 return new String(org.apache.commons.codec.binary.Base64.encodeBase64(spec.getEncoded()));
77 }
78
79 public static String savePublicKey(PublicKey publ) throws GeneralSecurityException {
80 KeyFactory fact = KeyFactory.getInstance("RSA");
81 X509EncodedKeySpec spec = fact.getKeySpec(publ, X509EncodedKeySpec.class);
82 return new String(org.apache.commons.codec.binary.Base64.encodeBase64(spec.getEncoded()));
83 }
84
85 public static String generaRSAEnvelope(String operation_timestamp, JSONObject data)
86 throws UnsupportedEncodingException {
87 String body = new String(org.apache.commons.codec.binary.Base64.encodeBase64(data.toString().getBytes("UTF-8")), "UTF-8");
88 return body + "\n" + operation_timestamp;
89 }
90
91 public static String generaAESEnvelope(String httpMethod, String url, String app_version,
92 String operation_timestamp, String data) throws UnsupportedEncodingException {
93 url = url.replaceAll(".*\\/client\\/(public|private)\\/*", "").replaceAll("http(s)?://", "");
94
95 String body = data != null ? data.toString() : "";
96
97 if (httpMethod.equals("GET") && url.contains("?"))
98 body = url.replaceAll(".*\\?", "");
99
100 body = new String(org.apache.commons.codec.binary.Base64.encodeBase64(body.getBytes("UTF-8")), "UTF-8");
101
102 return httpMethod + "\n" + url + "\n" + body + "\n" + app_version + "\n" + operation_timestamp;
103 }
104
105
106 }