0
|
1 /*
|
|
2 * ConnectBot: simple, powerful, open-source SSH client for Android
|
|
3 * Copyright 2007 Kenny Root, Jeffrey Sharkey
|
|
4 *
|
|
5 * Licensed under the Apache License, Version 2.0 (the "License");
|
|
6 * you may not use this file except in compliance with the License.
|
|
7 * You may obtain a copy of the License at
|
|
8 *
|
|
9 * http://www.apache.org/licenses/LICENSE-2.0
|
|
10 *
|
|
11 * Unless required by applicable law or agreed to in writing, software
|
|
12 * distributed under the License is distributed on an "AS IS" BASIS,
|
|
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
14 * See the License for the specific language governing permissions and
|
|
15 * limitations under the License.
|
|
16 */
|
|
17
|
|
18 package com.five_ten_sg.connectbot.util;
|
|
19
|
|
20 /**
|
|
21 * This class is from:
|
|
22 *
|
|
23 * Encryptor.java
|
|
24 * Copyright 2008 Zach Scrivena
|
|
25 * zachscrivena@gmail.com
|
|
26 * http://zs.freeshell.org/
|
|
27 */
|
|
28
|
|
29 import java.security.MessageDigest;
|
|
30 import java.security.SecureRandom;
|
|
31 import java.util.Arrays;
|
|
32
|
|
33 import javax.crypto.Cipher;
|
|
34 import javax.crypto.spec.IvParameterSpec;
|
|
35 import javax.crypto.spec.SecretKeySpec;
|
|
36
|
|
37
|
|
38 /**
|
|
39 * Perform AES-128 encryption.
|
|
40 */
|
|
41 public final class Encryptor {
|
|
42 /** name of the character set to use for converting between characters and bytes */
|
|
43 private static final String CHARSET_NAME = "UTF-8";
|
|
44
|
|
45 /** random number generator algorithm */
|
|
46 private static final String RNG_ALGORITHM = "SHA1PRNG";
|
|
47
|
|
48 /** message digest algorithm (must be sufficiently long to provide the key and initialization vector) */
|
|
49 private static final String DIGEST_ALGORITHM = "SHA-256";
|
|
50
|
|
51 /** key algorithm (must be compatible with CIPHER_ALGORITHM) */
|
|
52 private static final String KEY_ALGORITHM = "AES";
|
|
53
|
|
54 /** cipher algorithm (must be compatible with KEY_ALGORITHM) */
|
|
55 private static final String CIPHER_ALGORITHM = "AES/CBC/PKCS5Padding";
|
|
56
|
|
57
|
|
58 /**
|
|
59 * Private constructor that should never be called.
|
|
60 */
|
|
61 private Encryptor()
|
|
62 {}
|
|
63
|
|
64
|
|
65 /**
|
|
66 * Encrypt the specified cleartext using the given password.
|
|
67 * With the correct salt, number of iterations, and password, the decrypt() method reverses
|
|
68 * the effect of this method.
|
|
69 * This method generates and uses a random salt, and the user-specified number of iterations
|
|
70 * and password to create a 16-byte secret key and 16-byte initialization vector.
|
|
71 * The secret key and initialization vector are then used in the AES-128 cipher to encrypt
|
|
72 * the given cleartext.
|
|
73 *
|
|
74 * @param salt
|
|
75 * salt that was used in the encryption (to be populated)
|
|
76 * @param iterations
|
|
77 * number of iterations to use in salting
|
|
78 * @param password
|
|
79 * password to be used for encryption
|
|
80 * @param cleartext
|
|
81 * cleartext to be encrypted
|
|
82 * @return
|
|
83 * ciphertext
|
|
84 * @throws Exception
|
|
85 * on any error encountered in encryption
|
|
86 */
|
|
87 public static byte[] encrypt(
|
|
88 final byte[] salt,
|
|
89 final int iterations,
|
|
90 final String password,
|
|
91 final byte[] cleartext)
|
|
92 throws Exception {
|
|
93 /* generate salt randomly */
|
|
94 SecureRandom.getInstance(RNG_ALGORITHM).nextBytes(salt);
|
|
95 /* compute key and initialization vector */
|
|
96 final MessageDigest shaDigest = MessageDigest.getInstance(DIGEST_ALGORITHM);
|
|
97 byte[] pw = password.getBytes(CHARSET_NAME);
|
|
98
|
|
99 for (int i = 0; i < iterations; i++) {
|
|
100 /* add salt */
|
|
101 final byte[] salted = new byte[pw.length + salt.length];
|
|
102 System.arraycopy(pw, 0, salted, 0, pw.length);
|
|
103 System.arraycopy(salt, 0, salted, pw.length, salt.length);
|
|
104 Arrays.fill(pw, (byte) 0x00);
|
|
105 /* compute SHA-256 digest */
|
|
106 shaDigest.reset();
|
|
107 pw = shaDigest.digest(salted);
|
|
108 Arrays.fill(salted, (byte) 0x00);
|
|
109 }
|
|
110
|
|
111 /* extract the 16-byte key and initialization vector from the SHA-256 digest */
|
|
112 final byte[] key = new byte[16];
|
|
113 final byte[] iv = new byte[16];
|
|
114 System.arraycopy(pw, 0, key, 0, 16);
|
|
115 System.arraycopy(pw, 16, iv, 0, 16);
|
|
116 Arrays.fill(pw, (byte) 0x00);
|
|
117 /* perform AES-128 encryption */
|
|
118 final Cipher cipher = Cipher.getInstance(CIPHER_ALGORITHM);
|
|
119 cipher.init(
|
|
120 Cipher.ENCRYPT_MODE,
|
|
121 new SecretKeySpec(key, KEY_ALGORITHM),
|
|
122 new IvParameterSpec(iv));
|
|
123 Arrays.fill(key, (byte) 0x00);
|
|
124 Arrays.fill(iv, (byte) 0x00);
|
|
125 return cipher.doFinal(cleartext);
|
|
126 }
|
|
127
|
|
128
|
|
129 /**
|
|
130 * Decrypt the specified ciphertext using the given password.
|
|
131 * With the correct salt, number of iterations, and password, this method reverses the effect
|
|
132 * of the encrypt() method.
|
|
133 * This method uses the user-specified salt, number of iterations, and password
|
|
134 * to recreate the 16-byte secret key and 16-byte initialization vector.
|
|
135 * The secret key and initialization vector are then used in the AES-128 cipher to decrypt
|
|
136 * the given ciphertext.
|
|
137 *
|
|
138 * @param salt
|
|
139 * salt to be used in decryption
|
|
140 * @param iterations
|
|
141 * number of iterations to use in salting
|
|
142 * @param password
|
|
143 * password to be used for decryption
|
|
144 * @param ciphertext
|
|
145 * ciphertext to be decrypted
|
|
146 * @return
|
|
147 * cleartext
|
|
148 * @throws Exception
|
|
149 * on any error encountered in decryption
|
|
150 */
|
|
151 public static byte[] decrypt(
|
|
152 final byte[] salt,
|
|
153 final int iterations,
|
|
154 final String password,
|
|
155 final byte[] ciphertext)
|
|
156 throws Exception {
|
|
157 /* compute key and initialization vector */
|
|
158 final MessageDigest shaDigest = MessageDigest.getInstance(DIGEST_ALGORITHM);
|
|
159 byte[] pw = password.getBytes(CHARSET_NAME);
|
|
160
|
|
161 for (int i = 0; i < iterations; i++) {
|
|
162 /* add salt */
|
|
163 final byte[] salted = new byte[pw.length + salt.length];
|
|
164 System.arraycopy(pw, 0, salted, 0, pw.length);
|
|
165 System.arraycopy(salt, 0, salted, pw.length, salt.length);
|
|
166 Arrays.fill(pw, (byte) 0x00);
|
|
167 /* compute SHA-256 digest */
|
|
168 shaDigest.reset();
|
|
169 pw = shaDigest.digest(salted);
|
|
170 Arrays.fill(salted, (byte) 0x00);
|
|
171 }
|
|
172
|
|
173 /* extract the 16-byte key and initialization vector from the SHA-256 digest */
|
|
174 final byte[] key = new byte[16];
|
|
175 final byte[] iv = new byte[16];
|
|
176 System.arraycopy(pw, 0, key, 0, 16);
|
|
177 System.arraycopy(pw, 16, iv, 0, 16);
|
|
178 Arrays.fill(pw, (byte) 0x00);
|
|
179 /* perform AES-128 decryption */
|
|
180 final Cipher cipher = Cipher.getInstance(CIPHER_ALGORITHM);
|
|
181 cipher.init(
|
|
182 Cipher.DECRYPT_MODE,
|
|
183 new SecretKeySpec(key, KEY_ALGORITHM),
|
|
184 new IvParameterSpec(iv));
|
|
185 Arrays.fill(key, (byte) 0x00);
|
|
186 Arrays.fill(iv, (byte) 0x00);
|
|
187 return cipher.doFinal(ciphertext);
|
|
188 }
|
|
189 }
|