转自: 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加密解密效果一致,支持中文)的更多相关文章

  1. [Linux]Ubuntu下安装Sublime-text 且 支持中文输入

    ------------------------------------------------------------------------------------------ 首先进行如下操作: ...

  2. c# aes,des,md5加密等解密算法

    一:可逆加密,即是能加密也能解密 对称可逆加密:加密后能解密回原文,加密key和解密key是一个 加密算法都是公开的,密钥是保密的, 即使拿到密文 你是推算不了密钥 也推算不了原文 加密解密的速度快, ...

  3. php/js/linux: js加密(rsa公钥加密) php解密(rsa私钥解密)

    php/js/linux: js加密(rsa公钥加密) php解密(rsa私钥解密) 一: js rsa 插件 https://github.com/UFO0001/WX_RSA 或者: https: ...

  4. PHP AES cbc模式 pkcs7 128加密解密

    今天在对接一个第三方接口的时候,对方需要AES CBC模式下的加密.这里简单写一个demo class Model_Junjingbao extends Model { private static ...

  5. AES加密解密&amp;&amp;SHA1、SHA加密&amp;&amp;MD5加密

    AES加密解密 SHA1.SHA加密 MD5加密 二话不说立即附上代码: package com.luo.util; import java.io.UnsupportedEncodingExcepti ...

  6. AES中ECB模式的加密与解密(Python3.7)

    本文主要解决的问题 本文主要是讲解AES加密算法中的ECB模式的加密解密的Python3.7实现.具体AES加密算法的原理这里不做过多介绍,想了解的可以参考文末的参考链接. 主要解决了两个问题: 在P ...

  7. aes加密在linux下会生成随机key的解决办法

    直接贴代码了: package com.segerp.tygl.weixin.common; import java.io.UnsupportedEncodingException; import j ...

  8. Android DES加密的CBC模式加密解密和ECB模式加密解密

    DES加密共有四种模式:电子密码本模式(ECB).加密分组链接模式(CBC).加密反馈模式(CFB)和输出反馈模式(OFB). CBC模式加密: import java.security.Key; i ...

  9. 各种加密解密函数(URL加密解密、sha1加密解密、des加密解密)

    原文:各种加密解密函数(URL加密解密.sha1加密解密.des加密解密) 普通hash函数如md5.sha1.base64等都是不可逆函数.虽然我们利用php可以利用这些函数写出可逆函数来.但是跨语 ...

随机推荐

  1. mysql bigint与bigint unsigned

    -------------------------------以下是个人根据网上翻阅加个人理解总结结果------------------------------- mysql 表中数据类型和存储过程 ...

  2. windows传文件到linux服务器--- secureCRT PK xftp

    背景: 需要从windows上传下载文件到aws虚拟服务器上并进行服务器环境搭建,由于secureCRT的局限性它只支持pub格式的密钥,不支持pem格式密钥,xshell是支持pem格式的,所以尝试 ...

  3. pymongo helper

    import pymongo import click # 数据库基本信息 db_configs = { 'type': 'mongo', 'host': '127.0.0.1', 'port': ' ...

  4. Ubuntu安装邮件服务器

    Ubuntu搭建邮件服务器 此文我们使用Postfix来搭建邮箱服务器,Postifx是一个SMTP服务器.SMTP服务器也被称为MTA(message transfer agent) 一.安装pos ...

  5. Mysql 按年、季度、月、周查询统计

    User表 CREATE TABLE `user` ( `id` int(11) NOT NULL AUTO_INCREMENT COMMENT '用户ID', `username` varchar( ...

  6. env (arcpy)

    addOutputsToMap (读写) 设置是否应将工具产生的输出数据集添加至应用程序显示. Boolean autoCommit (读写) 支持“自动提交”环境的工具将在 ArcSDE 事务中进行 ...

  7. RK3399 pro 开发记录

    RK3399有三种启动模式:1.Normal模式:2.Loader模式:3.MaskRom模式.      Normal模式是正常的启动过程,各个组件依次加载,直到正常进入系统.      Loade ...

  8. 如何将eclipse项目导入到idea

    intellij idea中文资料网上比较少,对于eclipse的项目如何导入intellij idea也没有完整的说明,本人在这里整理下,方便更多人加入到intellij idea的阵容里. 直接上 ...

  9. Xamarin图表开发基础教程(1)

    Xamarin图表开发基础教程(1) 在Xamarin图表开发中,最常用的框架是OxyPlot和Microcharts.其中,OxyOPlot提供多种多样的图表类型和丰富的图表功能,可以实现各种复杂的 ...

  10. PHP 类属性

    属性 (Properties) 类的变量成员叫做“属性”,或者叫“字段”.“特征”,在本文档统一称为“属性”.属性声明是由关键字 public,protected或者 private 开头,然后跟一个 ...