theresasogunle /
Rave-Java-Library
| 1 | /* |
||
| 2 | * To change this license header, choose License Headers in Project Properties. |
||
| 3 | * To change this template file, choose Tools | Templates |
||
| 4 | * and open the template in the editor. |
||
| 5 | */ |
||
| 6 | package com.github.theresasogunle; |
||
| 7 | |||
| 8 | import java.io.UnsupportedEncodingException; |
||
| 9 | import java.security.InvalidKeyException; |
||
| 10 | import java.security.MessageDigest; |
||
| 11 | import java.util.Arrays; |
||
| 12 | import java.security.NoSuchAlgorithmException; |
||
| 13 | import java.util.Base64; |
||
| 14 | import java.util.logging.Level; |
||
| 15 | import java.util.logging.Logger; |
||
| 16 | import javax.crypto.BadPaddingException; |
||
| 17 | import javax.crypto.Cipher; |
||
| 18 | import javax.crypto.IllegalBlockSizeException; |
||
| 19 | import javax.crypto.NoSuchPaddingException; |
||
| 20 | import javax.crypto.SecretKey; |
||
| 21 | import javax.crypto.spec.SecretKeySpec; |
||
| 22 | import org.json.JSONObject; |
||
| 23 | /** |
||
| 24 | * |
||
| 25 | * @author Theresa |
||
| 26 | */ |
||
| 27 | |||
| 28 | |||
| 29 | public class Encryption { |
||
| 30 | |||
| 31 | |||
| 32 | RaveConstant keys= new RaveConstant(); |
||
| 33 | |||
| 34 | |||
| 35 | // Method to turn bytes in hex |
||
| 36 | public static String toHexStr(byte[] bytes){ |
||
| 37 | |||
| 38 | StringBuilder builder = new StringBuilder(); |
||
| 39 | |||
| 40 | for(int i = 0; i < bytes.length; i++ ){ |
||
| 41 | builder.append(String.format("%02x", bytes[i])); |
||
| 42 | } |
||
| 43 | |||
| 44 | return builder.toString(); |
||
| 45 | } |
||
| 46 | |||
| 47 | // this is the getKey function that generates an encryption Key for you by passing your Secret Key as a parameter. |
||
| 48 | public static String getKey(String seedKey) { |
||
| 49 | try { |
||
| 50 | MessageDigest md = MessageDigest.getInstance("md5"); |
||
| 51 | byte[] hashedString = md.digest(seedKey.getBytes("utf-8")); |
||
| 52 | byte[] subHashString = toHexStr(Arrays.copyOfRange(hashedString, hashedString.length - 12, hashedString.length)).getBytes("utf-8"); |
||
|
0 ignored issues
–
show
|
|||
| 53 | String subSeedKey = seedKey.replace("FLWSECK-", ""); |
||
| 54 | subSeedKey = subSeedKey.substring(0, 12); |
||
|
0 ignored issues
–
show
|
|||
| 55 | byte[] combineArray = new byte[24]; |
||
|
0 ignored issues
–
show
|
|||
| 56 | System.arraycopy(subSeedKey.getBytes(), 0, combineArray, 0, 12); |
||
|
0 ignored issues
–
show
|
|||
| 57 | System.arraycopy(subHashString, subHashString.length - 12, combineArray, 12, 12); |
||
|
0 ignored issues
–
show
|
|||
| 58 | return new String(combineArray); |
||
|
0 ignored issues
–
show
|
|||
| 59 | } catch (NoSuchAlgorithmException | UnsupportedEncodingException ex) { |
||
| 60 | Logger.getGlobal().log(Level.SEVERE, null, ex); |
||
| 61 | } |
||
| 62 | return null; |
||
| 63 | } |
||
| 64 | |||
| 65 | // This is the encryption function that encrypts your payload by passing the stringified format and your encryption Key. |
||
| 66 | public static String encryptData(String message, String _encryptionKey) { |
||
| 67 | try { |
||
| 68 | final byte[] digestOfPassword = _encryptionKey.getBytes("utf-8"); |
||
| 69 | final byte[] keyBytes = Arrays.copyOf(digestOfPassword, 24); |
||
|
0 ignored issues
–
show
|
|||
| 70 | |||
| 71 | final SecretKey key = new SecretKeySpec( keyBytes , "DESede"); |
||
| 72 | final Cipher cipher = Cipher.getInstance("DESede/ECB/PKCS5Padding"); |
||
|
0 ignored issues
–
show
|
|||
| 73 | cipher.init(Cipher.ENCRYPT_MODE, key); |
||
| 74 | final byte[] plainTextBytes = message.getBytes("utf-8"); |
||
| 75 | final byte[] cipherText = cipher.doFinal(plainTextBytes); |
||
| 76 | return Base64.getEncoder().encodeToString(cipherText); |
||
| 77 | |||
| 78 | } catch (UnsupportedEncodingException | NoSuchAlgorithmException | NoSuchPaddingException | InvalidKeyException | IllegalBlockSizeException | BadPaddingException e) { |
||
|
0 ignored issues
–
show
Ignoring an Exception may lead to hard to find bugs. Consider logging or rethrowing the original exception. If you want to throw a different exception, you can set the original exception as its cause to preserve the stacktrace.
When instantiating a new Exception, you can set another Exception as its cause. See the Oracle documentation on Throwables. Usage example throw new Exception("Exception Message", originalException);
Complete Example: class ReThrowException {
public static void throwsException() {
try {
throw new Exception("I am the original exception");
} catch (final Exception e) {
throw new RuntimeException("I am the new exception", e);
}
}
public static void main(String[] args) {
try {
throwsException();
}
catch (final RuntimeException e) {
System.out.println(e.getMessage());
System.out.println("and my cause is: " + e.getCause().getMessage());
e.printStackTrace();
}
}
}
Loading history...
|
|||
| 79 | |||
| 80 | |||
| 81 | return ""; |
||
| 82 | } |
||
| 83 | |||
| 84 | } |
||
| 85 | |||
| 86 | /** |
||
| 87 | * |
||
| 88 | * @param api(JSON object) |
||
| 89 | * @return String |
||
| 90 | */ |
||
| 91 | |||
| 92 | public String encryptParameters(JSONObject api) { |
||
| 93 | |||
| 94 | try{ |
||
| 95 | api.put("PBFPubKey",RaveConstant.PUBLIC_KEY); |
||
| 96 | }catch(Exception ex){} |
||
|
0 ignored issues
–
show
Ignoring an Exception may lead to hard to find bugs. Consider logging or rethrowing the original exception. If you want to throw a different exception, you can set the original exception as its cause to preserve the stacktrace.
When instantiating a new Exception, you can set another Exception as its cause. See the Oracle documentation on Throwables. Usage example throw new Exception("Exception Message", originalException);
Complete Example: class ReThrowException {
public static void throwsException() {
try {
throw new Exception("I am the original exception");
} catch (final Exception e) {
throw new RuntimeException("I am the new exception", e);
}
}
public static void main(String[] args) {
try {
throwsException();
}
catch (final RuntimeException e) {
System.out.println(e.getMessage());
System.out.println("and my cause is: " + e.getCause().getMessage());
e.printStackTrace();
}
}
}
Loading history...
|
|||
| 97 | |||
| 98 | |||
| 99 | |||
| 100 | String message= api.toString(); |
||
| 101 | |||
| 102 | String encrypt_secret_key=getKey(RaveConstant.SECRET_KEY); |
||
| 103 | String encrypted_message= encryptData(message,encrypt_secret_key); |
||
| 104 | |||
| 105 | |||
| 106 | return encrypted_message; |
||
| 107 | |||
| 108 | } |
||
| 109 | /** |
||
| 110 | * |
||
| 111 | * |
||
| 112 | * @return String |
||
| 113 | * @param api |
||
| 114 | * |
||
| 115 | */ |
||
| 116 | |||
| 117 | |||
| 118 | public String encryptParametersPreAuth(JSONObject api){ |
||
| 119 | |||
| 120 | try{ |
||
| 121 | api.put("PBFPubKey","FLWPUBK-8cd258c49f38e05292e5472b2b15906e-X"); |
||
| 122 | }catch(Exception ex){} |
||
|
0 ignored issues
–
show
Ignoring an Exception may lead to hard to find bugs. Consider logging or rethrowing the original exception. If you want to throw a different exception, you can set the original exception as its cause to preserve the stacktrace.
When instantiating a new Exception, you can set another Exception as its cause. See the Oracle documentation on Throwables. Usage example throw new Exception("Exception Message", originalException);
Complete Example: class ReThrowException {
public static void throwsException() {
try {
throw new Exception("I am the original exception");
} catch (final Exception e) {
throw new RuntimeException("I am the new exception", e);
}
}
public static void main(String[] args) {
try {
throwsException();
}
catch (final RuntimeException e) {
System.out.println(e.getMessage());
System.out.println("and my cause is: " + e.getCause().getMessage());
e.printStackTrace();
}
}
}
Loading history...
|
|||
| 123 | |||
| 124 | String message= api.toString(); |
||
| 125 | |||
| 126 | |||
| 127 | |||
| 128 | String encrypt_secret_key=getKey("FLWSECK-c51891678d48c39eff3701ff686bdb69-X"); |
||
| 129 | String encrypted_message= encryptData(message,encrypt_secret_key); |
||
| 130 | |||
| 131 | |||
| 132 | return encrypted_message; |
||
| 133 | |||
| 134 | } |
||
| 135 | |||
| 136 | |||
| 137 | |||
| 138 | } |
||
| 139 |
Using constants for hard-coded numbers is a best practice. A constant’s name can explain the rationale behind this magic number. It is also easier to find if you ever need to change it.