· 6 years ago · Feb 01, 2019, 12:14 PM
1package utils;
2
3import android.annotation.SuppressLint;
4
5import java.security.InvalidKeyException;
6import java.security.KeyPairGenerator;
7import java.security.NoSuchAlgorithmException;
8import java.util.Arrays;
9import java.util.Random;
10
11import javax.crypto.BadPaddingException;
12import javax.crypto.Cipher;
13import javax.crypto.IllegalBlockSizeException;
14import javax.crypto.NoSuchPaddingException;
15import javax.crypto.SecretKey;
16import javax.crypto.spec.SecretKeySpec;
17
18public class EncryptionModule {
19
20 /**
21 * Generates an RSA-Keypair(Public+Private relation) using the KeyPairGenerator by Javax.crypto
22 * @return String[0] = publicKey; String[1] = privateKey
23 */
24 public static String[] generateRSA(){
25 String[] keypair = new String[2];
26
27 KeyPairGenerator keyGen = null;
28 try {
29 keyGen = KeyPairGenerator.getInstance("RSA");
30 } catch (NoSuchAlgorithmException e) {
31 e.printStackTrace();
32 }
33 if (keyGen != null) {
34 keyGen.initialize(256);
35 }
36 byte[] publicKey = new byte[0];
37 if (keyGen != null) {
38 publicKey = keyGen.genKeyPair().getPublic().getEncoded();
39 }
40 StringBuilder retString = new StringBuilder();
41 for (byte aPublicKey1 : publicKey) {
42 retString.append(Integer.toHexString(0x0100 + (aPublicKey1 & 0x00FF)).substring(1));
43 }
44
45 assert keyGen != null;
46 byte[] privateKey = keyGen.genKeyPair().getPrivate().getEncoded();
47 StringBuilder retStringPriv = new StringBuilder();
48 for (byte aPublicKey : publicKey) {
49 retStringPriv.append(Integer.toHexString(0x0100 + (aPublicKey & 0x00FF)).substring(1));
50 }
51
52 keypair[1] = retStringPriv.toString();
53
54 return keypair;
55 }
56
57 /**
58 * Generates a 32Parts long randomly generated String which is created with the following
59 * chars: a to z; A to Z; 0-9; some other visible UTF-8 characters
60 * @return AES cipher
61 */
62 public static String generateAESKey(){
63 Random random = new Random();
64 char[] key = new char[32];
65
66 for(int i = 0; i<32;i++){
67 key[i] = (char) (65 + random.nextInt(61));
68 }
69
70 return Arrays.toString(key);
71 }
72
73 /**
74 * Encrypts an inputted String that should be encrypted with the given parameters
75 * Code samples belong to https://stackoverflow.com/questions/15554296/simple-java-aes-encrypt-decrypt-example
76 * @param toEncrypt String that should be encrypted
77 * @param passkey passkey with which the {@code toEncrypt} plaintext should be encrypted
78 * @return returns the encrypted AES-String which has been encrypted with the {@code toEcrypt}
79 */
80 @SuppressLint("GetInstance")
81 public static String encryptAES(String toEncrypt, String passkey){
82
83 SecretKeySpec secretKey = new SecretKeySpec(passkey.getBytes(), "AES");
84 Cipher cipher = null;
85 try {
86 cipher = Cipher.getInstance("AES");
87 } catch (NoSuchAlgorithmException | NoSuchPaddingException e) {
88 e.printStackTrace();
89 }
90 try {
91 if (cipher != null) {
92 cipher.init(Cipher.ENCRYPT_MODE, secretKey);
93 }
94 } catch (InvalidKeyException e) {
95 e.printStackTrace();
96 }
97 try {
98 if (cipher != null) {
99 return new String(cipher.doFinal(toEncrypt.getBytes()));
100 }
101 } catch (BadPaddingException | IllegalBlockSizeException e) {
102 e.printStackTrace();
103 }
104 return null;
105 }
106
107 /**
108 * Decrypts an inputted encrypted String that should be decrypted with the given parameters
109 * Code samples belong to https://stackoverflow.com/questions/15554296/simple-java-aes-encrypt-decrypt-example
110 * @param toDecrypt the encrypted String that should be decrypted
111 * @param passkey the passkey with which the {@code toDecrypt} value should be decrypted
112 * @return returns the decrypted plaintext which has been decryoted with the {@code toDecrypt}
113 */
114 @SuppressLint("GetInstance")
115 public static String decryptAES(String toDecrypt, String passkey){
116 SecretKeySpec secretKey = new SecretKeySpec(passkey.getBytes(), "AES");
117 Cipher cipher;
118 try {
119 cipher = Cipher.getInstance("AES");
120 cipher.init(Cipher.DECRYPT_MODE, secretKey);
121 return new String(cipher.doFinal(toDecrypt.getBytes()));
122 } catch (NoSuchAlgorithmException | NoSuchPaddingException | InvalidKeyException | BadPaddingException | IllegalBlockSizeException e) {
123 e.printStackTrace();
124 }
125 return null;
126 }
127
128 /**
129 * Encrypts the given {@code toEncrypt} plaintext using RSA
130 * Relates to https://gist.github.com/dmydlarz/32c58f537bb7e0ab9ebf
131 * @param toEncrypt should be a 32parts long AES-Cipher
132 * @param publicKey the other user's public key
133 * @return encrypted {@code toEncrypt} RSA-encrypted AES-Cipher
134 */
135 public static String encryptRSA(String toEncrypt, String publicKey){
136 Cipher cipher;
137 try {
138 cipher = Cipher.getInstance("RSA");
139 SecretKey secretKey = new SecretKeySpec(publicKey.getBytes(), 0, publicKey.getBytes().length, "RSA");
140 cipher.init(Cipher.ENCRYPT_MODE, secretKey);
141 return new String(cipher.doFinal(toEncrypt.getBytes()));
142 } catch (NoSuchAlgorithmException | NoSuchPaddingException | BadPaddingException | IllegalBlockSizeException | InvalidKeyException e) {
143 e.printStackTrace();
144 }
145
146 return null;
147 }
148
149 /**
150 * Decrypts the given {@code toDecrypt} text using RSA
151 * Relates to https://gist.github.com/dmydlarz/32c58f537bb7e0ab9ebf
152 * @param toDecrpyt should be decrypted AES-cipher
153 * @param privateKey the user's private key
154 * @return encrypted {@code toEncrypt} RSA-decrypted AES-Cipher
155 */
156 public static String decryptRSA(String toDecrpyt, String privateKey){
157 Cipher cipher;
158 try {
159 cipher = Cipher.getInstance("RSA");
160 SecretKey secretKey = new SecretKeySpec(privateKey.getBytes(), 0, privateKey.getBytes().length, "RSA");
161 cipher.init(Cipher.DECRYPT_MODE, secretKey);
162 return new String(cipher.doFinal(toDecrpyt.getBytes()));
163 } catch (NoSuchAlgorithmException | NoSuchPaddingException | InvalidKeyException | BadPaddingException | IllegalBlockSizeException e) {
164 e.printStackTrace();
165 }
166 return null;
167 }
168}