java加密工具类,可设置对应的加解密key
public class AesEncryptUtil { //使用AES-128-CBC加密模式,key需要为16位,key和iv可以相同! private static String KEY ="whshenke20180606"; private static String IV ="whshenke20180606"; /** * 加密方法 * @param data 要加密的数据 * @param key 加密key * @param iv 加密iv * @return 加密的结果 * @throws Exception */ public static String encrypt(String data, String key, String iv) throws Exception { try { Cipher cipher = Cipher.getInstance("AES/CBC/NoPadding");//"算法/模式/补码方式" int blockSize = cipher.getBlockSize(); byte[] dataBytes = data.getBytes(); int plaintextLength = dataBytes.length; if (plaintextLength % blockSize != 0) { plaintextLength = plaintextLength + (blockSize - (plaintextLength % blockSize)); } byte[] plaintext = new byte[plaintextLength]; System.arraycopy(dataBytes, 0, plaintext, 0, dataBytes.length); SecretKeySpec keyspec = new SecretKeySpec(key.getBytes("UTF-8"), "AES"); IvParameterSpec ivspec = new IvParameterSpec(iv.getBytes()); cipher.init(Cipher.ENCRYPT_MODE, keyspec, ivspec); byte[] encrypted = cipher.doFinal(plaintext); return Base64Util.encode(encrypted); } catch (Exception e) { e.printStackTrace(); return null; } } /** * 解密方法 * @param data 要解密的数据 * @param key 解密key * @param iv 解密iv * @return 解密的结果 * @throws Exception whshenke20180606 */ public static String desEncrypt(String data, String key, String iv) { try { byte[] encrypted1 = Base64Util.decode(data); Cipher cipher = Cipher.getInstance("AES/CBC/NoPadding"); SecretKeySpec keyspec = new SecretKeySpec(key.getBytes(), "AES"); IvParameterSpec ivspec = new IvParameterSpec(iv.getBytes()); cipher.init(Cipher.DECRYPT_MODE, keyspec, ivspec); byte[] original = cipher.doFinal(encrypted1); String originalString = new String(original, "UTF-8"); return originalString; } catch (Exception e) { //e.printStackTrace(); return null; } } /** * 使用默认的key和iv加密 * @param data * @return * @throws Exception */ public static String encrypt(String data) throws Exception { return encrypt(data, KEY, IV); } /** * 使用默认的key和iv解密 * @param data * @return * @throws Exception */ public static String desEncrypt(String data){ return desEncrypt(data, KEY, IV); } /** * 测试 */ public static void main(String args[]) throws Exception { //KNnAbiCvFxispeG+wS01jA== //KNnAbiCvFxispeG+wS01jA== //KNnAbiCvFxispeG+wS01jA== String test = "测试"; String data = null; String key = "dufy20170329java"; String iv = "dufy20170329java"; // data = encrypt(test, key, iv); //KNnAbiCvFxispeG+wS01jA== System.out.println(data); System.out.println(desEncrypt("8XguKV2aprVwg4UqEie7f9gkr7xDaSKrGZZD7x1J3uISWkvQAtpq8hpkskwi V+8bJmRcmQWIZ4sRom37GB7w7O4AH+HjH5ixViJ7k6bx+oEFVWSmIqKEj4e5 lMcyXgcXpTB8IGnt1WKS2p7Qnn+cjA==", key, iv)); } }
低调大师中文资讯倾力打造互联网数据资讯、行业资源、电子商务、移动互联网、网络营销平台。
持续更新报道IT业界、互联网、市场资讯、驱动更新,是最及时权威的产业资讯及硬件资讯报道平台。
转载内容版权归作者及来源网站所有,本站原创内容转载请注明来源。
- 上一篇
java项目部署运行
运用插件运行项目: tomcat-maven-plugin:1.1 1.右键项目--》runas --》5mavenbuild--》tomcat:run 2.name:部署名称(自定义,这里我使用maventest) basedirectory:项目文件夹路径 goals:运行命令:tomcat:run --》由于我用的是MyEclipse默认的maven 如果是tomcat6,这里就写tomcat6:run 3.打开console,可以看到部署项目的版本信息 还有部署项目的URL地址,这个URL就是你的项目的访问路径 默认:http://localhost:8080/mavenbos/
- 下一篇
(转载)Xcode中other linker flags的作用
原文地址:https://blog.csdn.net/iosfengguibin/article/details/52086435 今天导入三方的sdk之后,空项目,总是报错“selector not recognized”,在网上找到这篇文章,才解决了问题,感谢作者的分享。 使用场景 在ios开发中,我们经常会使用到第三方的一些静态库,导入第三方类库运行程序后你会发现,编译时可以正常编译但是运行时会app会闪退,报出selector not recognized的错误。一般的第三方库的开发文档中都会写出这种问题的解决方法,如在Other Linker Flags中加入-ObjC或者-all_load或者-force_load这样的解决方法。为什要这要做呢?报错为什么编译的时候有问题呢,首先我们先引入一个链接器的概念. 链接器 还记得我们在学习C程序的时候,从C代码到可执行文件经历的步骤是: 源代码 > 预处理器 > 编译器 > 汇编器 > 机器码 > 链接器 > 可执行文件 在最后一步需要把.o文件和C语言运行库链接起来,这时候需要用到ld命令。源...
相关文章
文章评论
共有0条评论来说两句吧...
文章二维码
点击排行
推荐阅读
最新文章
- CentOS8安装Docker,最新的服务器搭配容器使用
- Docker使用Oracle官方镜像安装(12C,18C,19C)
- CentOS8编译安装MySQL8.0.19
- CentOS8,CentOS7,CentOS6编译安装Redis5.0.7
- SpringBoot2整合MyBatis,连接MySql数据库做增删改查操作
- SpringBoot2整合Redis,开启缓存,提高访问速度
- SpringBoot2配置默认Tomcat设置,开启更多高级功能
- Hadoop3单机部署,实现最简伪集群
- CentOS7,CentOS8安装Elasticsearch6.8.6
- CentOS6,7,8上安装Nginx,支持https2.0的开启