· 9 years ago · Dec 14, 2016, 10:04 AM
1package lain.messenger;
2
3import android.util.Log;
4
5import java.io.UnsupportedEncodingException;
6import java.math.BigInteger;
7import java.security.InvalidAlgorithmParameterException;
8import java.security.InvalidKeyException;
9import java.security.Key;
10import java.security.KeyFactory;
11import java.security.KeyPair;
12import java.security.KeyPairGenerator;
13import java.security.NoSuchAlgorithmException;
14import java.security.PublicKey;
15import java.security.SecureRandom;
16import java.security.interfaces.RSAPrivateKey;
17import java.security.spec.PKCS8EncodedKeySpec;
18import java.security.spec.RSAPublicKeySpec;
19import java.security.spec.X509EncodedKeySpec;
20
21import javax.crypto.BadPaddingException;
22import javax.crypto.Cipher;
23import javax.crypto.IllegalBlockSizeException;
24import javax.crypto.KeyGenerator;
25import javax.crypto.NoSuchPaddingException;
26import javax.crypto.SecretKey;
27import javax.crypto.spec.IvParameterSpec;
28import javax.crypto.spec.SecretKeySpec;
29import android.util.Base64;
30/**
31 * Created by Lain on 12/13/2016.
32 */
33
34
35public class CipherHelper {
36
37
38 String strDataToEncrypt = new String();
39 String strCipherText = new String();
40 String strDecryptedText = new String();
41 byte [] byteCipherText;
42 byte[] encodedBytes = null;
43
44 public byte[] encryptMessage(byte[] messageToEncrypt, byte[] RSAPublicKey) {
45
46
47 try {
48
49 //Init AES
50 KeyGenerator keyGen = KeyGenerator.getInstance("AES");
51 keyGen.init(256);
52 SecretKey secretKey = keyGen.generateKey();
53 Cipher aesCipherForEncryption = initAESCipher(secretKey.getEncoded(), Cipher.ENCRYPT_MODE);
54
55 //Encrypt message with AES
56 byte[] encryptedMessage = aesCipherForEncryption.doFinal(messageToEncrypt);
57
58
59 //Init RSA
60 Cipher c = Cipher.getInstance("RSA");
61 BigInteger publicExponent = new BigInteger("65537");
62
63 PublicKey publicKey = KeyFactory.getInstance("RSA").generatePublic(new RSAPublicKeySpec(new BigInteger(RSAPublicKey), publicExponent));
64 c.init(Cipher.ENCRYPT_MODE, publicKey);
65
66 Log.i("RSA", "Given rsa user key while encrypt: " + Base64.encodeToString(publicKey.toString().getBytes(), 1));
67
68 //Encrypt AES key with RSA
69 Log.i("AES", "Initial key " + Base64.encodeToString(secretKey.getEncoded(), 1));
70 byte[] encryptedKey = c.doFinal(secretKey.getEncoded());
71
72 Log.i("AES", "Key size is " + secretKey.getEncoded().length);
73 Log.i("AES", "Encrypted key: " + Base64.encodeToString(encryptedKey, 1));
74 Log.i("AES", "Encrypted key size is " + encryptedKey.length);
75 //Concat key with message
76 byte[] encryptedMessageWithKey = new byte[encryptedKey.length + encryptedMessage.length];
77
78 for (int i = 0; i < encryptedKey.length; i++) {
79 encryptedMessageWithKey[i] = encryptedKey[i];
80 }
81
82 for (int i = encryptedKey.length; i < encryptedMessageWithKey.length; i++) {
83 encryptedMessageWithKey[i] = encryptedMessage[i - encryptedKey.length];
84 }
85
86 //System.arraycopy(encryptedMessage, 0, encryptedMessageWithKey, encryptedKey.length, encryptedMessage.length + encryptedKey.length);
87
88 return encryptedMessageWithKey;
89
90
91 } catch (Exception e) {
92 e.printStackTrace();
93 }
94
95 return null;
96 }
97
98 public byte[] decryptMessage(byte[] encryptedMessage, RSAPrivateKey RSAPrivatekey) {
99
100
101
102
103 // Decode the encoded data with RSA public key
104 byte[] decodedAESKey = null;
105 try {
106 //init RSA cipher with given user key
107 Cipher c = Cipher.getInstance("RSA");
108 KeyFactory kf = KeyFactory.getInstance("RSA");
109 c.init(Cipher.DECRYPT_MODE, RSAPrivatekey);
110
111 Log.i("RSA", "private key while decrypt: " + Base64.encodeToString(RSAPrivatekey.getEncoded(), 1));
112
113 //decrypt AES key
114 int AESKeySize = 128;
115 byte[] encodedKey = new byte[AESKeySize];
116 System.arraycopy(encryptedMessage, 0, encodedKey, 0, AESKeySize);
117
118 Log.i("AES", "Key in message: " + Base64.encodeToString(encodedKey, 1));
119 Log.i("AES", "Key in message size: " + encodedKey.length);
120 decodedAESKey = c.doFinal(encodedKey);
121
122 Log.i("AES", "Decrypted key: " + Base64.encodeToString(decodedAESKey, 1));
123 Log.i("AES", "Decrypted key size: " + decodedAESKey.length);
124 //Decrypt message with AES key
125 Cipher aesCipherForDecryption = initAESCipher(decodedAESKey, Cipher.DECRYPT_MODE);
126
127 byte[] encodedText = new byte[encryptedMessage.length - AESKeySize];
128 System.arraycopy(encryptedMessage, AESKeySize, encodedText, 0, encryptedMessage.length - AESKeySize);
129
130 byte[] decryptedMessage = aesCipherForDecryption.doFinal(encodedText);
131 return decryptedMessage;
132
133 } catch (Exception e) {
134 e.printStackTrace();
135 }
136
137 //catched exception
138 return null;
139 }
140
141 private Cipher initAESCipher(byte[] key, int mode) throws NoSuchPaddingException, NoSuchAlgorithmException, InvalidAlgorithmParameterException, InvalidKeyException {
142
143 //create initialization vector and make it zero
144 /*int ivLength = 16;
145 byte[] iv = new byte[ivLength];
146 for (int i = 0; i < iv.length; i++) {
147 iv[i] = 0;
148 }*/
149
150 Cipher cipher = Cipher.getInstance("AES/ECB/PKCS7PADDING");
151 cipher.init(mode, new SecretKeySpec(key, 0, key.length, "AES"));
152
153 return cipher;
154 }
155}