Java各种加密方式集锦(AES,DES,RSA,DSA,MD5,SHA)
一. 什么是加密
加密,是以某种特殊的算法改变原有的信息数据,使得未授权的用户即使获得了已加密的信息,但因不知解密的方法,仍然无法了解信息的内容。在网络数据传输过程中会经常用到报文加密,通常是对报文体body信息或者某些参数进行加密。大体上分为双向加密和单向加密,而双向加密又分为对称加密和非对称加密.
二.加密方式有哪些,具体怎么加密
(一).双向加密
1 .对称秘钥加密
对称秘钥加密,就是采用这种加密方法的双方使用方式用同样的**进行加密和解密。**是控制加密及解密过程的指令。算法是一组规则,规定如何进行加密和解密
常用的对称加密有:DES、IDEA、RC2、RC4、SKIPJACK、RC5、AES算法等
(1). DES加密(秘钥key至少8位)
DES算法为密码体制中的对称密码体制,又被成为美国数据加密标准,是1972年美国IBM公司研制的对称密码体制加密算法。 明文按64位进行分组, **长64位,**事实上是56位参与DES运算(第8、16、24、32、40、48、56、64位是校验位, 使得每个**都有奇数个1)分组后的明文组和56位的**按位替代或交换的方法形成密文组的加密方法。
以下加密方式秘钥长度需要大于等于8
import net.minidev.json.JSONObject; import org.apache.commons.codec.binary.Base64; import org.apache.commons.lang3.StringUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import javax.crypto.*; import javax.crypto.spec.DESKeySpec; import java.io.UnsupportedEncodingException; import java.security.SecureRandom; public class DESEncryptUtil { private static Logger LOGGER = LoggerFactory.getLogger(DESEncryptUtil.class); //加密 public static String encrypt(String data,String key){ if(StringUtils.isBlank(data)||StringUtils.isBlank(key)){ LOGGER.info("参数和**不允许为空"); return null; } String strs = null; try { byte[] bytes = encryptOrDecrypt(Cipher.ENCRYPT_MODE,data.getBytes(),key.getBytes()); // base64编码字节 strs = new String(new Base64().encode(bytes),"utf-8"); } catch (UnsupportedEncodingException e) { LOGGER.error("加密失败,errormsg={}",e.getMessage()); } return strs; } //解密 public static String decrypt(String data,String key){ if(StringUtils.isBlank(data)||StringUtils.isBlank(key)){ LOGGER.info("参数和**不允许为空"); return null; } String strs = null; try { byte[] src = new Base64().decode(data); byte[] bytes = encryptOrDecrypt(Cipher.DECRYPT_MODE,src,key.getBytes()); strs = new String(bytes,"utf-8"); } catch (UnsupportedEncodingException e) { LOGGER.error("解密失败,errormsg={}",e.getMessage()); } return strs; } public static byte[] encryptOrDecrypt(int mode,byte[] data,byte[] key){ try{ // 强随机数生成器 (RNG) SecureRandom random = new SecureRandom(); // DESKeySpec是一个成加***的**内容的(透明)规范的接口。 DESKeySpec desKey = new DESKeySpec(key); // 创建一个密匙工厂,然后用它把DESKeySpec转换成 SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES"); // 得到**对象SecretKey SecretKey securekey = keyFactory.generateSecret(desKey); // Cipher对象实际完成加密操作 Cipher cipher = Cipher.getInstance("DES"); // 用密匙初始化Cipher对象 cipher.init(mode, securekey, random); // 现在,获取数据并加密,正式执行加密操作 return cipher.doFinal(data); }catch(Throwable e){ e.printStackTrace(); } return null; } public static void main(String[] args) { JSONObject json = new JSONObject(); json.put("首联","一叶扁舟伴水流"); String data = encrypt(json.toJSONString(),"12345678"); System.out.println("明文是:"+json); System.out.println("加密后:"+data); System.out.println("解密后:"+decrypt(data,"12345678")); } }
运行结果是:
(2)AES加密(秘钥key至少8位)
AES密码学中的高级加密标准(Advanced Encryption Standard,AES),又称 高级加密标准
Rijndael加密法,是美国联邦政府采用的一种区块加密标准。这个标准用来替代原先的DES,已经被多方分析且广为全世界所使用。经过五年的甄选流程,高级加密标准由美国国家标准与技术研究院(NIST)于2001年11月26日发布于FIPS PUB 197,并在2002年5月26日成为有效的标准。2006年,高级加密标准已然成为对称**加密中最流行的算法之一。该算法为比利时密码学家Joan Daemen和Vincent Rijmen所设计,结合两位作者的名字,以Rijndael之命名之,投稿高级加密标准的甄选流程。(Rijdael的发音近于 "Rhinedoll"。)
import net.minidev.json.JSONObject; import org.apache.commons.codec.binary.Base64; import org.apache.commons.lang3.StringUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import javax.crypto.Cipher; import javax.crypto.KeyGenerator; import javax.crypto.SecretKey; import javax.crypto.spec.SecretKeySpec; import java.io.UnsupportedEncodingException; import java.security.SecureRandom; public class AESUtils { private static Logger LOGGER = LoggerFactory.getLogger(AESUtils.class); public static String encrypt(String data,String key){ if(StringUtils.isBlank(data)||StringUtils.isBlank(key)){ LOGGER.info("参数和**不允许为空"); return null; } String strs = null; try { byte[] bytes = doAES(Cipher.ENCRYPT_MODE,data.getBytes(),key.getBytes()); // base64编码字节 strs = new String(new Base64().encode(bytes),"utf-8"); } catch (UnsupportedEncodingException e) { LOGGER.error("加密失败,errormsg={}",e.getMessage()); } return strs; } public static String decrypt(String data,String key){ if(StringUtils.isBlank(data)||StringUtils.isBlank(key)){ LOGGER.info("参数和**不允许为空"); return null; } String strs = null; try { byte[] src = new Base64().decode(data); byte[] bytes = doAES(Cipher.DECRYPT_MODE,src,key.getBytes()); strs = new String(bytes,"utf-8"); } catch (UnsupportedEncodingException e) { LOGGER.error("解密失败,errormsg={}",e.getMessage()); } return strs; } public static byte[] doAES(int mode,byte[] data,byte[] key){ try{ //1.构造**生成器,指定为AES算法,不区分大小写 KeyGenerator kgen = KeyGenerator.getInstance("AES"); //2.根据ecnodeRules规则初始化**生成器 //生成一个128位的随机源,根据传入的字节数组 kgen.init(128, new SecureRandom(key)); //3.产生原始对称** SecretKey secretKey = kgen.generateKey(); //4.获得原始对称**的字节数组 byte[] enCodeFormat = secretKey.getEncoded(); //5.根据字节数组生成AES** SecretKeySpec keySpec = new SecretKeySpec(enCodeFormat,"AES"); //6.根据指定算法AES自成密码器 Cipher cipher = Cipher.getInstance("AES");// 创建密码器 //7.初始化密码器,第一个参数为加密(Encrypt_mode)或者解密解密(Decrypt_mode)操作,第二个参数为使用的KEY cipher.init(mode, keySpec);// 初始化 return cipher.doFinal(data); }catch (Exception e){ LOGGER.error("加解密失败,errormsg={}",e.getMessage()); } return null; } public static void main(String[] args) { JSONObject json = new JSONObject(); json.put("首联","一叶扁舟伴水流"); String data = encrypt(json.toJSONString(),"123456789"); System.out.println("明文是:"+json); System.out.println("加密后:"+data); System.out.println("解密后:"+decrypt(data,"123456789")); } }
2 .非对称秘钥加密
1976年,美国学者Dime和Henman为解决信息公开传送和**管理问题,提出一种新的**交换协议,允许在不安全的媒体上的通讯双方交换信息,安全地达成一致的**,这就是“公开**系统”。相对于“对称加密算法”这种方法也叫做“非对称加密算法”。 与对称加密算法不同,非对称加密算法需要两个**:公开**(publickey)和私有** (privatekey)。公开**与私有**是一对,如果用公开**对数据进行加密,只有用对应的私有**才能解密;如果用私有**对数据进行加密,那么只有用对应的公开**才能解密。因为加密和解密使用的是两个不同的**,所以这种算法叫作非对称加密算法。
(1). RSA 公钥加密算法(1024位key的最多只能加密127位数据)
RSA 公钥加密算法是1977年由Ron Rivest、Adi Shamirh和LenAdleman在(美国麻省理工学院)开发的。RSA取名来自开发他们三者的名字。RSA是目前最有影响力的公钥加密算法,它能够抵抗到目前为止已知的所有密码攻击,已被ISO推荐为公钥数据加密标准。RSA算法基于一个十分简单的数论事实:将两个大素数相乘十分容易,但那时想要对其乘积进行因式分解却极其困难,因此可以将乘积公开作为加***。
import net.minidev.json.JSONObject; import javax.crypto.BadPaddingException; import javax.crypto.Cipher; import javax.crypto.IllegalBlockSizeException; import javax.crypto.NoSuchPaddingException; import java.security.InvalidKeyException; import java.security.KeyPair; import java.security.KeyPairGenerator; import java.security.NoSuchAlgorithmException; import java.security.interfaces.RSAPrivateKey; import java.security.interfaces.RSAPublicKey; public class RSA { /** * 加密 * @param publicKey * @param srcBytes * @return * @throws NoSuchAlgorithmException * @throws NoSuchPaddingException * @throws InvalidKeyException * @throws IllegalBlockSizeException * @throws BadPaddingException */ public static byte[] encrypt(RSAPublicKey publicKey, byte[] srcBytes) throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, IllegalBlockSizeException, BadPaddingException { if(publicKey!=null){ //Cipher负责完成加密或解密工作,基于RSA Cipher cipher = Cipher.getInstance("RSA"); //根据公钥,对Cipher对象进行初始化 cipher.init(Cipher.ENCRYPT_MODE, publicKey); byte[] resultBytes = cipher.doFinal(srcBytes); return resultBytes; } return null; } /** * 解密 * @param privateKey * @param srcBytes * @return * @throws NoSuchAlgorithmException * @throws NoSuchPaddingException * @throws InvalidKeyException * @throws IllegalBlockSizeException * @throws BadPaddingException */ public static byte[] decrypt(RSAPrivateKey privateKey, byte[] srcBytes) throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, IllegalBlockSizeException, BadPaddingException{ if(privateKey!=null){ //Cipher负责完成加密或解密工作,基于RSA Cipher cipher = Cipher.getInstance("RSA"); //根据公钥,对Cipher对象进行初始化 cipher.init(Cipher.DECRYPT_MODE, privateKey); byte[] resultBytes = cipher.doFinal(srcBytes); return resultBytes; } return null; } /** * @param args * @throws NoSuchAlgorithmException * @throws BadPaddingException * @throws IllegalBlockSizeException * @throws NoSuchPaddingException * @throws InvalidKeyException */ public static void main(String[] args) throws NoSuchAlgorithmException, InvalidKeyException, NoSuchPaddingException, IllegalBlockSizeException, BadPaddingException { JSONObject json = new JSONObject(); json.put("首联","一叶扁舟伴水流"); //KeyPairGenerator类用于生成公钥和私钥对,基于RSA算法生成对象 KeyPairGenerator keyPairGen = KeyPairGenerator.getInstance("RSA"); //初始化**对生成器,**大小为1024位 keyPairGen.initialize(1024); //生成一个**对,保存在keyPair中 KeyPair keyPair = keyPairGen.generateKeyPair(); //得到私钥 RSAPrivateKey privateKey = (RSAPrivateKey)keyPair.getPrivate(); //得到公钥 RSAPublicKey publicKey = (RSAPublicKey)keyPair.getPublic(); //用公钥加密 byte[] srcBytes = json.toJSONString().getBytes(); byte[] resultBytes = encrypt(publicKey, srcBytes); //用私钥解密 byte[] decBytes = decrypt(privateKey, resultBytes); System.out.println("明文是:" + json.toJSONString()); System.out.println("加密后是:" + new String(resultBytes)); System.out.println("解密后是:" + new String(decBytes)); } }
控制台输出结果:
(2). DSA加密算法
Digital Signature Algorithm (DSA)是Schnorr和ElGamal签名算法的变种,被美国NIST作为DSS(DigitalSignature Standard)。
import net.minidev.json.JSONObject; import sun.misc.BASE64Decoder; import sun.misc.BASE64Encoder; import java.security.*; import java.security.spec.PKCS8EncodedKeySpec; import java.security.spec.X509EncodedKeySpec; import java.util.HashMap; import java.util.Map; public class DSAUtils { public static final String KEY_ALGORITHM = "DSA"; public static final String SIGNATURE_ALGORITHM = "DSA"; public static final String DEFAULT_SEED = "$%^*%^()(HJG8awfjas7"; //默认种子 public static final String PUBLIC_KEY = "DSAPublicKey"; public static final String PRIVATE_KEY = "DSAPrivateKey"; public static void main(String[] args) throws Exception { JSONObject json = new JSONObject(); json.put("首联","一叶扁舟伴水流"); String str = json.toJSONString(); byte[] data = str.getBytes(); Map<String, Object> keyMap = initKey();// 构建** PublicKey publicKey = (PublicKey) keyMap.get(PUBLIC_KEY); PrivateKey privateKey = (PrivateKey) keyMap.get(PRIVATE_KEY); System.out.println("私钥format:" + privateKey.getFormat()); System.out.println("公钥format:" + publicKey.getFormat()); // 产生签名 String sign = sign(data, getPrivateKey(keyMap)); System.out.println(sign); // 验证签名 boolean verify1 = verify("aaa".getBytes(), getPublicKey(keyMap), sign); System.err.println("经验证 数据和签名匹配:" + verify1); boolean verify = verify(data, getPublicKey(keyMap), sign); System.err.println("经验证 数据和签名匹配:" + verify); } /** * 生成** * * @param seed 种子 * @return **对象 * @throws Exception */ public static Map<String, Object> initKey(String seed) throws Exception { System.out.println("生成**"); KeyPairGenerator ****** = KeyPairGenerator.getInstance(KEY_ALGORITHM); SecureRandom secureRandom = new SecureRandom(); secureRandom.setSeed(seed.getBytes()); //Modulus size must range from 512 to 1024 and be a multiple of 64 ******.initialize(640, secureRandom); KeyPair keys = ******.genKeyPair(); PrivateKey privateKey = keys.getPrivate(); PublicKey publicKey = keys.getPublic(); Map<String, Object> map = new HashMap<String, Object>(2); map.put(PUBLIC_KEY, publicKey); map.put(PRIVATE_KEY, privateKey); return map; } /** * 生成默认** * * @return **对象 * @throws Exception */ public static Map<String, Object> initKey() throws Exception { return initKey(DEFAULT_SEED); } /** * 取得私钥 * * @param keyMap * @return * @throws Exception */ public static String getPrivateKey(Map<String, Object> keyMap) throws Exception { Key key = (Key) keyMap.get(PRIVATE_KEY); return encryptBASE64(key.getEncoded()); //base64加密私钥 } /** * 取得公钥 * * @param keyMap * @return * @throws Exception */ public static String getPublicKey(Map<String, Object> keyMap) throws Exception { Key key = (Key) keyMap.get(PUBLIC_KEY); return encryptBASE64(key.getEncoded()); //base64加密公钥 } /** * 用私钥对信息进行数字签名 * @param data 加密数据 * @param privateKey 私钥-base64加密的 * @return * @throws Exception */ public static String sign(byte[] data, String privateKey) throws Exception { System.out.println("用私钥对信息进行数字签名"); byte[] keyBytes = decryptBASE64(privateKey); PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(keyBytes); KeyFactory factory = KeyFactory.getInstance(KEY_ALGORITHM); PrivateKey priKey = factory.generatePrivate(keySpec);//生成 私钥 //用私钥对信息进行数字签名 Signature signature = Signature.getInstance(SIGNATURE_ALGORITHM); signature.initSign(priKey); signature.update(data); return encryptBASE64(signature.sign()); } /** * BASE64Encoder 加密 * @param data 要加密的数据 * @return 加密后的字符串 */ private static String encryptBASE64(byte[] data) { BASE64Encoder encoder = new BASE64Encoder(); String encode = encoder.encode(data); return encode; } /** * BASE64Decoder 解密 * @param data 要解密的字符串 * @return 解密后的byte[] * @throws Exception */ private static byte[] decryptBASE64(String data) throws Exception { BASE64Decoder decoder = new BASE64Decoder(); byte[] buffer = decoder.decodeBuffer(data); return buffer; } /** * 校验数字签名 * @param data 加密数据 * @param publicKey * @param sign 数字签名 * @return * @throws Exception */ public static boolean verify(byte[] data, String publicKey, String sign) throws Exception { byte[] keyBytes = decryptBASE64(publicKey); X509EncodedKeySpec keySpec = new X509EncodedKeySpec(keyBytes); KeyFactory keyFactory = KeyFactory.getInstance(KEY_ALGORITHM); PublicKey pubKey = keyFactory.generatePublic(keySpec); Signature signature = Signature.getInstance(SIGNATURE_ALGORITHM); signature.initVerify(pubKey); signature.update(data); return signature.verify(decryptBASE64(sign)); //验证签名 } }
控制台输出结果:
(二).单向加密
1.MD5加密
MD5 即Message-Digest Algorithm 5(信息-摘要算法 5),用于确保信息传输完整一致。是计算机广泛使用的杂凑算法之一(又译摘要算法、哈希算法),主流编程语言普遍已有MD5实现。将数据(如汉字)运算为另一固定长度值,是杂凑算法的基础原理,MD5的前身有MD2、MD3和MD4。MD5的作用是让大容量信息在用数字签名软件签署私人**前被"压缩"成一种保密的格式(就是把一个任意长度的字节串变换成一定长的十六进制数字串)。
除了MD5以外,其中比较有名的还有sha-1、RIPEMD以及Haval等
import net.minidev.json.JSONObject; import java.io.UnsupportedEncodingException; import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; public class MD5Utils { public static byte[] encrypt(String data) throws NoSuchAlgorithmException { // 根据MD5算法生成MessageDigest对象 MessageDigest md5 = MessageDigest.getInstance("MD5"); byte[] src = data.getBytes(); // 使用src更新摘要 md5.update(src); // 完成哈希计算,得到result return md5.digest(); } public static void main(String[] args) throws NoSuchAlgorithmException, UnsupportedEncodingException { JSONObject json = new JSONObject(); json.put("首联","一叶扁舟伴水流"); System.out.println("明文是:"+json); byte[] result = encrypt(json.toJSONString()); System.out.println("密文是:"+new String(result,"utf-8")); } }
2.SHA加密
SHA 是一种数据加密算法,该算法经过加密专家多年来的发展和改进已日益完善,现在已成为公认的最安全的散列算法之一,并被广泛使用。该算法的思想是接收一段明文,然后以一种不可逆的方式将它转换成一段(通常更小)密文,也可以简单的理解为取一串输入码(称为预映射或信息),并把它们转化为长度较短、位数固定的输出序列即散列值(也称为信息摘要或信息认证代码)的过程。散列函数值可以说时对明文的一种“指纹”或是“摘要”所以对散列值的数字签名就可以视为对此明文的数字签名。
import net.minidev.json.JSONObject; import java.io.UnsupportedEncodingException; import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; public class SHAUtils { public static byte[] encrypt(String data) throws NoSuchAlgorithmException { // 根据SHA算法生成MessageDigest对象 MessageDigest md5 = MessageDigest.getInstance("SHA"); byte[] src = data.getBytes(); // 使用src更新摘要 md5.update(src); // 完成哈希计算,得到result return md5.digest(); } public static void main(String[] args) throws NoSuchAlgorithmException, UnsupportedEncodingException { JSONObject json = new JSONObject(); json.put("首联","一叶扁舟伴水流"); System.out.println("明文是:"+json); byte[] result = encrypt(json.toJSONString()); System.out.println("密文是:"+new String(result,"utf-8")); }
控制台输出结果:
参考资料:
https://blog.****.net/MJM_49/article/details/77429883
https://blog.****.net/jjwwmlp456/article/details/21016177