标签:AES return String 解密 Java result 加密 byte
一 概述
此例子用于简单的加密解密操作
二 自定义规则的MD5加密解密
// MD5加密32位
public static String MD5(String inStr) {
MessageDigest md5 = null;
try {
md5 = MessageDigest.getInstance("MD5");
} catch (Exception e) {
System.out.println(e.toString());
e.printStackTrace();
return "";
}
char[] charArray = inStr.toCharArray();
byte[] byteArray = new byte[charArray.length];
for (int i = 0; i < charArray.length; i++)
byteArray[i] = (byte) charArray[i];
byte[] md5Bytes = md5.digest(byteArray);
StringBuffer hexValue = new StringBuffer();
for (int i = 0; i < md5Bytes.length; i++) {
int val = ((int) md5Bytes[i]) & 0xff;
if (val < 16)
hexValue.append("0");
hexValue.append(Integer.toHexString(val));
}
return hexValue.toString();
}
加密
// 可逆的加密算法
public static String encode(String inStr) {
char[] a = inStr.toCharArray();
for (int i = 0; i < a.length; i++) {
a[i] = (char) (a[i] ^ 'p');
}
String s = new String(a);
return s;
}
解密
// 加密后解密
public static String decode(String inStr) {
char[] a = inStr.toCharArray();
for (int i = 0; i < a.length; i++) {
a[i] = (char) (a[i] ^ 'p');
}
String k = new String(a);
return k;
}
三 基于JDK的AES加密解密
加密
//加密密钥
public static final String secret = "QINGSHANKEJIWAYINANDYINGE";
/**
* @Author zhoucheng
* @MethodName AESJDKEncode
* @Param [message, Key]
* @Date 18:22 2021/4/8
* @return: byte[]
* @Version 1.0
* @Description AES加密
**/
public static byte[] AESJDKEncode(String message, String Key) {
try {
KeyGenerator keyGeneratorEncode = KeyGenerator.getInstance("AES");
keyGeneratorEncode.init(256, new SecureRandom(Key.getBytes(StandardCharsets.UTF_8)));
SecretKey secretKey = keyGeneratorEncode.generateKey();
byte[] encode = secretKey.getEncoded();
SecretKeySpec key = new SecretKeySpec(encode, "AES");
//创建密码器
Cipher cipher = Cipher.getInstance("AES");
cipher.init(Cipher.ENCRYPT_MODE, key);
byte[] result = cipher.doFinal(message.getBytes(StandardCharsets.UTF_8));
return result;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
解密
/**
* @Author zhoucheng
* @MethodName AESJDKDecode
* @Param [message, Key]
* @Date 18:22 2021/4/8
* @return: byte[]
* @Version 1.0
* @Description AES解密
**/
public static byte[] AESJDKDecode(byte[] message, String Key) {
try {
KeyGenerator keyGeneratorDecode = KeyGenerator.getInstance("AES");
keyGeneratorDecode.init(256, new SecureRandom(Key.getBytes(StandardCharsets.UTF_8)));
SecretKey secretKey = keyGeneratorDecode.generateKey();
byte[] decode = secretKey.getEncoded();
SecretKeySpec key = new SecretKeySpec(decode, "AES");
//创建密码器
Cipher cipher = Cipher.getInstance("AES");
cipher.init(Cipher.DECRYPT_MODE, key);
byte[] result = cipher.doFinal(message);
return result;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
byte数组转换成字符串输出
/**
* @Author zhoucheng
* @MethodName convertByteToHexString
* @Param [bytes]
* @Date 19:11 2021/4/8
* @return: java.lang.String
* @Version 1.0
* @Description 将byte数组转化为16进制字符串
**/
public static String convertByteToHexString(byte[] bytes) {
String result = "";
for (int i = 0; i < bytes.length; i++) {
int temp = bytes[i] & 0xff;
String tempHex = Integer.toHexString(temp);
if (tempHex.length() < 2) {
result += "0" + tempHex;
}else {
result += tempHex;
}
}
return result;
}
一些加密方式的自我简单实现。
标签:AES,return,String,解密,Java,result,加密,byte 来源: https://blog.csdn.net/calm_encode/article/details/115613945
本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享; 2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关; 3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关; 4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除; 5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。