




已阅读5页,还剩5页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
JAVA 实现 AES 加密算法代码 近些年 DES 使用越来越少 原因就在于其使用 56 位密 钥 比较容易被破解 近些年来逐渐被 AES 替代 AES 已 经变成目前对称加密中最流行算法之一 AES 可以使用 128 192 和 256 位密钥 并且用 128 位分组加密和解密 数据 本文就简单介绍如何通过 JAVA 实现 AES 加密 1 JAVA 实现 闲话少许 掠过 AES 加密原理及算法 关于这些直接搜索专业网站吧 我们直接看 JAVA 的具体 实现 1 1 加密 代码有详细解释 不多废话 加密 param content 需要加密的内容 param password 加密密码 return public static byte encrypt String content String password try KeyGenerator kgen KeyGenerator getInstance AES kgen init 128 new SecureRandom password getBytes SecretKey secretKey kgen generateKey byte enCodeFormat secretKey getEncoded SecretKeySpec key new SecretKeySpec enCodeFormat AES Cipher cipher Cipher getInstance AES 创建密码器 byte byteContent content getBytes utf 8 cipher init Cipher ENCRYPT MODE key 初始化 byte result cipher doFinal byteContent return result 加密 catch NoSuchAlgorithmException e e printStackTrace catch NoSuchPaddingException e e printStackTrace catch InvalidKeyException e e printStackTrace catch UnsupportedEncodingException e e printStackTrace catch IllegalBlockSizeException e e printStackTrace catch BadPaddingException e e printStackTrace return null 加密 param content 需要加密的内容 param password 加密密码 return public static byte encrypt String content String password try KeyGenerator kgen KeyGenerator getInstance AES kgen init 128 new SecureRandom password getBytes SecretKey secretKey kgen generateKey byte enCodeFormat secretKey getEncoded SecretKeySpec key new SecretKeySpec enCodeFormat AES Cipher cipher Cipher getInstance AES 创建密码器 byte byteContent content getBytes utf 8 cipher init Cipher ENCRYPT MODE key 初始化 byte result cipher doFinal byteContent return result 加密 catch NoSuchAlgorithmException e e printStackTrace catch NoSuchPaddingException e e printStackTrace catch InvalidKeyException e e printStackTrace catch UnsupportedEncodingException e e printStackTrace catch IllegalBlockSizeException e e printStackTrace catch BadPaddingException e e printStackTrace return null 2 2 解密 代码有详细注释 不多废 话 注意 解密的时候要传入 byte 数组 view plaincopy to clipboardprint 解密 param content 待解密内容 param password 解密密钥 return public static byte decrypt byte content String password try KeyGenerator kgen KeyGenerator getInstance AES kgen init 128 new SecureRandom password getBytes SecretKey secretKey kgen generateKey byte enCodeFormat secretKey getEncoded SecretKeySpec key new SecretKeySpec enCodeFormat AES Cipher cipher Cipher getInstance AES 创建密码器 cipher init Cipher DECRYPT MODE key 初始化 byte result cipher doFinal content return result 加 密 catch NoSuchAlgorithmException e e printStackTrace catch NoSuchPaddingException e e printStackTrace catch InvalidKeyException e e printStackTrace catch IllegalBlockSizeException e e printStackTrace catch BadPaddingException e e printStackTrace return null 解密 param content 待解密内容 param password 解密 密钥 return public static byte decrypt byte content String password try KeyGenerator kgen KeyGenerator getInstance AES kgen init 128 new SecureRandom password getBytes SecretKey secretKey kgen generateKey byte enCodeFormat secretKey getEncoded SecretKeySpec key new SecretKeySpec enCodeFormat AES Cipher cipher Cipher getInstance AES 创建密码器 cipher init Cipher DECRYPT MODE key 初始化 byte result cipher doFinal content return result 加 密 catch NoSuchAlgorithmException e e printStackTrace catch NoSuchPaddingException e e printStackTrace catch InvalidKeyException e e printStackTrace catch IllegalBlockSizeException e e printStackTrace catch BadPaddingException e e printStackTrace return null 2 3 测试代码 String content test String password 12345678 加密 System out println 加密前 content byte encryptResult encrypt content password 解密 byte decryptResult decrypt encryptResult password System out println 解密 后 new String decryptResult String content test String password 12345678 加密 System out println 加密前 content byte encryptResult encrypt content password 解密 byte decryptResult decrypt encryptResult password System out println 解密后 new String decryptResult 输出结果如下 加密前 test 解密后 test 2 4 容易出错的地方 但是如果我们将测试代码修改一下 如下 String content test String password 12345678 加密 System out println 加密前 content byte encryptResult encrypt content password try String encryptResultStr new String encryptResult utf 8 解密 byte decryptResult decrypt encryptResultStr getBytes utf 8 password System out println 解密后 new String decryptResult catch UnsupportedEncodingException e e printStackTrace String content test String password 12345678 加密 System out println 加密前 content byte encryptResult encrypt content password try String encryptResultStr new String encryptResult utf 8 解密 byte decryptResult decrypt encryptResultStr getBytes utf 8 password System out println 解密后 new String decryptResult catch UnsupportedEncodingException e e printStackTrace 则 系统会报出如下异常 javax crypto IllegalBlockSizeException Input length must be multiple of 16 when decrypting with padded cipher at com sun crypto provider SunJCE f b DashoA13 at com sun crypto provider SunJCE f b DashoA13 at com sun crypto provider AESCipher engineDoFinal DashoA13 at javax crypto Cipher doFinal DashoA13 这主 要是因为加密后的 byte 数组是不能强制转换成字符串的 换言之 字符串和 byte 数组在这种情况下不是互逆的 要 避免这种情况 我们需要做一些修订 可以考虑将二进制 数据转换成十六进制表示 主要有如下两个方法 2 4 1 将二进制转换成 16 进制 将二进制转换成 16 进 制 param buf return public static String parseByte2HexStr byte buf StringBuffer sb new StringBuffer for int i 0 i String hex Integer toHexString buf i if hex length 1 hex 0 hex sb append hex toUpperCase return sb toString 将二进制转换成 16 进制 param buf return public static String parseByte2HexStr byte buf StringBuffer sb new StringBuffer for int i 0 i String hex Integer toHexString buf i if hex length 1 hex 0 hex sb append hex toUpperCase return sb toString 2 4 2 将 16 进制转换为二进制 将 16 进制转换为二进制 param hexStr return public static byte parseHexStr2Byte String hexStr if hexStr length return null byte result new byte hexStr length 2 for int i 0 i int high Integer parseInt hexStr substring i 2 i 2 1 16 int low Integer parseInt hexStr substring i 2 1 i 2 2 16 result i byte high 16 low return result 将 16 进制转换为二进制 param hexStr return public static byte parseHexStr2Byte String hexStr if hexStr length return null byte result new byte hexStr length 2 for int i 0 i int high Integer parseInt hexStr substring i 2 i 2 1 16 int low Integer parseInt hexStr substring i 2 1 i 2 2 16 result i byte high 16 low return result 然后 我们再修订以上测试代码 如下 String content test String password 12345678 加密 System out println 加密前 content byte encryptResult encrypt content password String encryptResultStr parseByte2HexStr encryptResult System out println 加密后 encryptResultStr 解密 byte decryptFrom parseHexStr2Byte encryptResultStr byte decryptResult decrypt decryptFrom password System out println 解密后 new String decryptResult String content test String password 12345678 加密 System out println 加密前 content byte encryptResult encrypt content password String encryptResultStr parseByte2HexStr encryptResult System out println 加密后 encryptResultStr 解密 byte decryptFrom parseHexStr2Byte encryptResultStr byte decryptResult decrypt decryptFrom password System out println 解密后 new String decryptResult 测试结果如下 加密前 test 加密后 73C58BAFE578C59366D8C995CD0B9D6D 解密后 test 2 5 另外一种加密方式 还有一种加密方式 大家可以参 考如下 加密 param content 需 要加密的内容 param password 加密密码 return public static byte encrypt2 String content String password try SecretKeySpec key new SecretKeySpec password getBytes AES Cipher cipher Cipher getInstance AES ECB NoPadding byte byteContent content getBytes utf 8 cipher init Cipher ENCRYPT MODE key 初始化 byte result cipher doFinal byteContent return result 加密 catch NoSuchAlgorithmException e e printStackTrace catch NoSuchPaddingException e e printStackTrace catch InvalidKeyException e e printStackTrace catch UnsupportedEncodingException e e printStackTrace catch IllegalBlockSizeException e e printStackTrace catch BadPaddingException e e printStackTrace return null 加密 param content 需要加密的内容 param password 加密密码 return public static byte encrypt2 String content String password try SecretKeySpec key new SecretKeySpec password getBytes AES Cipher cipher Cipher getIn
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 包钢集团考试题型及答案
- 临床遴选试题及答案
- 大气干旱试题及答案
- 大班棋力考试题及答案
- java多态方法面试题及答案
- 2025年风景园林专业毕业设计开题报告
- 2025年材料物理专业毕业设计开题报告
- 2025年科学是美丽的试题及答案
- 2025年新增灯光考试题及答案解析
- 2025年交警招录考试题库
- 韶关学院《教育学》2023-2024学年第一学期期末试卷
- 合同履约保证信息台账模板
- DB62-T 5041-2024 凹凸棒石黏土矿产地质勘查规范
- 肝脏肿瘤手术麻醉
- 矿山安全生产法律法规
- 2024年广西公需科目参考答案
- 医学会当选委员主委副主任委员感言发言稿
- 2024城市滨水空间设计标准
- 巨人通力电梯NOVA GKE调试说明书故障代码GPN15 GVN15-GKE - 51668093D01-2022
- 广东深圳航空招聘笔试题库2024
- 2024连续油管技术规范
评论
0/150
提交评论