AES加密逐句分析、解密、md5
阿新 • • 發佈:2019-02-16
AES加密逐句分析
1.KeyGenerator kgen = KeyGenerator.getInstance("AES"); //例項化一個用AES加密演算法的金鑰生成器
2.kgen.init(128, new SecureRandom(password.getBytes())); //使用使用者提供的password初始化此金鑰生成器,使其具有確定的金鑰大小128位元組長。
3.SecretKey secretKey = kgen.generateKey(); //生成一個金鑰。
4.byte[] enCodeFormat = secretKey.getEncoded(); //返回基本編碼格式 的金鑰,如果此金鑰不支援編碼,則返回 null。
5.SecretKeySpec key = new SecretKeySpec(enCodeFormat, "AES"); //根據給定的enCodeFormat位元組陣列構造一個用AES演算法加密的金鑰。
6.Cipher cipher = Cipher.getInstance("AES");// 建立密碼器
7.cipher.init(Cipher.ENCRYPT_MODE, key);// 以加密的方式用金鑰初始化此 Cipher。
8.byte[] byteContent = content.getBytes("utf-8");//使用給定的UTF-8編碼 將此String編碼到byte序列,並將結果儲存到byteContent 陣列。
9.byte[] result = cipher.doFinal(byteContent); //按byteContent單部分操作加密指定的
return result; // 加密 返回加密過後的byteContent
建議:下載個jdk中文文件。自己對照就會了。
/** * AES加密 * @param content 待加密的內容 * @param encryptKey 加密金鑰 * @return 加密後的byte[] * @throws Exception