常见的加密的方式

编程入门 行业动态 更新时间:2024-10-18 10:16:49

<a href=https://www.elefans.com/category/jswz/34/1770088.html style=常见的加密的方式"/>

常见的加密的方式

常见的加密的方式

MD5加密

概念

MD5信息摘要算法(英语:MD5 Message-Digest Algorithm),一种被广泛使用的密码散列函数,可以产生出一个128位(16字节)的散列值(hash value),用于确保信息传输完整一致。MD5由美国密码学家罗纳德·李维斯特(Ronald Linn Rivest)设计,于1992年公开,用以取代MD4算法。这套算法的程序在 RFC 1321 标准中被加以规范。1996年后该算法被证实存在弱点,可以被加以破解,对于需要高度安全性的数据,专家一般建议改用其他算法,如SHA-2。2004年,证实MD5算法无法防止碰撞(collision),因此不适用于安全性认证,如SSL公开密钥认证或是数字签名等用途。

核心代码

package com.inspur.hismon;import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;public class MD5Utils {/*** 对明文字符串进行md5加密* @param source 传入的字符串* @return 加密的结果*/public static String md5(String source) {String en_pass = "";//加密后的密码// 1.判断source是否有效if (source != null && source.length() > 0) {// 2.如果不是有效的字符串抛出异常try {// 3.获取MessageDigest对象String algorithm = "md5";MessageDigest messageDigest = MessageDigest.getInstance(algorithm);// 4.获取明文字符串对应的字节数组byte[] input = source.getBytes();// 5.执行加密byte[] output = messageDigest.digest(input);// 6.创建BigInteger对象int signum = 1;BigInteger bigInteger = new BigInteger(signum, output);// 7.按照16进制将bigInteger的值转换为字符串int radix = 16;en_pass = bigInteger.toString(radix).toUpperCase();} catch (NoSuchAlgorithmException e) {e.printStackTrace();}}return en_pass;}public static void main(String[] args) {String password="666666";String en_password = md5(password);System.out.println("en_pass:"+en_password);//123456:E10ADC3949BA59ABBE56E057F20F883E//666666:F379EAF3C831B04DE153469D1BEC345E}
}

DES加密

概念

DES算法为密码体制中的对称密码体制,又被称为美国数据加密标准,是1972年美国IBM公司研制的对称密码体制加密算法。 明文按64位进行分组,密钥长64位,密钥事实上是56位参与DES运算(第8、16、24、32、40、48、56、64位是校验位, 使得每个密钥都有奇数个1)分组后的明文组和56位的密钥按位替代或交换的方法形成密文组的加密方法。

DES算法具有极高安全性,除了用穷举搜索法对DES算法进行攻击外,还没有发现更有效的办法。而56位长的密钥的穷举空间为2^56,这意味着如果一台计算机的速度是每一秒钟检测一百万个密钥,则它搜索完全部密钥就需要将近2285年的时间,可见,这是难以实现的。然而,这并不等于说DES是不可破解的。而实际上,随着硬件技术和Internet的发展,其破解的可能性越来越大,而且,所需要的时间越来越少。使用经过特殊设计的硬件并行处理要几个小时。

核心代码

package com.inspur.hismon;import javax.crypto.Cipher;
import java.security.Key;public class EncrypDES {// 字符串默认键值private static String strDefaultKey = "inventec2020@#$%^&";// 加密工具private Cipher encryptCipher = null;// 解密工具private Cipher decryptCipher = null;/*** 默认构造方法,使用默认密钥*/public EncrypDES() throws Exception {this(strDefaultKey);}/*** 指定密钥构造方法** @param strKey*            指定的密钥* @throws Exception*/public EncrypDES(String strKey) throws Exception {// Security.addProvider(new com.sun.crypto.provider.SunJCE());Key key = getKey(strKey.getBytes());encryptCipher = Cipher.getInstance("DES");encryptCipher.init(Cipher.ENCRYPT_MODE, key);decryptCipher = Cipher.getInstance("DES");decryptCipher.init(Cipher.DECRYPT_MODE, key);}/*** 将byte数组转换为表示16进制值的字符串, 如:byte[]{8,18}转换为:0813,和public static byte[]** hexStr2ByteArr(String strIn) 互为可逆的转换过程** @param arrB*            需要转换的byte数组* @return 转换后的字符串* @throws Exception*             本方法不处理任何异常,所有异常全部抛出*/public static String byteArr2HexStr(byte[] arrB) throws Exception {int iLen = arrB.length;// 每个byte用2个字符才能表示,所以字符串的长度是数组长度的2倍StringBuffer sb = new StringBuffer(iLen * 2);for (int i = 0; i < iLen; i++) {int intTmp = arrB[i];// 把负数转换为正数while (intTmp < 0) {intTmp = intTmp + 256;}// 小于0F的数需要在前面补0if (intTmp < 16) {sb.append("0");}sb.append(Integer.toString(intTmp, 16));}return sb.toString();}/*** 将表示16进制值的字符串转换为byte数组,和public static String byteArr2HexStr(byte[] arrB)* 互为可逆的转换过程** @param strIn*            需要转换的字符串* @return 转换后的byte数组*/public static byte[] hexStr2ByteArr(String strIn) throws Exception {byte[] arrB = strIn.getBytes();int iLen = arrB.length;// 两个字符表示一个字节,所以字节数组长度是字符串长度除以2byte[] arrOut = new byte[iLen / 2];for (int i = 0; i < iLen; i = i + 2) {String strTmp = new String(arrB, i, 2);arrOut[i / 2] = (byte) Integer.parseInt(strTmp, 16);}return arrOut;}/**** 加密字节数组** @param arrB*            需加密的字节数组* @return 加密后的字节数组*/public byte[] encrypt(byte[] arrB) throws Exception {return encryptCipher.doFinal(arrB);}/*** 加密字符串** @param strIn*            需加密的字符串* @return 加密后的字符串*/public String encrypt(String strIn) throws Exception {return byteArr2HexStr(encrypt(strIn.getBytes()));}/*** 解密字节数组** @param arrB*            需解密的字节数组* @return 解密后的字节数组*/public byte[] decrypt(byte[] arrB) throws Exception {return decryptCipher.doFinal(arrB);}/*** 解密字符串** @param strIn*            需解密的字符串* @return 解密后的字符串*/public String decrypt(String strIn) throws Exception {return new String(decrypt(hexStr2ByteArr(strIn)));}/*** 从指定字符串生成密钥,密钥所需的字节数组长度为8位 不足8位时后面补0,超出8位只取前8位** @param arrBTmp*            构成该字符串的字节数组* @return 生成的密钥*/private Key getKey(byte[] arrBTmp) throws Exception {// 创建一个空的8位字节数组(默认值为0)byte[] arrB = new byte[8];// 将原始字节数组转换为8位for (int i = 0; i < arrBTmp.length && i < arrB.length; i++) {arrB[i] = arrBTmp[i];}// 生成密钥Key key = new javax.crypto.spec.SecretKeySpec(arrB, "DES");return key;}public static void main(String[] args) {try {String msg = "123456";EncrypDES des = new EncrypDES();// 使用默认密钥System.out.println("加密前的字符:" + msg);System.out.println("加密后的字符:" + des.encrypt(msg));System.out.println("解密后的字符:" + des.decrypt(des.encrypt(msg)));System.out.println("--------优美分隔符------");String msg2 = "666666";String key = "2020@#$2020";EncrypDES des2 = new EncrypDES(key);// 自定义密钥System.out.println("加密前的字符:" + msg2);System.out.println("加密后的字符:" + des2.encrypt(msg2));System.out.println("解密后的字符:" + des2.decrypt(des2.encrypt(msg2)));} catch (Exception e) {e.printStackTrace();}}}

更多推荐

常见的加密的方式

本文发布于:2024-02-05 08:23:42,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1673770.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:常见   方式

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!