`
Franciswmf
  • 浏览: 516664 次
  • 性别: Icon_minigender_1
  • 来自: 上海
文章分类
社区版块
存档分类
最新评论
阅读更多
一、加密算法:

1.1 散列算法(单向散列,不可逆)
MD5: Message Digest Algorithm 5,信息-摘要算法
SHA: Secure Hash Algorithm,安全散列算法,诸如:SHA1 SHA256 SHA512
HMAC:Hash Message Authentication Code,散列消息鉴别码

1.2 对称加密(加密解密使用同一秘钥,速度快)
1.2.1 DES:
DES是一种基于56位密钥的对称算法,1976年被美国联邦政府的国家标准局确定为联邦资料处理标准(FIPS),随后在国际上广泛流传开来。现在DES已经不是一种安全的加密算法,已被公开破解,现在DES已经被高级加密标准(AES)所代替
1.2.2 3DES
3DES是DES的一种派生算法,主要提升了DES的一些实用所需的安全性。
1.2.3 AES
AES是现在对称加密算法中最流行的算法之一。

1.3 非对称加密(公钥加密,私钥解密,可以签名,更安全)
RSA
DSA
ECC

1.4 BASE64
严格来说Base64并不是一种加密/解密算法,而是一种编码方式。
Base64不生成密钥,通过Base64编码后的密文就可以直接“翻译”为明文,但是可以通过向明文中添加混淆字符来达到加密的效果。

二、加签算法:
SHA1WithRSA(是一个常用的RSA加签算法):用SHA算法进行签名,用RSA算法进行加密

参考博客:
https://blog.csdn.net/qq_35605213/article/details/80591869
http://www.cnblogs.com/KKatherine/p/4128444.html
https://www.cnblogs.com/tancky/p/6409823.html
https://blog.csdn.net/it_beecoder/article/details/71480770
https://blog.csdn.net/UtopiaOfArtoria/article/details/82429467
https://blog.csdn.net/u013151053/article/details/81949810
https://blog.csdn.net/poiuyppp/article/details/81145982
https://www.cnblogs.com/shuqi/p/java.html
https://www.2cto.com/kf/201804/738493.html
//
https://blog.csdn.net/lovelichao12/article/details/75007189
https://www.cnblogs.com/xyzq/p/6679400.html
https://blog.csdn.net/john2522/article/details/53365358
https://blog.csdn.net/u011518120/article/details/52184725
https://blog.csdn.net/cheng9981/article/details/52825554

示例demo:
import java.security.KeyFactory; import java.security.KeyPair; import java.security.KeyPairGenerator; import java.security.PrivateKey; import java.security.PublicKey; import java.security.interfaces.RSAPrivateKey; import java.security.interfaces.RSAPublicKey; import java.security.spec.PKCS8EncodedKeySpec; import java.security.spec.X509EncodedKeySpec; import java.util.HashMap; import java.util.Map; import javax.crypto.Cipher; import org.apache.commons.codec.binary.Base64; import org.apache.commons.codec.binary.Hex; /** * @description: RSA加密解密工具类 * @title: RSAUtils.java * @author: Administrator * @date: 2019年2月12日 * @version: 1.0.0 */ public class RSAUtils { /* 解释: RSA算法,需要两个密钥来进行加密和解密,分别是公钥和私钥。 需要注意的一点,这个公钥和私钥必须是一对的,如果用公钥对数据进行加密,那么只有使用对应的私钥才能解密,反之亦然。 由于加密和解密使用的是两个不同的密钥,因此,这种算法叫做非对称加密算法。 其算法具体实现基于一个十分简单的数论事实:将两个大素数相乘十分容易,但是想要对其乘积进行因式分解却极其困难,因此可以将乘积公开作为加密密钥。 */ /** * 生产RSA密钥对 * @return */ public static Map<String,Object> generateKey(){ try { //实例化一个密钥对生成器 KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("RSA"); // ‘1024’,表示生成的是512位字符 keyPairGenerator.initialize(1024); KeyPair keyPair = keyPairGenerator.generateKeyPair(); RSAPublicKey rsaPublicKey = (RSAPublicKey)keyPair.getPublic(); RSAPrivateKey rsaPrivateKey = (RSAPrivateKey) keyPair.getPrivate(); Map<String,Object> keyPairs = new HashMap<>(); keyPairs.put("publicKey",rsaPublicKey); keyPairs.put("privateKey",rsaPrivateKey); return keyPairs; } catch (Exception e) { e.printStackTrace(); } return null; } /** * RSA私钥加密 * @param message 要加密的信息 * @param rsaPrivateKey RSA私钥 * @return 加密后的字符串 */ public static String encrypt(String message, RSAPrivateKey rsaPrivateKey){ try { PKCS8EncodedKeySpec pkcs8EncodedKeySpec = new PKCS8EncodedKeySpec(rsaPrivateKey.getEncoded()); KeyFactory keyFactory = KeyFactory.getInstance("RSA"); PrivateKey privateKey = keyFactory.generatePrivate(pkcs8EncodedKeySpec); Cipher cipher = Cipher.getInstance("RSA"); cipher.init(Cipher.ENCRYPT_MODE, privateKey); byte[] resultBytes = cipher.doFinal(message.getBytes()); return Hex.encodeHexString(resultBytes); } catch (Exception e) { e.printStackTrace(); } return null; } /** * RSA公钥解密 * @param encryptMsg 要解密的信息 * @param rsaPublicKey RSA公钥 * @return 解密后的字符串 */ public static String decrypt(String encryptMsg, RSAPublicKey rsaPublicKey){ try { X509EncodedKeySpec x509EncodedKeySpec = new X509EncodedKeySpec(rsaPublicKey.getEncoded()); KeyFactory keyFactory = KeyFactory.getInstance("RSA"); PublicKey publicKey = keyFactory.generatePublic(x509EncodedKeySpec); Cipher cipher = Cipher.getInstance("RSA"); cipher.init(Cipher.DECRYPT_MODE, publicKey); byte[] resultBytes = cipher.doFinal(Hex.decodeHex(encryptMsg.toCharArray())); return new String(resultBytes); } catch (Exception e) { e.printStackTrace(); } return null; } /** * RSA公钥加密 * @param message * @param rsaPublicKey * @return */ public static String rsaPublicKeyEncode(String message,RSAPublicKey rsaPublicKey){ try { Cipher cip = Cipher.getInstance("RSA"); cip.init(cip.ENCRYPT_MODE, rsaPublicKey); byte[] by = cip.doFinal(message.getBytes()); return Base64.encodeBase64String(by); } catch (Exception e) { throw new RuntimeException(e); } } /** * RSA私钥解密 * @param encryptMsg * @param rsaPrivateKey * @return */ public static String rsaPrivateKeyDecode(String encryptMsg,RSAPrivateKey rsaPrivateKey){ try { Cipher cip = Cipher.getInstance("RSA"); cip.init(cip.DECRYPT_MODE, rsaPrivateKey); byte[] by = Base64.decodeBase64(encryptMsg.getBytes()); return new String(cip.doFinal(by)); } catch (Exception e) { throw new RuntimeException(e); } } /** * RSA公钥字符串转公钥 * @param publicKeyStr */ public static RSAPublicKey getPublicKey(String publicKeyStr){ byte[] keyBytes=null; try { // keyBytes = (new BASE64Decoder()).decodeBuffer(publicKeyStr);//需引入jdk1.8 keyBytes=Base64.decodeBase64(publicKeyStr); X509EncodedKeySpec keySpec = new X509EncodedKeySpec(keyBytes); KeyFactory keyFactory = KeyFactory.getInstance("RSA"); RSAPublicKey publicKey = (RSAPublicKey) keyFactory.generatePublic(keySpec); return publicKey; } catch (Exception e) { throw new RuntimeException(e); } } /** * RSA私钥字符串转私钥 * @param privateKeyStr */ public static RSAPrivateKey getPrivateKey(String privateKeyStr){ byte[] keyBytes=null; try { // keyBytes = (new BASE64Decoder()).decodeBuffer(privateKeyStr);//需引入jdk1.8 keyBytes=Base64.decodeBase64(privateKeyStr); PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(keyBytes); KeyFactory keyFactory = KeyFactory.getInstance("RSA"); RSAPrivateKey privateKey = (RSAPrivateKey) keyFactory.generatePrivate(keySpec); return privateKey; } catch (Exception e) { throw new RuntimeException(e); } } /** * 测试 * @param args */ public static void main(String[] args) { Map<String, Object> map=RSAUtils.generateKey(); RSAPublicKey rsaPublicKey=(RSAPublicKey) map.get("publicKey"); RSAPrivateKey rsaPrivateKey=(RSAPrivateKey) map.get("privateKey"); //公钥 String publicKey=Base64.encodeBase64String(rsaPublicKey.getEncoded()); //私钥 String privateKey=Base64.encodeBase64String(rsaPrivateKey.getEncoded()); System.out.println("=========================生成RSA密钥对======================="); System.out.println("公钥:"+publicKey); System.out.println("私钥:"+privateKey); System.out.println("=========================示例1======================="); //【示例1】 String message="123456javaHELLO#$%_"; //rsa私钥加密 String encryptMessage=RSAUtils.encrypt(message, rsaPrivateKey); System.out.println("私钥加密后encryptMessage="+encryptMessage); //rsa公钥解密 String decryptMessage=RSAUtils.decrypt(encryptMessage, rsaPublicKey); System.out.println("公钥解密后decryptMessage="+decryptMessage); System.out.println("=========================示例2======================="); //【示例2】 String msg="123456javaHELLO#$%_中国"; //rsa公钥加密 String encryptMsg=RSAUtils.rsaPublicKeyEncode(msg, rsaPublicKey); System.out.println("公钥加密后encryptMsg="+encryptMsg); //rsa私钥解密 String decryptMsg=RSAUtils.rsaPrivateKeyDecode(encryptMsg, rsaPrivateKey); System.out.println("私钥解密后decryptMsg="+decryptMsg); System.out.println("==========================示例3======================"); //【示例3】 //前端js公钥加密后的字符串(注意"+"号的处理) //encodeURI(data).replace(/\+/g, '%2B'),后端处理时要将%2B转换回来 String encryptMsg2="giBQsrwlpkoRYoSkwzvHjkkiEES0Qu2CmanWDPedYwCBfSuUzU2zwEiergH5G+YzR53ygLDBs174lgvzoo8nbac/VbV5wPmrY7Pm9pTFeJR3uZBKvMvO23VxKzLUEPv6FvYif/0PYeUwS9AylRqONTbkNV6PV6E052rSaZlcUOE="; //后端保存的私钥 String privateKeyStr="MIICdgIBADANBgkqhkiG9w0BAQEFAASCAmAwggJcAgEAAoGBAN1tjHRYRk5q8qxx+v+PibdNNh7lrHjPyo+zC6IOG7XG1o1Yd/vZVhtqaGIICCjs3w2sSQuD87sJSKkO+apnOnGhFNgA2lJVyZqZ0Hp8WZxT0EB/CYNec0iUQ9AWFnDujcGcL/qlCrgFDpLLMWiv1l4A3oV/prqiFRTunFxVhVGbAgMBAAECgYBjVvR+deQE5mI9D13GLcVhIRi4v92izcZYtcDwUVgJM02qWBhsOtMwtYpjAKt4gNyJK9QRgH9mWAHrJrbHxkwnPKKiso+qNNSb9Dbjbua25AS9HP2VQwkQUWBGagFvLQGatddK4f5nh7137+Jmld7VbfpY59BQvqS7lmzHfEmPQQJBAPR6hzNLHjnDZZ9wL092Trk02CN0aXOQsItHpypCiCzF7gmi2xPOGG8K4aOp5w/nIO1rr8eyl/5agvYXE51LsqsCQQDn3O3dZxw/jwUrhyWZJemkd2vBe1gUT0dnzEGhAIfLDinGiAg9NEBiMyFFHMXvyCIhOY2fxc9OZ+M2oeVLiVzRAkEAl7rb4ifC0JLGpVdY5XZFkYiMhCShtcmc6DRGOWIlZbRJ8c0TLo2AeJpGQ+8UqrgFpZRp+gSLdJ70HIth7wOmFQJAcgTu5AWkmozWWgVns9w0/S/MyaTCN5qU3rJPQ8FjBbO9T2ftxUtArgm+vqHbpIKiZfxLbNp1i3UDAwayH2c5QQJAf9OhvMVT/K9FsGzRHlIPyfUPFa9zKRCTw7RGKbrhoromycqOD23HV8EwCBvXUrAtqOC7TxxcX6wdQgq/zJZ7EA=="; //密钥转换 RSAPrivateKey rsaPrivateKey2=RSAUtils.getPrivateKey(privateKeyStr); //私钥解密 String decryptMsg2=RSAUtils.rsaPrivateKeyDecode(encryptMsg2, rsaPrivateKey2); System.out.println("前端js公钥加密,后端私钥解密后decryptMsg2="+decryptMsg2); } }
分享到:
评论
相关资源推荐
Global site tag (gtag.js) - Google Analytics