基础篇:java.security框架之签名、加密、摘要及证书

本文介绍了Java.security框架,涵盖加密算法概念、秘钥生成、摘要算法(如MessageDigest)、签名工具(Signature)、加密工具类(Cipher)、证书存储、KeyStore以及HTTPS证书加载。讲解了对称加密、非对称加密、单向加密的区别,以及各种加密算法和密钥转换的使用示例。
摘要由CSDN通过智能技术生成

前言

和前端进行数据交互时或者和第三方商家对接时,需要对隐私数据进行加密。单向加密,对称加密,非对称加密,其对应的算法也各式各样。java提供了统一的框架来规范(java.security)安全加密这类API。下面将一一介绍

  • 加密算法概念及分类
  • 秘钥生成
  • 摘要算法工具-MessageDigest
  • 签名算法工具-Signature
  • 常用加密工具类-Cipher
  • Certificate-证书的保存
  • KeyStore-密钥证书的实体类
  • https证书加载
  • 另外本人整理了20年面试题大全,包含spring、并发、数据库、Redis、分布式、dubbo、JVM、微服务等方面总结,下图是部分截图,需要的话扫码,暗号CSDN。

    在这里插入图片描述

1 加密算法概念及分类

常用的加密算法类型有三种,如下:

  • 单向加密:也就是不可逆的加密,例如MD5,SHA,HMAC
  • 对称加密:也就是加密方和解密方利用同一个秘钥对数据进行加密和解密,例如DES,PBE等等
  • 非对称加密:非对称加密分为公钥和秘钥,二者是非对称的,例如用私钥加密的内容需要使用公钥来解密,使用公钥加密的内容需要用私钥来解密,DSA,RSA

2 秘钥生成

对称加密密钥的生成

  • KeyGenerator用于生成对称秘钥(可逆加密),或者一个密码性秘钥
  • 支持算法:AES、ARCFOUR、DES、DESede、HmacMD5、HmacSHA1、HmacSHA224、HmacSHA256、HmacSHA384、HmacSHA512、RC2
 
  1. public static final KeyGenerator getInstance(String algorithm, String provider)

  2. public static final KeyGenerator getInstance(String algorithm)

  3. public final void init(int keysize)

  4. public final void init(int keysize, SecureRandom random)

  5. public final void init(SecureRandom random)

  6. public final void init(AlgorithmParameterSpec params, SecureRandom random)

  7. public final SecretKey generateKey()

  • 示例
 
  1. public static void main(String[] args) throws Exception {

  2. SecretKey secretKey = generatorDesKey();

  3. System.out.println(secretKey);

  4. }

  5. public static SecretKey generatorDesKey() throws NoSuchAlgorithmException {

  6. KeyGenerator keyGen = KeyGenerator.getInstance("DES");

  7. SecureRandom random = new SecureRandom();

  8. random.nextBytes(new byte[128]);

  9. keyGen.init(56,random);

  10. SecretKey key = keyGen.generateKey();

  11. return key;

  12. }

  13. ------------输出结果------------------

  14. com.sun.crypto.provider.DESKey@185c3

  15. 复制代码

非对称加密秘钥的生成

  • KeyPairGenerator用于生成非对称加密算法的密钥对KeyPair,KeyPair会包括一个公钥和私钥
  • 支持算法:DiffieHellman、DSA、RSA、RSASSA-PSS、EC
 
  1. //KeyPairGenerator.java

  2. public static KeyPairGenerator getInstance(String algorithm)

  3. public static KeyPairGenerator getInstance(String algorithm, String provider)

  4. public void initialize(int keysize, SecureRandom random)

  5. public void initialize(AlgorithmParameterSpec params, SecureRandom random)

  6. public final KeyPair genKeyPair()

  7. //KeyPair.java

  8. public PublicKey getPublic()

  9. public PrivateKey getPrivate()

  10. 复制代码

  • 示例
 
  1. public static void main(String[] args) throws Exception {

  2. KeyPair keyPair = generatorRsaKey();

  3. System.out.println(keyPair);

  4. }

  5. public static KeyPair generatorRsaKey() throws Exception {

  6. KeyPairGenerator keyGen = KeyPairGenerator.getInstance("RSA");

  7. SecureRandom random = new SecureRandom();

  8. random.nextBytes(new byte[516]);

  9. keyGen.initialize(516,random);

  10. KeyPair keyPair = keyGen.genKeyPair();

  11. System.out.println(keyPair.getPrivate());

  12. System.out.println(keyPair.getPublic());

  13. return keyPair;

  14. }

  15. 复制代码

  • 输出结果
 
  1. SunRsaSign RSA private CRT key, 516 bits</

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值