HTTPS协议的实现原理

目录

1、 对称密钥加密

1、1 CBC工作模式

1、2 ECB工作模式

2、对称式加密

3、HTTPS的工作原理


我们所了解的HTTP协议会出现的问题:

  • 使用明文进行通信,内容有可能被窃听
  • 不验证通信方的身份,通信方的身份不能得到验证
  • 无法证明报文的完整性,报文可能在阐述过程中被他人篡改

而我们的HTTPS协议是让HTTP先和SSL进行通信,再由SSLTCP通信,通过SSL可以方式数据被窃听,能够保证数据时加密的,可以得到认证,保证的报文的完整性

1 对称密钥加密

对称密钥加密,意思就是加密和解密使用的密钥是相同的,我们可以先通过密钥进行加密一组数据进行发送,然后接受方可以通过相同的密钥对数据进行解密

  • 优点:运算速度快,因为使用的是同一个密钥
  • 缺点:无法安全的将密钥传输的通信方,因为我们解密需要密钥,那么传输密钥又会出现安全性问题

1、1 CBC工作模式

虽然加密和解密使用的是同一个密钥,但是CBC模式下,每次生成的密钥是不相同的

加密过程:

//加密
public static String encryptCBC(String content,String key) {
    String result="";
    //将原始加密的数据转换为字节数组
    byte[] source = content.getBytes();
    try {
        //创建一个AES算法对象
        //填充方式为NoPadding,加密内容必须为16字节
        //Cipher cipher = Cipher.getInstance("AES/ECB/NoPadding");
        //填充方式为PKCS5Padding,密钥为16字节
        //工作模式为CBC,相同的内容+相同的结果,每次生成的密钥不同
        Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
        //创建密钥
        SecretKeySpec keySpec = new SecretKeySpec(key.getBytes(), "AES");
        //16个字节的随机数组
        SecureRandom random = SecureRandom.getInstanceStrong();
        byte[] randArray = random.generateSeed(16);
        System.out.println("CBC工作模式下产生的随机数:"+Arrays.toString(randArray));
        IvParameterSpec iv = new IvParameterSpec(randArray);
        //初始化,Cipher.ENCRYPT_MODE加密
        cipher.init(Cipher.ENCRYPT_MODE, keySpec,iv);
        //最终结果
        byte[] enResult = cipher.doFinal(source);
        //合并数组(随机数数组+加密结果数组)
        enResult= megerArray(randArray,enResult);
        result = Base64.getEncoder().encodeToString(enResult);
    } catch (Exception e) {
        e.printStackTrace();
    }
    return result;

}

解密过程

//解密

public static String decryptCBC(String content,String key) {
    String result="";
    //按照Base64进行解码
    byte[] source = Base64.getDecoder().decode(content);
    //拆解出“随机数组”
    byte[] randArray = new byte[16];
    System.arraycopy(source, 0, randArray, 0, 16);
    System.out.println("CBC工作模式下拆解出来的随机数:"+Arrays.toString(randArray));
    //拆解出“加密后数组”
    byte[] input=new byte[source.length-randArray.length];
    System.arraycopy(source, randArray.length, input, 0, input.length);
    try {
        //创建一个AES算法对象
        Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
        //创建密钥
        SecretKeySpec keySpec = new SecretKeySpec(key.getBytes(), "AES");
        //创建参数对象(封装16字节的随机数组)
        IvParameterSpec iv = new IvParameterSpec(randArray);
        //初始化,Cipher.DECRYPT_MODE解密
       cipher.init(Cipher.DECRYPT_MODE, keySpec,iv);
        //最终结果
        byte[] deResult = cipher.doFinal(input);
        result=new String(deResult);
    } catch (Exception e) {
        e.printStackTrace();
    }
    return result;
}        

//合并数组
public static byte[] megerArray(byte[] arr1,byte[] arr2) {
    byte[] resultArray = new byte[arr1.length+arr2.length];
    System.arraycopy(arr1, 0, resultArray, 0, arr1.length);
    System.arraycopy(arr2, 0, resultArray, arr1.length, arr2.length);
    return resultArray;

}

1、2 ECB工作模式

加密和解密过程使用的同一个密钥,并且每次每次生成的密钥都是相同的

加密过程:

//加密

public static String encrypt(String content,String key) {
    String result="";
    try {
        //将原始加密的数据转换为字节数组
        byte[] source = content.getBytes();
        //创建一个AES算法对象
        //填充方式为NoPadding,加密内容必须为16字节
        //Cipher cipher = Cipher.getInstance("AES/ECB/NoPadding");
        //填充方式为PKCS5Padding,密钥为16字节
        Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
        //创建密钥
        SecretKeySpec keySpec = new SecretKeySpec(key.getBytes(), "AES");
        //初始化,Cipher.ENCRYPT_MODE加密
        cipher.init(Cipher.ENCRYPT_MODE, keySpec);
        //最终结果
        byte[] enResult = cipher.doFinal(source);
        result = Base64.getEncoder().encodeToString(enResult);
    }catch(Exception e) {
        e.printStackTrace();
    }
    return result;

}

解密过程:

//解密

public static String decrypt(String content,String key) {
    String result="";
    //按照Base64进行解码
    byte[] source = Base64.getDecoder().decode(content);
    try {
        //创建一个AES算法对象
        Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
        //创建密钥
        SecretKeySpec keySpec = new SecretKeySpec(key.getBytes(), "AES");
        //初始化,Cipher.DECRYPT_MODE解密
        cipher.init(Cipher.DECRYPT_MODE, keySpec);
        //最终结果
        byte[] deResult = cipher.doFinal(source);
        result=new String(deResult);
    } catch (Exception e) {
        e.printStackTrace();
    }
    return result;
}

2、对称式加密

        非对称加密方式,加密和解密使用的是不同的密钥,公开密钥所有人都可以获取,通信发送方获得接受方的密钥,就可以使用公开密钥进行加密,接受方收到通信内容后使用私钥进行解密,非对称密钥除了用来加密,还可以用来进行签名。因为私有密钥无法被其他人获取,因此通信发送方使用其私有密钥进行签名,通信接收方使用发送方的公开密钥对签名进行解密,就能判断这个签名是否正确。

  • 优点:可以更安全地将公开密钥传输给通信发送方;
  • 缺点:运算速度慢。

代码实现:

public class Rsakit {
    PrivateKey sk;  //公钥
    PublicKey pk;   //私钥
    //构造方法
    public Rsakit() throws GeneralSecurityException{
    //创建密钥生成器
        KeyPairGenerator kpGen = KeyPairGenerator.getInstance("RSA");
        kpGen.initialize(1024);
        //生成“密钥对”
        KeyPair kp = kpGen.generateKeyPair();
        this.sk = kp.getPrivate();
        this.pk = kp.getPublic();
    }
    public byte[] getPublicKey() {
        return this.pk.getEncoded();
    }

    public byte[] getPrivateKey() {
        return this.sk.getEncoded();
    }

    //用公钥进行加密
    public byte[] encrypt(byte[] message) throws GeneralSecurityException {
        //创建RSA加密算法对象
        Cipher cipher = Cipher.getInstance("RSA");
        //初始化
        cipher.init(Cipher.ENCRYPT_MODE, this.pk);
        //最终结果
        return cipher.doFinal(message);
    }

    //用公钥进行加密
    public byte[] decrypt(byte[] input) throws GeneralSecurityException {
        Cipher cipher = Cipher.getInstance("RSA");
        cipher.init(Cipher.DECRYPT_MODE, this.sk);        
        return cipher.doFinal(input);
    }

}

3、HTTPS的工作原理

  1. 用户通过浏览器请求HTTPS网站,服务器收到请求,选择浏览器支持的加密和hash算法,同时返回数字证书给浏览器,包含颁布机构、网址、公钥、整数有效期等信息
  2. 浏览器对整数的内容进行校验,如果有问题,则会有一个提示警告,否则,就生成随机密钥X,同时使用整数中的公钥进行加密,兵器发送给服务器
  3. 服务器收到后,使用私钥解密,得到随机密钥X,然后使用随机密钥X对网页内容进行加密,返回给浏览器
  4. 浏览器则使用随机密钥X和之前约定好的加密算法进行解密,得到最终网页内容

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值