开箱即可使用的AES CBC加密解密算法分享
开箱即可使用的AES CBC加密解密算法分享
这两个天一个朋友让帮忙有java封装一个AES CBC算法的加解密,果断的Ctrl+C和Ctrl+V。修修改改形成了一下代码。至于什么是AES CBC模式加密算法,我也不知道(猛男落泪~~)。网上一搜一大堆这些资料感兴趣的可以百度。
Java自带没有AES加密算法的jar包,需要手动增加,此处使用maven自动下载:
<dependency> <groupId>org.bouncycastle</groupId> <artifactId>bcprov-jdk16</artifactId> <version>1.46</version></dependency>接下来就是正文了:
package cn.diaoyc.aes;import sun.misc.BASE64Decoder;import sun.misc.BASE64Encoder;import javax.crypto.Cipher;import javax.crypto.spec.IvParameterSpec;import javax.crypto.spec.SecretKeySpec;/** * @author Tony * @projectName AESCB * @title AecCBCUtil * @package cn.diaoyc.aes * @date 2020/5/10 -- 10:06 * @version v1.1 * */public class AecCBCUtil { // 加密方式 private static String ALGORITHM = "AES"; //算法数据填充方式 private static String ALGORITHM_FILL_TYPE = "AES/CBC/PKCS5Padding"; //字符编码(String转byte[] 使用UTF-8的编码格式) private static String ENCODING_FORMAT = "UTF-8"; // 内部实例参数 private static AecCBCUtil instance = null; private AecCBCUtil() { } //采用单例模式,此静态方法供外部直接访问 public static AecCBCUtil getInstance() { if (instance == null) { instance = new AecCBCUtil(); } return instance; } /** * 加密 * @param originalContent 明文 * @param encryptKey 密钥 * @param ivParameter 初始偏移量 * @return 返回加密后的字符串 */ public String encrypt(String originalContent, String encryptKey, String ivParameter) { try { //解决传进来的明文 byte[] originalContentBytes = originalContent.getBytes(ENCODING_FORMAT); //解决传进来的密钥(String转成byte[]) byte[] enKeyBytes = encryptKey.getBytes(); //解决传进来的偏移量(String转成byte[]) byte[] ivParameterBytes = ivParameter.getBytes(); //根据传入的密钥按照AEC方式构造密钥 SecretKeySpec sKeySpec = new SecretKeySpec(enKeyBytes, ALGORITHM); //根据传入的偏移量指定一个初始化偏移量 IvParameterSpec iv = new IvParameterSpec(ivParameterBytes); //根据数据填充方式生成一个加解密对象 Cipher cipher = Cipher.getInstance(ALGORITHM_FILL_TYPE); //初始化 传入类型(加密/解密)、构造过的密钥、指定的初始偏移量 cipher.init(Cipher.ENCRYPT_MODE, sKeySpec, iv); //加密操作 byte[] encrypted = cipher.doFinal(originalContentBytes); //base64转码 String cipherString = new BASE64Encoder().encode(encrypted); return cipherString; } catch (Exception e) { throw new RuntimeException(e); } } /** * 解密 * @param cipherStr 加密过的密文 * @param encryptKey 密钥 * @param ivParameter 偏移量 * @return 返回解密过后的字符串 */ public String decrypt(String cipherStr, String encryptKey, String ivParameter) { try { //解决传进来的密文 使用base64解密 byte[] cipherStrByte = new BASE64Decoder().decodeBuffer(cipherStr); //解决传进来的密钥(String转成byte[]) 可以指定编码格式为:ASCII byte[] enKeyBytes = encryptKey.getBytes(); //解决传进来的偏移量(String转成byte[]) byte[] ivParameterBytes = ivParameter.getBytes(); //根据传入的密钥按照AEC方式构造密钥 SecretKeySpec sKeySpec = new SecretKeySpec(enKeyBytes, ALGORITHM); Cipher cipher = Cipher.getInstance(ALGORITHM_FILL_TYPE); IvParameterSpec iv = new IvParameterSpec(ivParameterBytes); cipher.init(Cipher.DECRYPT_MODE, sKeySpec, iv); //取得解密的明文数组 byte[] original = cipher.doFinal(cipherStrByte); return new String(original, ENCODING_FORMAT); } catch (Exception e) { throw new RuntimeException(e); } }public static void main(String[] args) { String originalContent = "{\"fsadfds\":{\"fdsfsdf\":\"sdfdfds\",\"sfdsfsd\":{\"sfdsfd\":\"\",\"sfsdfsdf\":\"\",\"sfdsfdsf\":\"\",\"sfsfsdf\":\"\"},\"sfsfs\":\"sfsfsf\",\"Type\":1,\"sfsfsf\":\"sfsfsfsdfsdfdfsdfs\"},\"P\":\"sfsfsf-sfdsfsdf-123456\",\"U\":\"fsdfdfds-sfsdf\"}"; String encryptKey = "N200fqw455uEghx5"; //密钥 String ivParameter = "Ipahgj7Jkl0Gwe7s";//偏移量 System.out.println("原始字符串:" + originalContent); //加密 String encrypt = AecCBCUtil.getInstance().encrypt(originalContent, encryptKey, ivParameter); System.out.println("加密后的字符串是:" + encrypt); System.out.println("****************我是分割线*******************"); //解密 String decrypt = AecCBCUtil.getInstance().decrypt(encrypt, encryptKey, ivParameter); System.out.println("解密后的:" + decrypt); }}说明
1. 本站所有资源来源于用户上传和网络,如有侵权请邮件联系站长!
2. 分享目的仅供大家学习和交流,您必须在下载后24小时内删除!
3. 不得使用于非法商业用途,不得违反国家法律。否则后果自负!
4. 本站提供的源码、模板、插件等等其他资源,都不包含技术服务请大家谅解!
5. 如有链接无法下载、失效或广告,请联系管理员处理!
6. 本站资源售价只是摆设,本站源码仅提供给会员学习使用!
7. 如遇到加密压缩包,请使用360解压,如遇到无法解压的请联系管理员
开心源码网 » 开箱即可使用的AES CBC加密解密算法分享
1. 本站所有资源来源于用户上传和网络,如有侵权请邮件联系站长!
2. 分享目的仅供大家学习和交流,您必须在下载后24小时内删除!
3. 不得使用于非法商业用途,不得违反国家法律。否则后果自负!
4. 本站提供的源码、模板、插件等等其他资源,都不包含技术服务请大家谅解!
5. 如有链接无法下载、失效或广告,请联系管理员处理!
6. 本站资源售价只是摆设,本站源码仅提供给会员学习使用!
7. 如遇到加密压缩包,请使用360解压,如遇到无法解压的请联系管理员
开心源码网 » 开箱即可使用的AES CBC加密解密算法分享