MD5和3DES加密方式浅析

发布时间:2020-07-28 12:39:46 作者:曼曼曼鳗鱼
来源:网络 阅读:909

    这几天在做字段加密的内容。所以就把这部分东西简单的了解一下。

1、首先,加密分对称加密及不对称加密。

    对称加密:在消息发送前使用密钥对消息进行加密,在对方收到消息后,使用相同的密钥进行解密。

    非对称加密:加密和解密使用不同的密钥。通常有密钥A和B,使用A加密得到的密文只有B可以解密(A自身也不可解)。即为私钥和公钥。顾名思义,私钥加密,公钥解密。

    典型的对称加密是DES算法,典型的非对称加密是RSA算法。


2、这次使用了MD5和3DES,还是先对这个做一个归纳。

MD5的全称是Message-Digest Algorithm 5,在90年代初由MIT的计算机科学实验室和RSA Data Security Inc发明,经MD2、MD3和MD4发展而来。Message-Digest,意思为报文摘要,对不定长的报文做出定长的“报文摘要”。

附代码:    

import java.security.MessageDigest;

public static final String encode(String s) {
        char hexDigits[] = {
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
'A', 'B', 'C', 'D', 'E', 'F' };
try {
byte[] btInput = s.getBytes();
// 获得MD5摘要算法的 MessageDigest 对象
MessageDigest mdInst = MessageDigest.getInstance("MD5");
// 使用指定的字节更新摘要
mdInst.update(btInput);
// 获得密文
byte[] md = mdInst.digest();
// 把密文转换成十六进制的字符串形式
int j = md.length;
char str[] = new char[j * 2];
int k = 0;
for (int i = 0; i < j; i++) {
byte byte0 = md[i];
str[k++] = hexDigits[byte0 >>> 4 & 0xf];
str[k++] = hexDigits[byte0 & 0xf];
}
return new String(str);
} catch (Exception e) {
e.printStackTrace();
return null;
}
}

注意:一定要在加密前,将需要加密的字符串转为字节数组。

DES算法:是使用一个56位的密钥以及附加的8位奇偶校验位(每组的第8位作为奇偶校验位),产生最大64位的分组大小。这是一个迭代的分组密码,使用称为Feistel的技术,其中将加密的文本块分为两半,使用子密钥对其中一半应用循环功能,然后将输出与另一半进行“异或”运算;接着交换这两半,这一过程会继续下去。但最后一个循环不交换。DES使用16轮循环,使用异或、置换、代换、移位操作四种基本运算。

而3DES就是对数据块进行三次DES算法加密来进行加固。

代码如下:

public static String COMMON_KEY = "843ce";//密钥
public static byte[] desEncrypt(String msg, String salt) {
if (msg == null)
msg = "";
if (salt == null) {
salt = COMMON_KEY;
}
byte[] keyBytes = new byte[8];
int saltLen = salt.length();
byte[] saltBytes = salt.getBytes();//转换成字节数组,这是加密的必要步骤!
for (int i = 0; i < 8; i++) {
keyBytes[i] = saltBytes[i % saltLen];
}
try {
DESKeySpec keySpec = new DESKeySpec(keyBytes);
SecretKey key = SecretKeyFactory.getInstance("DES").generateSecret(keySpec);
Cipher desCipher = Cipher.getInstance("DES/ECB/PKCS5Padding");
desCipher.init(Cipher.ENCRYPT_MODE, key);
byte[] text = msg.getBytes("UTF-8");
byte[] ciphertext = desCipher.doFinal(text);
return ciphertext;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
public static String desDecrypt(byte[] msg, String salt) {
if (msg == null)
return null;
if (salt == null) {
salt = COMMON_KEY;
}
byte[] keyBytes = new byte[8];
int saltLen = salt.length();
byte[] saltBytes = salt.getBytes();
for (int i = 0; i < 8; i++) {
keyBytes[i] = saltBytes[i % saltLen];
}
try {
DESKeySpec keySpec = new DESKeySpec(keyBytes);
SecretKey key = SecretKeyFactory.getInstance("DES").generateSecret(keySpec);
Cipher desCipher = Cipher.getInstance("DES/ECB/PKCS5Padding");
desCipher.init(Cipher.DECRYPT_MODE, key);
byte[] deciphertext = desCipher.doFinal(msg);
return new String(deciphertext, "UTF-8");
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
public static String dumpBytes(byte[] bytes) {
StringBuffer sb = new StringBuffer();
for (int i = 0; i < bytes.length; i++) {
if (i % 32 == 0 && i != 0) {
sb.append("\n");
}
String s = Integer.toHexString(bytes[i]);
if (s.length() < 2) {
s = "0" + s;
}
if (s.length() > 2) {
s = s.substring(s.length() - 2);
}
sb.append(s);
}
return sb.toString();
}
public static byte[] parseBytes(String str) {
try {
int len = str.length() / 2;
if (len <= 2) {
return new byte[] { Byte.parseByte(str) };
}
byte[] arr = new byte[len];
for (int i = 0; i < arr.length; i++) {
arr[i] = (byte) Integer.parseInt(
str.substring(i * 2, i * 2 + 2), 16);
}
return arr;
} catch (Exception e) {
return new byte[0];
}
}
/**
 * 加密
 * 
 * @param encrypt_value
 *            被加密的字符串
 * @param encrypt_key
 *            加密的密钥
 * @return
 */
public static String encryptAsString(String encrypt_value,
String encrypt_key) {
return dumpBytes(desEncrypt(encrypt_value, encrypt_key));
}
/**
 * 解密
 * 
 * @param encrypt_value
 *            要解密的字符串
 * @param encrypt_key
 *            密钥
 * @return
 */
public static String desEncryptAsString(String encrypt_value,
String encrypt_key) {
return desDecrypt(parseBytes(encrypt_value), encrypt_key);
}



推荐阅读:
  1. PHP比md5更安全的加密方式--哈希密码
  2. 动画浅析-CAAnimation和CATransition

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

加密 %d

上一篇:Devart数据库管理系列产品教程资源合集

下一篇:Leetcode-43 划水记录06 大数乘法

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》