您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息
免费发信息

JAVA代码实现:AES加密

2024/4/24 21:03:39发布3次查看
aes加密aes 是一种可逆加密算法,对用户的敏感信息加密处理。
本文暂不深入aes原理,仅关注java代码实现aes加解密。
java代码实现建议加密密码为16位,避免密码位数不足补0,导致密码不一致,加解密错误。
ios可设置任意长度的加密密码,java只支持16位/24位/32位,不知能否实现任意长度,望大佬告之。
package cn.roylion.common.util;import sun.misc.base64decoder;import sun.misc.base64encoder;import javax.crypto.badpaddingexception;import javax.crypto.cipher;import javax.crypto.illegalblocksizeexception;import javax.crypto.nosuchpaddingexception;import javax.crypto.spec.secretkeyspec;import java.io.ioexception;import java.io.unsupportedencodingexception;import java.security.invalidkeyexception;import java.security.nosuchalgorithmexception;/** * @author: roylion * @description: aes算法封装 * @date: created in 9:46 2018/8/9 */public class encryptutil{ /** * 加密算法 */ private static final string encry_algorithm = "aes"; /** * 加密算法/加密模式/填充类型 * 本例采用aes加密,ecb加密模式,pkcs5padding填充 */ private static final string cipher_mode = "aes/ecb/pkcs5padding"; /** * 设置iv偏移量 * 本例采用ecb加密模式,不需要设置iv偏移量 */ private static final string iv_ = null; /** * 设置加密字符集 * 本例采用 utf-8 字符集 */ private static final string character = "utf-8"; /** * 设置加密密码处理长度。 * 不足此长度补0; */ private static final int pwd_size = 16; /** * 密码处理方法 * 如果加解密出问题, * 请先查看本方法,排除密码长度不足补"0",导致密码不一致 * @param password 待处理的密码 * @return * @throws unsupportedencodingexception */ private static byte[] pwdhandler(string password) throws unsupportedencodingexception { byte[] data = null; if (password == null) { password = ""; } stringbuffer sb = new stringbuffer(pwd_size); sb.append(password); while (sb.length() < pwd_size) { sb.append("0"); } if (sb.length() > pwd_size) { sb.setlength(pwd_size); } data = sb.tostring().getbytes("utf-8"); return data; } //======================>原始加密<====================== /** * 原始加密 * @param cleartextbytes 明文字节数组,待加密的字节数组 * @param pwdbytes 加密密码字节数组 * @return 返回加密后的密文字节数组,加密错误返回null */ public static byte[] encrypt(byte[] cleartextbytes, byte[] pwdbytes) { try { // 1 获取加密密钥 secretkeyspec keyspec = new secretkeyspec(pwdbytes, encry_algorithm); // 2 获取cipher实例 cipher cipher = cipher.getinstance(cipher_mode); // 查看数据块位数 默认为16(byte) * 8 =128 bit// system.out.println("数据块位数(byte):" + cipher.getblocksize()); // 3 初始化cipher实例。设置执行模式以及加密密钥 cipher.init(cipher.encrypt_mode, keyspec); // 4 执行 byte[] ciphertextbytes = cipher.dofinal(cleartextbytes); // 5 返回密文字符集 return ciphertextbytes; } catch (nosuchpaddingexception e) { e.printstacktrace(); } catch (nosuchalgorithmexception e) { e.printstacktrace(); } catch (badpaddingexception e) { e.printstacktrace(); } catch (illegalblocksizeexception e) { e.printstacktrace(); } catch (invalidkeyexception e) { e.printstacktrace(); } catch (exception e) { e.printstacktrace(); } return null; } /** * 原始解密 * @param ciphertextbytes 密文字节数组,待解密的字节数组 * @param pwdbytes 解密密码字节数组 * @return 返回解密后的明文字节数组,解密错误返回null */ public static byte[] decrypt(byte[] ciphertextbytes, byte[] pwdbytes) { try { // 1 获取解密密钥 secretkeyspec keyspec = new secretkeyspec(pwdbytes, encry_algorithm); // 2 获取cipher实例 cipher cipher = cipher.getinstance(cipher_mode); // 查看数据块位数 默认为16(byte) * 8 =128 bit// system.out.println("数据块位数(byte):" + cipher.getblocksize()); // 3 初始化cipher实例。设置执行模式以及加密密钥 cipher.init(cipher.decrypt_mode, keyspec); // 4 执行 byte[] cleartextbytes = cipher.dofinal(ciphertextbytes); // 5 返回明文字符集 return cleartextbytes; } catch (nosuchalgorithmexception e) { e.printstacktrace(); } catch (invalidkeyexception e) { e.printstacktrace(); } catch (nosuchpaddingexception e) { e.printstacktrace(); } catch (badpaddingexception e) { e.printstacktrace(); } catch (illegalblocksizeexception e) { e.printstacktrace(); } catch (exception e) { e.printstacktrace(); } // 解密错误 返回null return null; } //======================>base64<====================== /** * base64加密 * @param cleartext 明文,待加密的内容 * @param password 密码,加密的密码 * @return 返回密文,加密后得到的内容。加密错误返回null */ public static string encryptbase64(string cleartext, string password) { try { // 1 获取加密密文字节数组 byte[] ciphertextbytes = encrypt(cleartext.getbytes(character), pwdhandler(password)); // 2 对密文字节数组进行base64 encoder 得到 base6输出的密文 base64encoder base64encoder = new base64encoder(); string ciphertext = base64encoder.encode(ciphertextbytes); // 3 返回base64输出的密文 return ciphertext; } catch (unsupportedencodingexception e) { e.printstacktrace(); } catch (exception e) { e.printstacktrace(); } // 加密错误 返回null return null; } /** * base64解密 * @param ciphertext 密文,带解密的内容 * @param password 密码,解密的密码 * @return 返回明文,解密后得到的内容。解密错误返回null */ public static string decryptbase64(string ciphertext, string password) { try { // 1 对 base64输出的密文进行base64 decodebuffer 得到密文字节数组 base64decoder base64decoder = new base64decoder(); byte[] ciphertextbytes = base64decoder.decodebuffer(ciphertext); // 2 对密文字节数组进行解密 得到明文字节数组 byte[] cleartextbytes = decrypt(ciphertextbytes, pwdhandler(password)); // 3 根据 character 转码,返回明文字符串 return new string(cleartextbytes, character); } catch (unsupportedencodingexception e) { e.printstacktrace(); } catch (ioexception e) { e.printstacktrace(); } catch (exception e) { e.printstacktrace(); } // 解密错误返回null return null; } //======================>hex<====================== /** * hex加密 * @param cleartext 明文,待加密的内容 * @param password 密码,加密的密码 * @return 返回密文,加密后得到的内容。加密错误返回null */ public static string encrypthex(string cleartext, string password) { try { // 1 获取加密密文字节数组 byte[] ciphertextbytes = encrypt(cleartext.getbytes(character), pwdhandler(password)); // 2 对密文字节数组进行 转换为 hex输出密文 string ciphertext = byte2hex(ciphertextbytes); // 3 返回 hex输出密文 return ciphertext; } catch (unsupportedencodingexception e) { e.printstacktrace(); } catch (exception e) { e.printstacktrace(); } // 加密错误返回null return null; } /** * hex解密 * @param ciphertext 密文,带解密的内容 * @param password 密码,解密的密码 * @return 返回明文,解密后得到的内容。解密错误返回null */ public static string decrypthex(string ciphertext, string password) { try { // 1 将hex输出密文 转为密文字节数组 byte[] ciphertextbytes = hex2byte(ciphertext); // 2 将密文字节数组进行解密 得到明文字节数组 byte[] cleartextbytes = decrypt(ciphertextbytes, pwdhandler(password)); // 3 根据 character 转码,返回明文字符串 return new string(cleartextbytes, character); } catch (unsupportedencodingexception e) { e.printstacktrace(); } catch (exception e) { e.printstacktrace(); } // 解密错误返回null return null; } /*字节数组转成16进制字符串 */ public static string byte2hex(byte[] bytes) { // 一个字节的数, stringbuffer sb = new stringbuffer(bytes.length * 2); string tmp = ""; for (int n = 0; n < bytes.length; n++) { // 整数转成十六进制表示 tmp = (java.lang.integer.tohexstring(bytes[n] & 0xff)); if (tmp.length() == 1) { sb.append("0"); } sb.append(tmp); } return sb.tostring().touppercase(); // 转成大写 } /*将hex字符串转换成字节数组 */ private static byte[] hex2byte(string str) { if (str == null || str.length() < 2) { return new byte[0]; } str = str.tolowercase(); int l = str.length() / 2; byte[] result = new byte[l]; for (int i = 0; i < l; ++i) { string tmp = str.substring(2 * i, 2 * i + 2); result[i] = (byte) (integer.parseint(tmp, 16) & 0xff); } return result; } public static void main(string[] args) { string test = encrypthex("test", "1234567800000000"); system.out.println(test); system.out.println(decrypthex(test, "1234567800000000")); }}
相关推荐:
加密解密 - 用php实现java中的aes加密
aes加密解密c#代码
以上就是java代码实现:aes加密的详细内容。
该用户其它信息

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录