常见哈希算法;加密算法,对称式加密与非对称式加密的对比( 二 )


对称式加密:对称加密算法就是传统的用一个密码进行加密和解密 。例如,我们常用的 WinZIP 和WinRAR对压缩包的加密和解密,就是使用对称加密算法 。从程序的角度看,所谓加密,就是这样一个函数,它接收密码和明文,然后输出密文:secret = encrypt(key,message);而解密则相反,它接收密码和密文,然后输出明文:plain = decrypt(key,secret);![在这里插入图片描述](https://img-blog.csdnimg.cn/3db48750354545ad90200562a8d6450c.png)密钥长度直接决定加密强度,而工作模式和填充模式可以看成是对称加密算法的参数和格式选择 。Java标准库提供的算法实现并不包括所有的工作模式和所有填充模式,但是通常我们只需要挑选常用的使用就可以了 。最后注意,DES 算法由于密钥过短,可以在短时间内被暴力破解,所以现在已经不安全了 。、**用AES算法加密**使用AES算法的ECB模式加密并解密 。```javaimport java.security.*;import java.util.Base64;import javax.crypto.*;import javax.crypto.spec.*;public class Main {public static void main(String[] args) throws Exception {String message = "Hello, world!";System.out.println("Message(原始信息): " + message);byte[] key = "1234567890abcdef".getBytes();byte[] data = http://www.kingceram.com/post/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");SecretKey keySpec = new SecretKeySpec(key, "AES");cipher.init(Cipher.ENCRYPT_MODE, keySpec);return cipher.doFinal(input);}// 解密:public static byte[] decrypt(byte[] key, byte[] input) throws GeneralSecurityException {Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");SecretKey keySpec = new SecretKeySpec(key, "AES");cipher.init(Cipher.DECRYPT_MODE, keySpec);return cipher.doFinal(input);}}
CBC模式
cbc模式是在原来的AES加密模式的基础加入了一个IV参数,作为它的随机数,这样的情况下,对于同一份明文,每次生成的密文都不同 。
package com.apesource.demo04;import java.security.*;import java.util.Base64;import javax.crypto.*;import javax.crypto.spec.*;public class Main {public static void main(String[] args) throws Exception {// 原文:String message = "Hello, world!";System.out.println("Message(原始信息): " + message);// 256位密钥 = 32 bytes Key:byte[] key = "1234567890abcdef1234567890abcdef".getBytes();// 加密:byte[] data = http://www.kingceram.com/post/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");// 恢复秘钥对象SecretKeySpec keySpec = new SecretKeySpec(key, "AES");// CBC模式需要生成一个16 bytes的initialization vector:SecureRandom sr = SecureRandom.getInstanceStrong();byte[] iv = sr.generateSeed(16); // 生成16个字节的随机数System.out.println(Arrays.toString(iv));IvParameterSpec ivps = new IvParameterSpec(iv); // 随机数封装成IvParameterSpec参数对象// 初始化秘钥:操作模式、秘钥、IV参数cipher.init(Cipher.ENCRYPT_MODE, keySpec, ivps);// 加密byte[] data = http://www.kingceram.com/post/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); // IVSystem.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;}}