对称加密算法与非对称加密算法

对称加密算法

概念

对称加密(也叫私钥加密)指加密和解密使用相同密钥的加密算法,在大多数的对称算法中,加密密钥和解密密钥是相同的,所以也称这种加密算法为单密钥算法。

对称加密算法的特点是算法公开、计算量小、加密速度快、加密效率高。不足之处是,交易双方都使用同样钥匙,安全性得不到保证。

对称加密算法就是传统的用一个秘钥进行加密和解密。

在软件开发中,常用的对称加密算法有:

算法

密钥长度工作模式填充模式
DES56/64ECB/CBC/PCBC/CTR/...NoPadding/PKCS5Padding/...
AES128/192/256ECB/CBC/PCBC/CTR/...NoPadding/PKCS5Padding/PKCS7Padding/...
IDEA128ECBPKCS5Padding/PKCS7Padding/...

密钥长度直接决定加密强度,而工作模式和填充模式可以看成是对称加密算法的参数和格式选择。Java标准库提供的算法实现并不包括所有的工作模式和所有的填充模式。

最后,值得注意的是,DES算法由于密钥果断,可以在短时间内被暴力破解,所有现在已经不安全了。 

使用AES加密

AES算法是目前应用最广泛的加密算法。比较常见的是工作模式是ECB和CBC。

ECB模式

ECB模式是最简单的AES加密模式,它需要一个固定长度的密钥,固定的铭文会生成固定的密文。

AES + ECB模式下的代码:

import java.security.GeneralSecurityException;
import java.util.Base64;

import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;

public class Demo01 {
//AES + ECB
	public static void main(String[] args) throws GeneralSecurityException {
		// 原文:
		String message = "天生我材必有用飞流直下三千尺";
		System.out.println("Message(原始信息): " + message);

		// 128位密钥 = 16 bytes Key:
		byte[] key = "1234567890abcdef".getBytes();

		// 加密:
		byte[] data = message.getBytes();
		byte[] encrypted = encrypt(key, data);
		System.out.println("Encrypted(加密内容): " + Base64.getEncoder().encodeToString(encrypted));

		// 解密:
		byte[] decrypted = decrypt(key, encrypted);
		System.out.println("Decrypted(解密内容): " + new String(decrypted));
	}

	// 加密:
	public static byte[] encrypt(byte[] key, byte[] input) throws GeneralSecurityException  {
		// 创建密码对象,需要传入算法/工作模式/填充模式
		Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");

		// 根据key的字节内容,"恢复"秘钥对象
		SecretKey keys = new SecretKeySpec(key, "AES");
		// 初始化秘钥:设置加密模式ENCRYPT_
		cipher.init(Cipher.ENCRYPT_MODE, keys);
		// 根据原始内容(字节),进行加密
		return cipher.doFinal(input);
	}

	// 解密:
	public static byte[] decrypt(byte[] key, byte[] input) throws GeneralSecurityException{
		// 创建密码对象,需要传入算法/工作模式/填充模式
		Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
		// 根据key的字节内容,"恢复"秘钥对象
		SecretKey keys = new SecretKeySpec(key, "AES");
		// 初始化秘钥:设置解密模式DECRYPT_MODE
		cipher.init(Cipher.DECRYPT_MODE, keys);
		// 根据原始内容(字节),进行解密
		return cipher.doFinal(input);
	}
}

Java标准库提供的对称加密接口非常简单,使用时按以下步骤编写代码:

  • 根据算法名称/工作模式/填充模式获取Cipher实例
  • 根据算法名称初始化一个 Secretkey 实例,密钥必须是指定长度
  • 使用 SerectKey 初始化 Cipher 实例,并设置加密或解密模式
  • 传入明文或密文,获得密文或明文

CBC模式 

ECB 模式是最简单的 AES 加密模式,这种一对一的加密方式会导致安全性降低。所以,更好的方式是通过 CBC 模式它需要一个随机数作为 IV 参数,这样对于同一份明文,每次生成的密文都不同:

AES加密 + CBC模式

import java.security.GeneralSecurityException;
import java.security.SecureRandom;
import java.util.Arrays;
import java.util.Base64;

import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import javax.xml.transform.Source;

//AES加密 + CBC模式
public class Demo02 {
		public static void main(String[] args) throws Exception {
	        // 原文:
	        String message = "Hello World!";
	        System.out.println("Message(原始信息): " + message);
	        
	        // 256位密钥 = 32 bytes Key:
	        byte[] key = "1234567890abcdef".getBytes();  //必须为16位
	        
	        // 加密:
	        byte[] data = message.getBytes();
	        byte[] encrypted = encrypt(key, data);
	        System.out.println("Encrypted(加密内容): " + 
					Base64.getEncoder().encodeToString(encrypted));
	        
	        // 解密:
	        byte[] decrypted = decrypt(key, encrypted);
	        System.out.println("Decrypted(解密内容): " + new String(decrypted));
	    }

	    // 加密:
	    public static byte[] encrypt(byte[] key, byte[] input) throws GeneralSecurityException {
	        // 设置算法/工作模式CBC/填充
	    	Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
	    	// 恢复秘钥对象
	        SecretKey keySpec = new SecretKeySpec(key,"AES");
	        // CBC模式需要生成一个16 bytes的initialization vector:
	        SecureRandom sr = SecureRandom.getInstanceStrong();
	        byte[] iv = sr.generateSeed(16);     //生成16个字节的随机数
	        System.out.println("iv字节数组(内容)"+Arrays.toString(iv));
	        System.out.println("iv字节数组(长度)"+iv.length);
	        //随机数封装成IvParameterSpec参数对象
	        IvParameterSpec ivps = new IvParameterSpec(iv);
	        // 初始化秘钥:操作模式、秘钥、IV参数
	        cipher.init(Cipher.ENCRYPT_MODE, keySpec,ivps);
	        // 加密
	        byte[] data = cipher.doFinal(input);
	        // IV不需要保密,把IV和密文一起返回:
	        return join(iv,data);
	    }

	    // 解密:
	    public static byte[] decrypt(byte[] key, byte[] input) throws GeneralSecurityException {
	        // 把input分割成IV和密文:
	        byte[] iv = new byte[16];
	        byte[] data = new byte[input.length-16];
	        
	        System.arraycopy(input,0,iv,0,16);  //IV
	        System.arraycopy(input,16,data,0,data.length);   //密文
	        System.out.println(Arrays.toString(iv));
	        // 解密:
	       Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");    //密码对象
	       SecretKeySpec keySpec = new SecretKeySpec(key, "AES");    //恢复秘钥
	       IvParameterSpec ivps = new IvParameterSpec(iv);     //恢复IV
	        // 初始化秘钥:操作模式、秘钥、IV参数
	        cipher.init(Cipher.DECRYPT_MODE, keySpec,ivps);
	        // 解密操作
	        return cipher.doFinal(data);
	    }
	    
	    // 合并数组
	    public static byte[] join(byte[] bs1, byte[] bs2) {
	       byte[] r = new byte[bs1.length + bs2.length];
	       System.arraycopy(bs1, 0, r, 0, bs1.length);
	       System.arraycopy(bs2, 0, r, bs1.length, bs2.length);
	        return r;
	    }
	}

观察输出,可以发现每次生成的IV不同,密文也不同。

密钥交换算法

如何在不安全的信道上安全的传输密钥?要解决这个问题,就需要使用密钥交换算法: DH 算法(Diffie-Hellman 算法)。 DH 算法解决了密钥在双方不直接传递密钥的情况下完成密钥交换,这个神奇的交换原理完全由数学理论支持。

使用Java实现DH算法的代码如下:

import java.math.BigInteger;
import java.security.InvalidKeyException;
import java.security.KeyFactory;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.NoSuchAlgorithmException;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.security.spec.InvalidKeySpecException;
import java.security.spec.X509EncodedKeySpec;

import javax.crypto.KeyAgreement;

public class Demo03 {
	public static void main(String[] args) {
		// Bob和Alice:
        Person bob = new Person("Bob");
        Person alice = new Person("Alice");
        
        // 各自生成KeyPair: 公钥+私钥
        bob.generateKeyPair();
        alice.generateKeyPair();

        // 双方交换各自的PublicKey(公钥):
        // Bob根据Alice的PublicKey生成自己的本地密钥(共享公钥):
        bob.generateSecretKey(alice.publicKey.getEncoded());
        
        // Alice根据Bob的PublicKey生成自己的本地密钥(共享公钥):
        alice.generateSecretKey(bob.publicKey.getEncoded());

        // 检查双方的本地密钥是否相同:
        bob.printKeys();
        alice.printKeys();
	}
}

//用户类
class Person {
	public final String name; // 姓名

	// 密钥
	public PublicKey publicKey; // 公钥
	private PrivateKey privateKey; // 私钥
	private byte[] secretKey; // 本地秘钥(共享密钥)

	// 构造方法
	public Person(String name) {
		this.name = name;
	}

	// 生成本地KeyPair:(公钥+私钥)
	public void generateKeyPair() {
		try {
			KeyPairGenerator kpGen = KeyPairGenerator.getInstance("DH");
		    kpGen.initialize(512);
		    
		    //生成一个"秘钥对"
		    KeyPair kp = kpGen.generateKeyPair();
		    this.privateKey = kp.getPrivate();
		    this.publicKey = kp.getPublic();
		} catch (NoSuchAlgorithmException e) {
			e.printStackTrace();
		}
		
	}

	// 按照 "对方的公钥" => 生成"共享密钥"
	public void generateSecretKey(byte[] receivedPubKeyBytes) {
		//从byte[]恢复PublicKey
		X509EncodedKeySpec keySpec = new X509EncodedKeySpec(receivedPubKeyBytes);
		//根据DH算法获取KeyFactory
		
		try {
			KeyFactory kf = KeyFactory.getInstance("DH");
			//通过KeyFactory创建公钥
			PublicKey receivedPubKeys = kf.generatePublic(keySpec);
			
			//创建秘钥协议对象(用于秘钥协商)
			KeyAgreement keyAgreement = KeyAgreement.getInstance("DH");
			//"初始化"自己的privateKey
			keyAgreement.init(this.privateKey);
			//根据"对方的"
			keyAgreement.doPhase(receivedPubKeys,true);
			//生成secretKey本地密钥(共享密钥)
			this.secretKey = keyAgreement.generateSecret();
		} catch (NoSuchAlgorithmException e) {
			e.printStackTrace();
		} catch (InvalidKeySpecException e) {
			e.printStackTrace();
		} catch (InvalidKeyException e) {
			e.printStackTrace();
		} catch (IllegalStateException e) {
			e.printStackTrace();
		}
	}

	public void printKeys() {
		System.out.printf("Name: %s\n", this.name);
		System.out.printf("Private key: %x\n", new BigInteger(1, this.privateKey.getEncoded()));
		System.out.printf("Public key: %x\n", new BigInteger(1, this.publicKey.getEncoded()));
		System.out.printf("Secret key: %x\n", new BigInteger(1, this.secretKey));
	}
}

非对称加密算法

概述

非对称加密: 加密和解密使用的不是相同的密钥,用户A密钥加密后所得的信息,只能用用户A的解密密钥才能解密。如果知道了其中一个,并不能计算出另外一个。因此如果公开了一对密钥中的一个,并不会危害到另外一个的秘密性质。称公开的密钥为公钥;不公开的密钥为私钥。只有同一个公钥-私钥对才能正常加解密。

非对称加密的缺点: 运算速度非常慢,比对称加密要慢很多。

从 DH 算法我们可以看到,公钥-私钥组成的密钥对是非常有用的加密方式,因为公钥是可以公开的,而私钥是完全保密的,由此奠定了非对称加密的基础。

非对称加密的典型算法就是 RSA 算法,它是由Ron Rivest、Adi Shamir、Leonard Adleman这三个人一起发明的,所以用他们三个人的姓氏首字母缩写表示。

案例

import java.math.BigInteger;
import java.security.GeneralSecurityException;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.PrivateKey;
import java.security.PublicKey;

import javax.crypto.Cipher;

// RSA
public class Demo04 {
		public static void main(String[] args) throws Exception {
			// 明文:
			byte[] plain = "Hello, encrypt use RSA".getBytes("UTF-8");

			// 创建公钥/私钥对
			Human hong = new Human("小红");
			Human ming = new Human("小明");
			
			// 小明使用小红的公钥进行加密
			// 1.获取小红的公钥
			PublicKey hongPublicKey = hong.getPublicKey();
			System.out.println(String.format("小红的public key(公钥): %x", new BigInteger(1, hongPublicKey.getEncoded())));

			// 2.使用公钥加密
			byte[] encrypted = ming.encrypt(plain, hongPublicKey);
			System.out.println(String.format("encrypted(加密): %x", new BigInteger(1, encrypted)));

			// 小红使用自己的私钥解密:
			// 1.获取小红的私钥,并输出
			PrivateKey hongPrivateKey = hong.getPrivateKey();
			System.out.println(String.format("小红的private key(私钥): %x", new BigInteger(1, hongPrivateKey.getEncoded())));

			// 2.使用私钥解密
			byte[] decrypted = hong.decrypt(encrypted);
			System.out.println("decrypted(解密): " + new String(decrypted, "UTF-8"));
		}
	}

	// 用户类
	class Human {
		// 姓名
		String name;

		// 私钥:
		PrivateKey privatekey;

		// 公钥:
		PublicKey publickey;

		// 构造方法
		public Human(String name) throws GeneralSecurityException {
			// 初始化姓名
			this.name = name;

			// 生成公钥/私钥对:
			KeyPairGenerator kpGen = KeyPairGenerator.getInstance("RSA");
			kpGen.initialize(1024);
			KeyPair kp = kpGen.generateKeyPair();
			
			this.privatekey = kp.getPrivate();
			this.publickey= kp.getPublic();
		}

		// 把私钥导出为字节
		public PrivateKey getPrivateKey() {
			return this.privatekey;
		}

		// 把公钥导出为字节
		public PublicKey getPublicKey() {
			return this.publickey;
		}

		// 用公钥加密
		public byte[] encrypt(byte[] message,PublicKey publickey) throws GeneralSecurityException {
			// 使用公钥进行初始化
			Cipher cipher = Cipher.getInstance("RSA");
			cipher.init(Cipher.ENCRYPT_MODE, publickey);
			return cipher.doFinal(message);
		}

		// 用私钥解密:
		public byte[] decrypt(byte[] input) throws GeneralSecurityException {
			// 使用私钥进行初始化
			Cipher cipher = Cipher.getInstance("RSA");
			cipher.init(Cipher.DECRYPT_MODE, this.privatekey);
			return cipher.doFinal(input);
		}
	}

小结

  • 对称加密算法使用同一个密钥进行加密和解密,常用算法有 DES、AES 和 IDEA 等
  • 密钥长度由算法设计决定,AES 的密钥长度是 128/192/ 256 位
  • 使用对称加密算法需要指定算法名称、工作模式和填充模式
  • DH 算法是一种密钥交换协议,通信双方通过不安全的信道协商密钥,然后进行对称加密传输
  • 非对称加密就是加密和解密使用的不是相同的密钥,只有同一个公钥-私钥对才能正常加解密
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值