AES加密、解密(linux、window加密解密效果一致,支持中文)
转自: http://sunfish.iteye.com/blog/2169158
import java.io.UnsupportedEncodingException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom; import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.KeyGenerator;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec; import org.apache.axis.encoding.Base64; public class AES {
private static int length=128;
/**
* 加密
*
* @param content
* 需要加密的内容
* @param password
* 加密密码
* @return
* @throws NoSuchAlgorithmException
* @throws NoSuchPaddingException
* @throws UnsupportedEncodingException
* @throws InvalidKeyException
* @throws BadPaddingException
* @throws IllegalBlockSizeException
*/
private static byte[] encrypt(String content, String password)
throws Exception { KeyGenerator kgen = KeyGenerator.getInstance("AES");
SecureRandom secureRandom = SecureRandom.getInstance("SHA1PRNG" );
secureRandom.setSeed(password.getBytes());
kgen.init(length, secureRandom);
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; // 加密 } /**
* 解密
*
* @param content
* 待解密内容
* @param password
* 解密密钥
* @return
*/
private static byte[] decrypt(byte[] content, String password)
throws Exception { KeyGenerator kgen = KeyGenerator.getInstance("AES");
SecureRandom secureRandom = SecureRandom.getInstance("SHA1PRNG" );
secureRandom.setSeed(password.getBytes());
kgen.init(length, secureRandom);
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; // 加密 } // /**
// * 将二进制转换成16进制
// *
// * @param buf
// * @return
// */
// public static String parseByte2HexStr(byte buf[]) {
// StringBuffer sb = new StringBuffer();
// for (int i = 0; i < buf.length; i++) {
// String hex = Integer.toHexString(buf[i] & 0xFF);
// if (hex.length() == 1) {
// hex = '0' + hex;
// }
// sb.append(hex.toUpperCase());
// }
// return sb.toString();
// }
//
// /**
// * 将16进制转换为二进制
// *
// * @param hexStr
// * @return
// */
// public static byte[] parseHexStr2Byte(String hexStr) {
// if (hexStr.length() < 1)
// return null;
// byte[] result = new byte[hexStr.length() / 2];
// for (int i = 0; i < hexStr.length() / 2; 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;
// } /**
* 加密
*
* @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;
} public static String encrypt2Str(String content, String password) throws Exception {
byte[] encryptResult = encrypt(content, password);
return Base64.encode(encryptResult);
} public static String decrypt2Str(String content, String password) throws Exception { byte[] decryptResult = decrypt(Base64.decode(content), password);
return new String(decryptResult,"UTF-8");
} public static void main(String[] args) throws Exception {
String content = "t太阳est地";
String password = "12345678";
// 加密
System.out.println("加密前:" + content); String tt4 = encrypt2Str(content, password);
System.out.println(new String(tt4)); // 解密
String d = decrypt2Str(tt4, password);
System.out.println("解密后:" + d); // 加密前:t太阳est地
// Bpf0jyJDj/pVHaRf66+OMA==
// 解密后:t太阳est地
}
}
AES加密、解密(linux、window加密解密效果一致,支持中文)的更多相关文章
- [Linux]Ubuntu下安装Sublime-text 且 支持中文输入
------------------------------------------------------------------------------------------ 首先进行如下操作: ...
- c# aes,des,md5加密等解密算法
一:可逆加密,即是能加密也能解密 对称可逆加密:加密后能解密回原文,加密key和解密key是一个 加密算法都是公开的,密钥是保密的, 即使拿到密文 你是推算不了密钥 也推算不了原文 加密解密的速度快, ...
- php/js/linux: js加密(rsa公钥加密) php解密(rsa私钥解密)
php/js/linux: js加密(rsa公钥加密) php解密(rsa私钥解密) 一: js rsa 插件 https://github.com/UFO0001/WX_RSA 或者: https: ...
- PHP AES cbc模式 pkcs7 128加密解密
今天在对接一个第三方接口的时候,对方需要AES CBC模式下的加密.这里简单写一个demo class Model_Junjingbao extends Model { private static ...
- AES加密解密&&SHA1、SHA加密&&MD5加密
AES加密解密 SHA1.SHA加密 MD5加密 二话不说立即附上代码: package com.luo.util; import java.io.UnsupportedEncodingExcepti ...
- AES中ECB模式的加密与解密(Python3.7)
本文主要解决的问题 本文主要是讲解AES加密算法中的ECB模式的加密解密的Python3.7实现.具体AES加密算法的原理这里不做过多介绍,想了解的可以参考文末的参考链接. 主要解决了两个问题: 在P ...
- aes加密在linux下会生成随机key的解决办法
直接贴代码了: package com.segerp.tygl.weixin.common; import java.io.UnsupportedEncodingException; import j ...
- Android DES加密的CBC模式加密解密和ECB模式加密解密
DES加密共有四种模式:电子密码本模式(ECB).加密分组链接模式(CBC).加密反馈模式(CFB)和输出反馈模式(OFB). CBC模式加密: import java.security.Key; i ...
- 各种加密解密函数(URL加密解密、sha1加密解密、des加密解密)
原文:各种加密解密函数(URL加密解密.sha1加密解密.des加密解密) 普通hash函数如md5.sha1.base64等都是不可逆函数.虽然我们利用php可以利用这些函数写出可逆函数来.但是跨语 ...
随机推荐
- 【BZOJ】BZOJ3040 最短路 线段树优化Dijkstra
题目描述 N个点,M条边的有向图,求点1到点N的最短路(保证存在). 1<=N<=1000000,1<=M<=10000000 输入格式 第一行两个整数N.M,表示点数和边数. ...
- Linux----添加zabbix-agent
1.zabbxi-agent安装及配置 1.1 获取官方zabbix源 [root@localhost ~]# rpm -ivh http://repo.zabbix.com/zabbix/3.4/r ...
- @Scope("prototype")的正确用法——解决Bean的多例问题
转自: https://www.jianshu.com/p/54b0711a8ec8 1. 问题,Spring管理的某个Bean需要使用多例 在使用了Spring的web工程中,除非特殊情况,我们 ...
- 范仁义web前端介绍课程---3、课程大纲(初步)
范仁义web前端介绍课程---3.课程大纲(初步) 一.总结 一句话总结: 知识点脉络(知识架构):刚开始对这个稍微了解一下就可以了,在逐步的学习过程中,心里大概有这样一套知识点的脉络 二.范仁义前端 ...
- c++ curl 登陆renren.com (cookie的使用)<转>
size_t write_callback( void *ptr, size_t size, size_t nmemb, void *stream ) { int len = size * nmemb ...
- python3 ssl导入失败
make LibreSSL 2.6.4 and earlier do not provide the necessary APIs /root/Python-3.7.0/build/lib.linux ...
- 免费s账号网站
下面网址按排序顺序优先使用,数字越小优先级越高 1,https://io.freess.today/ 2,https://free-ss.site/ 3,https://ss.freess.org/ ...
- Java ArrayList,LinkedList使用
1.ArrayList底层采用数组实现,当使用不带参数的构造方法生成ArrayList对象时,实际上回在底层生成一个长度为10的Object类型数组. 2.如果增加的元素个数超过10个,那么Array ...
- Python Django使用HttpResponse返回图片并显示
views.py def read_img(request): """ : 读取图片 :param request: :return: """ ...
- C# mvc后台传过来的list 怎么在js使用
var arr= JSON.parse('@Html.Raw(new System.Web.Script.Serialization.JavaScriptSerializer().Serialize( ...