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可以利用这些函数写出可逆函数来.但是跨语 ...
随机推荐
- 数据结构实验之查找二:平衡二叉树 (SDUT 3374)
#include <stdio.h> #include <string.h> #include <stdlib.h> struct node { int data; ...
- 命令备忘 ss
简介: Socket Statistics(ss)命令类似于netstat,它用于显示各种有用的网络套接字信息. 长时间看,已经注意到netstat这个命令程序已经过时了.从而代替netstat的是s ...
- 【随记】SQL Server配置管理器”远程过程调用失败“的问题解决
打开配置管理器时出现如下错误: 问题原因: 在电脑上安装vs2012.vs2013,vs2015.vs2017.vs2019时会安装[Express LocalDB] 解决方案: 卸载所有[XXXLo ...
- 01_tf和numpy的区别
import numpy as npimport tensorflow as tf # 这里是为了演示numpy和tf的区别.np.random.seed(43) x_data = np.random ...
- Ubuntu --- 安装deb包
在Ubuntu下安装deb包需要使用dpkg命令.Dpkg 的普通用法: 1.sudo dpkg -i <package.deb> 安装一个 Debian 软件包,如你手动下载的文件. 2 ...
- Spring’s RestTemplate
Spring’s RestTemplate /** * After the word document is generated in memory we can upload it to the s ...
- [Java/File]读取日文CSV文件不乱码
try { StringBuilder sb=new StringBuilder(); sb.append("\nContent in File:'"+filePathname+& ...
- QtCore概述
所有其他Qt模块都依赖于这个模块. 要包含模块类的定义,请使用以下指令: include < QtCore > 如果您使用qmake来构建您的项目,则默认将QtCore包含在内. 核心功能 ...
- leetcode 374. Guess Number Higher or Lower 、375. Guess Number Higher or Lower II
374. Guess Number Higher or Lower 二分查找就好 // Forward declaration of guess API. // @param num, your gu ...
- Dart接口
/* 和Java一样,dart也有接口,但是和Java还是有区别的. 首先,dart的接口没有interface关键字定义接口,而是普通类或抽象类都可以作为接口被实现. 同样使用implements关 ...