|
1 | 1 | package com.codingapi.springboot.framework.crypto;
|
2 | 2 |
|
3 |
| -import org.apache.commons.crypto.stream.CryptoInputStream; |
4 |
| -import org.apache.commons.crypto.stream.CryptoOutputStream; |
5 |
| -import org.apache.commons.io.IOUtils; |
6 |
| -import org.springframework.util.Base64Utils; |
| 3 | +import org.bouncycastle.jce.provider.BouncyCastleProvider; |
7 | 4 |
|
| 5 | +import javax.crypto.Cipher; |
| 6 | +import javax.crypto.KeyGenerator; |
| 7 | +import javax.crypto.SecretKey; |
8 | 8 | import javax.crypto.spec.IvParameterSpec;
|
9 | 9 | import javax.crypto.spec.SecretKeySpec;
|
10 |
| -import java.io.ByteArrayInputStream; |
11 |
| -import java.io.ByteArrayOutputStream; |
12 | 10 | import java.io.IOException;
|
13 |
| -import java.io.InputStream; |
14 | 11 | import java.nio.charset.StandardCharsets;
|
15 |
| -import java.util.Properties; |
| 12 | +import java.security.AlgorithmParameters; |
| 13 | +import java.security.Key; |
| 14 | +import java.security.Security; |
| 15 | +import java.util.Random; |
16 | 16 |
|
17 | 17 | public class AES {
|
18 | 18 |
|
19 |
| - private final SecretKeySpec key; |
20 |
| - private final IvParameterSpec iv; |
21 |
| - private final Properties properties; |
22 |
| - private final String transform; |
| 19 | + public static final String KEY_ALGORITHM = "AES"; |
23 | 20 |
|
24 |
| - public AES(String transform, byte[] keys, byte[] iv) { |
25 |
| - this.key = new SecretKeySpec(keys, "AES"); |
26 |
| - this.iv = new IvParameterSpec(iv); |
27 |
| - this.properties = new Properties(); |
28 |
| - this.transform = transform; |
29 |
| - AESUtils.getInstance().init(this); |
| 21 | + public static final String CIPHER_ALGORITHM = "AES/CBC/PKCS7Padding"; |
| 22 | + |
| 23 | + private final Key key; |
| 24 | + private final AlgorithmParameters iv; |
| 25 | + private final String algorithm; |
| 26 | + |
| 27 | + public AES(String algorithm,Key key,AlgorithmParameters iv){ |
| 28 | + Security.addProvider(new BouncyCastleProvider()); |
| 29 | + this.algorithm = algorithm; |
| 30 | + this.key = key; |
| 31 | + this.iv = iv; |
| 32 | + } |
| 33 | + |
| 34 | + public AES(String algorithm, int generateKeySize) throws Exception { |
| 35 | + Security.addProvider(new BouncyCastleProvider()); |
| 36 | + this.algorithm = algorithm; |
| 37 | + this.key = generateKey(generateKeySize); |
| 38 | + this.iv = generateIV(randomIv()); |
| 39 | + } |
| 40 | + |
| 41 | + public AES(String algorithm, byte[] keys, byte[] ivs) throws Exception{ |
| 42 | + Security.addProvider(new BouncyCastleProvider()); |
| 43 | + this.algorithm = algorithm; |
| 44 | + this.key = convertToKey(keys); |
| 45 | + this.iv = generateIV(ivs); |
| 46 | + } |
| 47 | + |
| 48 | + public AES() throws Exception { |
| 49 | + this(CIPHER_ALGORITHM,256); |
| 50 | + } |
| 51 | + |
| 52 | + |
| 53 | + public AES(int generateKeySize) throws Exception { |
| 54 | + this(CIPHER_ALGORITHM,generateKeySize); |
| 55 | + } |
| 56 | + |
| 57 | + public AES(byte[] keys, byte[] ivs) throws Exception{ |
| 58 | + this(CIPHER_ALGORITHM,keys,ivs); |
| 59 | + } |
| 60 | + |
| 61 | + public AES(String key, String iv) throws Exception{ |
| 62 | + this(CIPHER_ALGORITHM,key.getBytes(StandardCharsets.UTF_8),iv.getBytes(StandardCharsets.UTF_8)); |
30 | 63 | }
|
31 | 64 |
|
| 65 | + private byte[] randomIv(){ |
| 66 | + Random random = new Random(); |
| 67 | + byte[] bytes = new byte[16]; |
| 68 | + random.nextBytes(bytes); |
| 69 | + return bytes; |
| 70 | + } |
32 | 71 |
|
33 |
| - public AES(String key, String iv) { |
34 |
| - this("AES/CBC/PKCS5Padding", key.getBytes(StandardCharsets.UTF_8), iv.getBytes(StandardCharsets.UTF_8)); |
| 72 | + private SecretKey generateKey(int keySize) throws Exception { |
| 73 | + KeyGenerator keyGenerator = KeyGenerator.getInstance(KEY_ALGORITHM); |
| 74 | + keyGenerator.init(keySize); |
| 75 | + return keyGenerator.generateKey(); |
35 | 76 | }
|
36 | 77 |
|
37 | 78 |
|
38 |
| - public String encode(String input) throws IOException { |
39 |
| - return Base64Utils.encodeToString(encode(input.getBytes(StandardCharsets.UTF_8))); |
| 79 | + private AlgorithmParameters generateIV(byte[] ivs) throws Exception { |
| 80 | + AlgorithmParameters params = AlgorithmParameters.getInstance(KEY_ALGORITHM); |
| 81 | + params.init(new IvParameterSpec(ivs)); |
| 82 | + return params; |
40 | 83 | }
|
41 | 84 |
|
42 |
| - public String decode(String input) throws IOException { |
43 |
| - return new String(decode(Base64Utils.decodeFromString(input)), StandardCharsets.UTF_8); |
| 85 | + private Key convertToKey(byte[] keyBytes){ |
| 86 | + return new SecretKeySpec(keyBytes, KEY_ALGORITHM); |
44 | 87 | }
|
45 | 88 |
|
| 89 | + public byte[] getKey(){ |
| 90 | + return key.getEncoded(); |
| 91 | + } |
46 | 92 |
|
47 |
| - public byte[] encode(byte[] input) throws IOException { |
48 |
| - final ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); |
49 |
| - try (CryptoOutputStream cos = new CryptoOutputStream(transform, properties, outputStream, key, iv)) { |
50 |
| - cos.write(input); |
51 |
| - cos.flush(); |
52 |
| - } |
53 |
| - return outputStream.toByteArray(); |
| 93 | + public byte[] getIv() throws IOException { |
| 94 | + return iv.getEncoded(); |
54 | 95 | }
|
55 | 96 |
|
56 |
| - public byte[] decode(byte[] input) throws IOException { |
57 |
| - final InputStream inputStream = new ByteArrayInputStream(input); |
58 |
| - try (CryptoInputStream cis = new CryptoInputStream(transform, properties, inputStream, key, iv)) { |
59 |
| - return IOUtils.toByteArray(cis); |
60 |
| - } |
| 97 | + public byte[] encrypt(byte[] data) throws Exception { |
| 98 | + Cipher cipher = Cipher.getInstance(algorithm); |
| 99 | + cipher.init(Cipher.ENCRYPT_MODE, key, iv); |
| 100 | + return cipher.doFinal(data); |
| 101 | + } |
| 102 | + |
| 103 | + |
| 104 | + public byte[] decrypt(byte[] encryptedData) throws Exception { |
| 105 | + Cipher cipher = Cipher.getInstance(algorithm); |
| 106 | + cipher.init(Cipher.DECRYPT_MODE, key, iv); |
| 107 | + return cipher.doFinal(encryptedData); |
61 | 108 | }
|
62 | 109 |
|
63 | 110 | }
|
0 commit comments