加密 @param data 待加密数据 @param key 密钥 @return byte[] 加密数据 @throws Exception
(byte[] data, byte[] key)
| 104 | * @throws Exception |
| 105 | */ |
| 106 | public static byte[] encrypt(byte[] data, byte[] key) throws NoSuchPaddingException, NoSuchAlgorithmException, InvalidKeyException, BadPaddingException, IllegalBlockSizeException { |
| 107 | // 还原密钥 |
| 108 | Key k = toKey(key); |
| 109 | |
| 110 | /* |
| 111 | * 实例化 使用PKCS7Padding填充方式,按如下方式实现 Cipher.getInstance(CIPHER_ALGORITHM, |
| 112 | * "BC"); |
| 113 | */ |
| 114 | Cipher cipher = Cipher.getInstance(CIPHER_ALGORITHM); |
| 115 | |
| 116 | // 初始化,设置为加密模式 |
| 117 | cipher.init(Cipher.ENCRYPT_MODE, k); |
| 118 | |
| 119 | // 执行操作 |
| 120 | return cipher.doFinal(data); |
| 121 | } |
| 122 | |
| 123 | /** |
| 124 | * 生成密钥 <br> |
nothing calls this directly
no test coverage detected