|
| 1 | +package codeAndDecode; |
| 2 | + |
| 3 | +import org.junit.jupiter.api.Assertions; |
| 4 | +import org.junit.jupiter.api.Test; |
| 5 | + |
| 6 | +import javax.crypto.BadPaddingException; |
| 7 | +import javax.crypto.Cipher; |
| 8 | +import javax.crypto.KeyGenerator; |
| 9 | +import javax.crypto.spec.SecretKeySpec; |
| 10 | +import java.security.SecureRandom; |
| 11 | + |
| 12 | +@SuppressWarnings({"SameParameterValue", "FieldCanBeLocal"}) |
| 13 | +public class CipherTest { |
| 14 | + |
| 15 | + private final String SRC = "Test"; |
| 16 | + private final String KEY = "key"; |
| 17 | + |
| 18 | + @Test |
| 19 | + void aes() throws Exception { |
| 20 | + test("AES", 128); |
| 21 | + } |
| 22 | + |
| 23 | + @Test |
| 24 | + void aes2() throws Exception { |
| 25 | + test("AES", 256); |
| 26 | + } |
| 27 | + |
| 28 | + @Test |
| 29 | + void des() throws Exception { |
| 30 | + test("DES", 56); |
| 31 | + } |
| 32 | + |
| 33 | + @Test |
| 34 | + void desEde() throws Exception { |
| 35 | + test("DESede", 168); |
| 36 | + } |
| 37 | + |
| 38 | + private void test(String algorithm, int keySize) throws Exception { |
| 39 | + byte[] encode = encode(SRC, KEY, algorithm, keySize); |
| 40 | + System.out.printf("code=%s, length=%d\n", new String(encode), encode.length); |
| 41 | + |
| 42 | + byte[] decode = decode(encode, KEY, algorithm, keySize); |
| 43 | + Assertions.assertArrayEquals(SRC.getBytes(), decode, "not equals"); |
| 44 | + |
| 45 | + Assertions.assertThrows(BadPaddingException.class, () -> decode(encode, "KEY", algorithm, keySize), "false key"); |
| 46 | + } |
| 47 | + |
| 48 | + /** |
| 49 | + * 加密 |
| 50 | + */ |
| 51 | + byte[] encode(String context, String key, String algorithm, int keySize) throws Exception { |
| 52 | + SecretKeySpec keySpec = getKey(keySize, algorithm, key.getBytes()); |
| 53 | + System.out.println("encode = " + keySpec.hashCode()); |
| 54 | + |
| 55 | + // 加密 |
| 56 | + Cipher cipher = Cipher.getInstance(algorithm); |
| 57 | + cipher.init(Cipher.ENCRYPT_MODE, keySpec); |
| 58 | + System.out.println("block size=" + cipher.getBlockSize()); |
| 59 | + return cipher.doFinal(context.getBytes()); |
| 60 | + } |
| 61 | + |
| 62 | + /** |
| 63 | + * 解密 |
| 64 | + */ |
| 65 | + byte[] decode(byte[] context, String key, String algorithm, int keySize) throws Exception { |
| 66 | + SecretKeySpec keySpec = getKey(keySize, algorithm, key.getBytes()); |
| 67 | + System.out.println("decode = " + keySpec.hashCode()); |
| 68 | + |
| 69 | + // 解密 |
| 70 | + Cipher cipher = Cipher.getInstance(algorithm); |
| 71 | + cipher.init(Cipher.DECRYPT_MODE, keySpec); |
| 72 | + System.out.println("block size=" + cipher.getBlockSize()); |
| 73 | + return cipher.doFinal(context); |
| 74 | + } |
| 75 | + |
| 76 | + @Test |
| 77 | + void keyTest() throws Exception { |
| 78 | + Assertions.assertArrayEquals( |
| 79 | + getKey(128, "AES", "AES".getBytes()).getEncoded(), |
| 80 | + getKey(128, "AES", "AES".getBytes()).getEncoded()); |
| 81 | + } |
| 82 | + |
| 83 | + /** |
| 84 | + * 生成密钥 |
| 85 | + * 采用传入密钥为随机算法种子生成实际密钥,可规避不同加密算法要求密钥长度不同 |
| 86 | + */ |
| 87 | + private SecretKeySpec getKey(int keySize, String algorithm, byte[] key) throws Exception { |
| 88 | + // 密钥生成器 |
| 89 | + KeyGenerator keyGenerator = KeyGenerator.getInstance(algorithm); |
| 90 | + // 随机数算法 |
| 91 | + SecureRandom secureRandom = SecureRandom.getInstance("SHA1PRNG"); |
| 92 | + // 种子 |
| 93 | + secureRandom.setSeed(key); |
| 94 | + // 生成 |
| 95 | + keyGenerator.init(keySize, secureRandom); |
| 96 | + return new SecretKeySpec(keyGenerator.generateKey().getEncoded(), algorithm); |
| 97 | + } |
| 98 | + |
| 99 | +// @Test |
| 100 | +// void nio() throws Exception { |
| 101 | +// // fail |
| 102 | +// ByteBuffer src = ByteBuffer.allocate(64); |
| 103 | +// src.put("Test1".getBytes()); |
| 104 | +// src.flip(); |
| 105 | +// System.out.println("src=" + src); |
| 106 | +// |
| 107 | +// SecretKeySpec encodeKeySpec = getKey(256, "AES", KEY.getBytes()); |
| 108 | +// Cipher encodeCipher = Cipher.getInstance("AES"); |
| 109 | +// encodeCipher.init(Cipher.ENCRYPT_MODE, encodeKeySpec); |
| 110 | +// |
| 111 | +// ByteBuffer encode = ByteBuffer.allocate(128); |
| 112 | +// System.out.println("encode=" + encodeCipher.update(src, encode)); |
| 113 | +// System.out.println("encode result=" + encode); |
| 114 | +// encodeCipher.doFinal(); |
| 115 | +// System.out.println("encode final=" + encode); |
| 116 | +// encode.flip(); |
| 117 | +// |
| 118 | +// SecretKeySpec decodeKeySpec = getKey(256, "AES", KEY.getBytes()); |
| 119 | +// Cipher decodeCipher = Cipher.getInstance("AES"); |
| 120 | +// decodeCipher.init(Cipher.DECRYPT_MODE, decodeKeySpec); |
| 121 | +// |
| 122 | +// ByteBuffer decode = ByteBuffer.allocate(64); |
| 123 | +// System.out.println("decode=" + decodeCipher.update(encode, decode)); |
| 124 | +// System.out.println("getOutputSize=" + decodeCipher.getOutputSize(64)); |
| 125 | +// decodeCipher.doFinal(); |
| 126 | +// |
| 127 | +// System.out.println(decode); |
| 128 | +// decode.flip(); |
| 129 | +// byte[] data = new byte[decode.limit()]; |
| 130 | +// decode.get(data); |
| 131 | +// System.out.println("decode result=" + new String(data)); |
| 132 | +// } |
| 133 | + |
| 134 | +} |
0 commit comments