Linux进行AES加密每次结果都不一致并且解密失败报错
1. 现象
windows操作系统下进行"123456"的AES加密
public static Key getSecretKey(String key) throws Exception {
SecretKey secureKey = null;
if (key == null) {
key = "";
}
KeyGenerator keyGenerator = KeyGenerator.getInstance("AES");
keyGenerator.init(new SecureRandom(key.getBytes()));
secureKey = keyGenerator.generateKey();
return secureKey;
}
修改到如下方式,问题解决:
public static Key getKey(String strKey) {
try {
if (strKey == null) {
strKey = "";
}
KeyGenerator _generator = KeyGenerator.getInstance("AES");
SecureRandom secureRandom = SecureRandom.getInstance("SHA1PRNG");
secureRandom.setSeed(strKey.getBytes());
_generator.init(128, secureRandom);
return _generator.generateKey();
} catch (Exception e) {
throw new RuntimeException(" 初始化密钥出现异常 ");
}
}
BASE64Encoder base64encoder = new BASE64Encoder();
String encode=base64encoder.encode(bytes);
2、解密前,需要将加密后的字符串从base64转回来再解密,如:
BASE64Decoder base64decoder = new BASE64Decoder();
byte[] encodeByte = base64decoder.decodeBuffer(str);
package com.binfoo.wechat.util; import java.security.Key;
import java.security.SecureRandom; import javax.crypto.Cipher;
import javax.crypto.KeyGenerator; import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder; public class SecurityUtil {
public static String DES = "AES"; // optional value AES/DES/DESede public static String CIPHER_ALGORITHM = "AES"; // optional value AES/DES/DESede public static Key getKey(String strKey) {
try {
if (strKey == null) {
strKey = "";
}
KeyGenerator _generator = KeyGenerator.getInstance("AES");
SecureRandom secureRandom = SecureRandom.getInstance("SHA1PRNG");
secureRandom.setSeed(strKey.getBytes());
_generator.init(128, secureRandom);
return _generator.generateKey();
} catch (Exception e) {
throw new RuntimeException(" 初始化密钥出现异常 ");
}
} public static String encrypt(String data, String key) throws Exception {
SecureRandom sr = new SecureRandom();
Key secureKey = getKey(key);
Cipher cipher = Cipher.getInstance(CIPHER_ALGORITHM);
cipher.init(Cipher.ENCRYPT_MODE, secureKey, sr);
byte[] bt = cipher.doFinal(data.getBytes());
String strS = new BASE64Encoder().encode(bt);
return strS;
} public static String decrypt(String message, String key) throws Exception {
SecureRandom sr = new SecureRandom();
Cipher cipher = Cipher.getInstance(CIPHER_ALGORITHM);
Key secureKey = getKey(key);
cipher.init(Cipher.DECRYPT_MODE, secureKey, sr);
byte[] res = new BASE64Decoder().decodeBuffer(message);
res = cipher.doFinal(res);
return new String(res);
} public static void main(String[] args) throws Exception {
String message = "123456";
String key = "landLeaf";
String encryptMsg = encrypt(message, key);
System.out.println("encrypted message is below :");
System.out.println(encryptMsg); String decryptedMsg = decrypt(encryptMsg, key);
System.out.println("decrypted message is below :");
System.out.println(decryptedMsg);
}
}
Linux进行AES加密每次结果都不一致并且解密失败报错的更多相关文章
- linux 下通过xhost进入图形界面,经常会出现报错“unable to open display”
linux 下通过xhost进入图形界面,经常会出现报错“unable to open display” linux下的操作步骤如下: [root@localhost ~]# vncserver N ...
- 因OpenCV版本不一致所引发的报错
目录 一 因OpenCV版本不一致所引发的报错 注:原创不易,转载请务必注明原作者和出处,感谢支持! 一 因OpenCV版本不一致所引发的报错 今天遇到了一个很有意思的报错. 事情是这样的, 在编译& ...
- R︱Linux+Rstudio Server尝鲜笔记(打造最佳Rstudio体验+报错的解决方案)
Rstudio Server 是Rstudio开发的基于R语言的网页版(只能在Linux),你在手机上都可以运行R,还是挺方便的.就是配置起来有点麻烦. 官方下载链接:https://www ...
- 进击的java - tomcat的安装,配置都正确之后,还是报错
1.问题 配置Apatch Tomcat过程报错: The CATALINA_HOME environment variable is not defined correctly.This envir ...
- linux查看与修改交换内存配置(解决zabbix-agent启动报错)
问题 zabbix-agent在一台centos6.5上启动报错: cannot allocate shared memory of size 949056: [28] No space left o ...
- HADOOP HA 报错 - 所有 namenode 都是standby --集群报错: Operation category READ is not supported in state standby
报错: 经过查看集群的jps如下: ==================== hadoop01 jps =================== FsShell ResourceManager Name ...
- 环境jdk、编码不一致造成的项目报错
一个项目在eclipse 中可以运行 , 到另一个eclipse 中不能运行,多是因为jdk过低.包没有引人.环境jdk.编码不一致造成的.或者是因为编译文件在另一个环境里跟JDK等 不匹配. 解决办 ...
- linux启动jmeter(二十三),执行./jmeter.sh报错解决方法(转载)
转载自 http://www.cnblogs.com/yangxia-test 1.l-bash: ./jmeter.sh: Permission denied解决办法:jmeter.sh的执行权限改 ...
- android 上AES解密是报错javax.crypto.BadPaddingException: pad block corrupted
网上看到两种方法: 1.SecretKeySpec skeySpec = new SecretKeySpec(getRawKey(key), "AES"); private sta ...
随机推荐
- react-native WebView 返回处理 (非回调方法可解决)
1.前言 项目中有些页面内容是变更比较频繁的,这些页面我们会考虑用网页来解决. 在RN项目中提供一个公用的Web页,如果是网页内容,就跳转到这个界面展示. 此时会有一个问题是,网页会有一级页面,二级页 ...
- Hive metastore整体代码分析及详解
从上一篇对Hive metastore表结构的简要分析中,我再根据数据设计的实体对象,再进行整个代码结构的总结.那么我们先打开metadata的目录,其目录结构: 可以看到,整个hivemeta的目录 ...
- linux下利用shell脚本实现添加crontab任务
本来直接用crontab -e 就可以打开vim,输入要执行的任务保存退出就可以添加任务直接启动运行了.但是今天组长说能不能写个shell不用打开vi就能添加到crontab的. 最先想到的是怎么在s ...
- ElasticSearch Kibana 和Logstash 安装x-pack记录
前言 最近用到了ELK的集群,想想还是用使用官方的x-pack的monitor功能对其进行监控,这里先上图看看: 环境如下: 操作系统: window 2012 R2 ELK : elasticsea ...
- python进阶学习笔记(一)
python进阶部分要学习的内容: 学习目标: 1.函数式编程 1.1,什么是函数式编程 函数式编程是一种抽象计算的编程模式 不同语言的抽象层次不同: 函数式编程的特点: python支持的函数式编程 ...
- MySQL Server 5.0安装教程
相信很多朋友刚开始接触mysql数据库服务器,下面是mysql的安装教程,步骤明细也有详细的说明. 工具/原料 mysql MySQL安装的图解 1 打开下载的mysql安装文件mysql-5 ...
- 机器学习之支持向量机(二):SMO算法
注:关于支持向量机系列文章是借鉴大神的神作,加以自己的理解写成的:若对原作者有损请告知,我会及时处理.转载请标明来源. 序: 我在支持向量机系列中主要讲支持向量机的公式推导,第一部分讲到推出拉格朗日对 ...
- Ubuntu上搭建SVN
参考文档:http://www.linuxidc.com/Linux/2016-08/133961.htm http://www.linuxidc.com/Linux/2015-01/111956.h ...
- github的拉取、提交,创建分支与合并
前期准备: 1.安装git 官网地址:https://git-scm.com/(下载下来,直接下一步) 2.github账号(这有点废话) 3.配置github密钥 下载及安装好git后,右 ...
- java获取windows下面的文件对象
import javax.swing.*;import javax.swing.filechooser.FileSystemView;import java.io.File; FileSystemVi ...