转自: 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. python使用ThreadPoolExecutor每秒并发5个

    import time from concurrent.futures import ThreadPoolExecutor from functools import partial from log ...

  2. Chapter Four

    JSON数据 默认情况下,当开发者新创建一个SpringBoot项目时,会添加Web依赖,在这个依赖中会默认加入jackson-databind作为Json处理器. @RestController 组 ...

  3. postgresql【二】postgresql强制删除数据库

    SELECT pg_terminate_backend(pg_stat_activity.pid) FROM pg_stat_activity WHERE datname='db_name' AND ...

  4. Unity编辑器环境在Inspector面板中显示变量

    Serialize功能Unity3D 中提供了非常方便的功能可以帮助用户将 成员变量 在Inspector中显示,并且定义Serialize关系. 简单的说,在没有自定义Inspector的情况下所有 ...

  5. 使用FCKeditor编辑器上传文件时中文文件名乱码

    修改:editor\filemanager\browser\default\frmupload.html 文件的编码改为UTF-8 实在不行:fckeditor/editor/filemanager/ ...

  6. Linux 打开文件数

    linux设置最大打开文件数 - daiyudong2020的博客 - CSDN博客 https://blog.csdn.net/daiyudong2020/article/details/77828 ...

  7. Python3基础 for-else break、continue跳出循环示例

             Python : 3.7.3          OS : Ubuntu 18.04.2 LTS         IDE : pycharm-community-2019.1.3    ...

  8. android 应用签名的作用

    来源:https://www.jianshu.com/p/61206c96471a 1..应用程序升级:如果你希望用户无缝升级到新的版本,那么你必须用同一个证书进行签名.这是由于只有以同一个证书签名, ...

  9. Qt连接数据库

    Qt连接数据库,参数设置 //连接数据库 bool VCManageDatabase::connectMYSQL() { //判断testConnect连接是否存在并连接 if (QSqlDataba ...

  10. 算法习题---5-7打印队列(UVa12100)

    一:题目 有一个打印机,有一些任务在排着队打印,每个任务都有优先级.打印时,每次取出队列第一个任务,如果它的优先级不是当前队列中最高的,就会被放到队尾,否则就打印出来.输出初始队列的第m个任务的打印时 ...