IDEA安全编码组件 国际数据加密标准---IDEA:完全是新突破,几乎同时和AES出现 @author SeanDragon
| 24 | * @author SeanDragon |
| 25 | */ |
| 26 | public final class ToolIDEA { |
| 27 | private ToolIDEA() { |
| 28 | throw new UnsupportedOperationException("我是工具类,别初始化我。。。"); |
| 29 | } |
| 30 | |
| 31 | /** |
| 32 | * 密钥算法 |
| 33 | */ |
| 34 | public static final String KEY_ALGORITHM = "IDEA"; |
| 35 | /** |
| 36 | * 加密/解密算法 / 工作模式 / 填充方式 |
| 37 | */ |
| 38 | public static final String CIPHER_ALGORITHM = "IDEA/ECB/PKCS5Padding"; |
| 39 | private static final Logger log = LoggerFactory.getLogger(ToolIDEA.class); |
| 40 | |
| 41 | static { |
| 42 | // 加入BouncyCastleProvider支持 |
| 43 | Security.addProvider(new BouncyCastleProvider()); |
| 44 | } |
| 45 | |
| 46 | /** |
| 47 | * 转换密钥 |
| 48 | * |
| 49 | * @param key |
| 50 | * 二进制密钥 |
| 51 | * |
| 52 | * @return Key 密钥 |
| 53 | * |
| 54 | * @throws Exception |
| 55 | */ |
| 56 | private static Key toKey(byte[] key) { |
| 57 | // 生成秘密密钥 |
| 58 | SecretKey secretKey = new SecretKeySpec(key, KEY_ALGORITHM); |
| 59 | |
| 60 | return secretKey; |
| 61 | } |
| 62 | |
| 63 | /** |
| 64 | * 解密 |
| 65 | * |
| 66 | * @param data |
| 67 | * 待解密数据 |
| 68 | * @param key |
| 69 | * 密钥 |
| 70 | * |
| 71 | * @return byte[] 解密数据 |
| 72 | * |
| 73 | * @throws Exception |
| 74 | */ |
| 75 | public static byte[] decrypt(byte[] data, byte[] key) throws InvalidKeyException, BadPaddingException, IllegalBlockSizeException, NoSuchPaddingException, NoSuchAlgorithmException { |
| 76 | // 还原密钥 |
| 77 | Key k = toKey(key); |
| 78 | |
| 79 | // 实例化 |
| 80 | Cipher cipher = Cipher.getInstance(CIPHER_ALGORITHM); |
| 81 | |
| 82 | // 初始化,设置为解密模式 |
| 83 | cipher.init(Cipher.DECRYPT_MODE, k); |
nothing calls this directly
no outgoing calls
no test coverage detected