首页 / JAVA / Java实现简单的加密,解密实例
Java实现简单的加密,解密实例
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了Java实现简单的加密,解密实例,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3184字,纯文字阅读大概需要5分钟。
内容图文
一 概述
此例子用于简单的加密解密操作
二 自定义规则的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;
}
一些加密方式的自我简单实现。
内容总结
以上是互联网集市为您收集整理的Java实现简单的加密,解密实例全部内容,希望文章能够帮你解决Java实现简单的加密,解密实例所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。