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地 } }
其中的org.apache.axis.encoding.Base64,可以直接用附件中的Base64代替(直接从axis反编译而得)
相关推荐
1. 在深入理解AES加密/解密算法理论的基础上,设计一个AES加密/解密软件系统; 2. 完成CBC和ECB模式加密解密
用C语言实现的AES加密解密算法,用C语言实现的AES加密解密算法
VB.NET实现AES加密解密代码,可直接使用
AES加密解密轮转算法工具,最大支持256bit长度的密钥,明文支持HEX和字串。可供学习设计参考。
本文实例为大家分享了python实现AES加密解密的具体代码,供大家参考,具体内容如下 (1)对于AES加密解密相关知识 (2)实现的功能就是输入0-16个字符,然后经过AES的加密解密最后可以得到原先的输入,运行的结果...
' 功能: AES加密解密 password<==>hwWpiegaWDeE8kTlb/5BQg== , 8705a989e81a583784f244e56ffe4142 ' [Str] 预备转换的byte ' [StrKey] 16位的密钥 ' [StrIv] 16位的偏移 ' [Encrypt] 加密(缺省值)还是解密 ' ...
支持密码长度AES_128/AES_192/AES_256,支持工作模式 ECB/CBC,支持填充模式 ZERO/PKCS7/ISO。已通过测试。内附使用方法。
详细演示AES加密解密过程.AES是分组密钥,算法输入128位数据,密钥长度也是128位。用Nr表示对一个数据分组加密的轮数(加密轮数与密钥长度的关系如表1所列)。每一轮都需要一个与输入分组具有相同长度的扩展密钥...
AES加密解密验证工具(支持各种加密模式)
高级加密标准(英语:Advanced Encryption Standard,缩写:AES),在密码学中又称Rijndael加密法,是美国联邦政府采用的一种区块加密标准。 该压缩包中包括aes加密算法的c++实现代码。
aes加密解密js
本地图片AES加密解密,利用AES加密的方式将图片保存于本地,其他App获取该图片无法显示,信息安全可靠加密速度高且十分有效,最好是保存的图片源文件在500KB以下为最佳!
php+uni-app AES加密解密
用Velirog语言实现AES加密解密,可在FPGA上实现
简介:采用密钥为16位长度的加密字符,算法为AES,CBC模式,PKCS5Padding填充模式。在Demo中使用了两个转换方法,二进制转为16进制,16进制转为2进制。 //加密解密的key String key = "1234567890"; //输入的内容 ...
AES加密解密工具 AES 是一种使用安全码进行信息加密的标准。 它支持 128 位、192 位和 256 位的密匙。 加密算法的实现在 ElAES.pas 单元中。 本人将其加密方法封装在 AES.pas 单元中, 只需要调用两个标准函数就可以...
主要介绍了java使用Hex编码解码实现Aes加密解密功能,结合完整实例形式分析了Aes加密解密功能的定义与使用方法,需要的朋友可以参考下
AES加密解密算法演示支持d7及xe AES加密解密算法演示支持d7及xe
js aes加密解密示例!js aes加密解密示例!js aes加密解密示例!
aes加密解密工具类,解决在linux服务加密解密异常问题。