常用对称加密算法
![a65ab4d8e56884fca4c14be6da41af39.png](https://i-blog.csdnimg.cn/blog_migrate/90a5d0381a842636163811ec737eaeba.png)
1. DES
以64位为分组。64位明文输入,64位密文输出。
算法特点
优点:效率高,算法简单,系统开销小,适合加密大量数据,明文长度和密文长度相等
缺点:需要以安全方式进行秘钥交换,秘钥管理复杂, 分组比较短、密钥太短、密码生命周期短、运算速度较慢。
用途
-
cookie: 可以使用DES来将用户的id和登录时间加密为uid和lid,存在cookie中,然后在拦截器中解密出来验证uid和lid的正确性,从而实现用户通行许可的验证。
-
可用于大量数据的加密
2. 3DES
3DES(或称为Triple DES)是三重数据加密算法(TDEA,Triple Data Encryption Algorithm)块密码的通称。它相当于是对每个数据块应用三次DES加密算法。
由于计算机运算能力的增强,原版DES密码的密钥长度变得容易被暴力破解;3DES即是设计用来提供一种相对简单的方法,即通过增加DES的密钥长度来避免类似的攻击,而不是设计一种全新的块密码算法。
原理:
设Ek()和Dk()代表DES算法的加密和解密过程,K代表DES算法使用的密钥,M代表明文,C代表密文:
3DES加密过程为:C=Ek3 (Dk2 (Ek1(M) ) )
3DES解密过程为:M=Dk1 (Ek2 (Dk3(C) ) )
算法特点
密钥长度的增加
优点:它以DES为基本模块,通过组合分组方法设计出分组加密算法。比起最初的DES,3DES更为安全。
缺点: 效率低,资源消耗大。
Demo:
package com.lett.symenc.improve;
import com.sun.org.apache.xerces.internal.impl.dv.util.Base64;
import org.bouncycastle.jce.provider.BouncyCastleProvider;
import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
/**
* @author lett
* @date 2020/07/29 9:59
*/
public class Des3Test {
/**
* JDK中 3DES 4种不同的模式
* DESede/CBC/NoPadding (112/168)
* DESede/CBC/PKCS5Padding (112/168)
* DESede/ECB/NoPadding (112/168)
* DESede/ECB/PKCS5Padding (112/168)
*/
private static final String algorithm = "DESede/ECB/PKCS5Padding";
public static void main(String[] args) {
//no padding模式下必须是8字节的倍数,也可手动填充
String input = "12345678";
KeyGenerator keyGenerator = null;
try {
keyGenerator = KeyGenerator.getInstance("DESede");
} catch (NoSuchAlgorithmException e) {
System.out.println("密钥生成过程出现错误!");
e.printStackTrace();
}
keyGenerator.init(168);
SecretKey secretKey = keyGenerator.generateKey();
byte[] key = secretKey.getEncoded();
System.out.println(key.length);
String s = null;
String result = null;
// IvParameterSpec iv = get8ByteIV();
//
// try {
// s = encryptionIV(input, key,iv);
// result = decryptIV(s, key,iv);
// } catch (Exception e) {
// System.out.println("加解密过程出现错误!");
// e.printStackTrace();
// }
try {
s = enc(input,key);
result = dec(s,key);
} catch (Exception e) {
e.printStackTrace();
}
System.out.println(s);
System.out.println(result);
}
/**
* CBC模式 3DES加密
* @param string 明文
* @param key 密钥
* @param ivParameterSpec 8字节向量
* @return Base64后的密文
* @throws Exception
*/
public static String encryptionIV(String string,byte [] key,IvParameterSpec ivParameterSpec) throws Exception{
Cipher cipher = Cipher.getInstance(algorithm);
SecretKeySpec secretKeySpec = new SecretKeySpec(key, "DESede");
cipher.init(Cipher.ENCRYPT_MODE,secretKeySpec,ivParameterSpec);
byte[] result = cipher.doFinal(string.getBytes());
return Base64.encode(result);
}
/**
* CBC模式 3DES解密
* @param string Base64后的密文
* @param key 密钥
* @param ivParameterSpec 8字节向量
* @return 明文
* @throws Exception
*/
public static String decryptIV(String string,byte[] key,IvParameterSpec ivParameterSpec)throws Exception{
Cipher cipher = Cipher.getInstance(algorithm);
SecretKeySpec secretKeySpec = new SecretKeySpec(key, "DESede");
cipher.init(Cipher.DECRYPT_MODE,secretKeySpec,ivParameterSpec);
byte[] decode = Base64.decode(string);
byte[] bytes = cipher.doFinal(decode);
return new String(bytes);
}
/**
* 生成8字节向量
* @return
*/
public static IvParameterSpec get8ByteIV(){
SecureRandom secureRandom = new SecureRandom();
byte[] seed = secureRandom.generateSeed(8);
IvParameterSpec spec = new IvParameterSpec(seed);
return spec;
}
/**
* ECB模式 3DES加密
* @param string 明文
* @param key 密钥
* @return Base64后的密文
* @throws Exception
*/
public static String enc(String string,byte[] key) throws Exception{
Cipher cipher = Cipher.getInstance(algorithm);
SecretKeySpec secretKeySpec = new SecretKeySpec(key,"DESede");
cipher.init(Cipher.ENCRYPT_MODE,secretKeySpec);
byte[] bytes = cipher.doFinal(string.getBytes());
String encode = Base64.encode(bytes);
return encode;
}
/**
* ECB模式 3DES解密
* @param string Base64后的密文
* @param key 密钥
* @return 明文
* @throws Exception
*/
public static String dec(String string,byte[] key)throws Exception{
Cipher cipher = Cipher.getInstance(algorithm);
SecretKeySpec secretKeySpec = new SecretKeySpec(key, "DESede");
cipher.init(Cipher.DECRYPT_MODE,secretKeySpec);
byte[] decode = Base64.decode(string);
byte[] bytes = cipher.doFinal(decode);
return new String(bytes);
}
}
出现的错误
![884d1041cba6babe45e00fd82f7e8b24.png](https://i-blog.csdnimg.cn/blog_migrate/9315e4577ab7932abb0a1a60485cd309.png)
源码IvParameterSpec
package javax.crypto.spec;
import java.security.spec.AlgorithmParameterSpec;
public class IvParameterSpec implements AlgorithmParameterSpec {
private byte[] iv;
public IvParameterSpec(byte[] var1) {
this(var1, 0, var1.length);
}
public IvParameterSpec(byte[] var1, int var2, int var3) {
if (var1 == null) {
throw new IllegalArgumentException("IV missing");
} else if (var1.length - var2 < var3) {
throw new IllegalArgumentException("IV buffer too short for given offset/length combination");
} else if (var3 < 0) {
throw new ArrayIndexOutOfBoundsException("len is negative");
} else {
this.iv = new byte[var3];
System.arraycopy(var1, var2, this.iv, 0, var3);
}
}
public byte[] getIV() {
return (byte[])this.iv.clone();
}
}
解决:
使用伪随机数生成器生成一个8字节的向量
3. AES
密码学中的高级加密标准(Advanced Encryption Standard,AES),又称Rijndael加密法,是美国联邦政府采用的一种区块加密标准。
加密模式
对称/分组密码一般分为流加密(如OFB、CFB等)和**块加密(如ECB、CBC等)**。
对于流加密,需要将分组密码转化为流模式工作。对于块加密(或称分组加密),如果要加密超过块大小的数据,就需要涉及填充和链加密模式。
-
ECB (Electronic Code Book电子密码本)模式
ECB模式是最早采用和最简单的模式,它将加密的数据分成若干组,每组的大小跟加密密钥长度相同,然后每组都用相同的密钥进行加密。
优点:
1.简单;
2.有利于并行计算;
3.误差不会被传送;
缺点:
1.不能隐藏明文的模式;
2.可能对明文进行主动攻击;
因此,此模式适于加密小消息。
-
CBC (Cipher Block Chaining,加密块链)模式
优点:
1.不容易主动攻击,安全性好于ECB,适合传输长度长的报文,是SSL、IPSec的标准。
缺点:
1.不利于并行计算;
2.误差传递;
3.需要初始化向量IV
-
CFB (Cipher FeedBack Mode,加密反馈)模式
![5649c70c921c7c99370fb7adb93c02fe.png](https://i-blog.csdnimg.cn/blog_migrate/9d5ae6268bbe01592b495fc9321e928c.png)
优点:
1.隐藏了明文模式;
2.分组密码转化为流模式;
3.可以及时加密传送小于分组的数据;
缺点:
1.不利于并行计算;
2.误差传送:一个明文单元损坏影响多个单元 ;
3.唯一的IV;
-
OFB (Output FeedBack,输出反馈)模式
![3cc270fea7f7b3c80ab40fea489e240d.png](https://i-blog.csdnimg.cn/blog_migrate/5892a1e5c901697d846a9af5f9ea1956.png)
![704cde6120056e668d4c58f124be4bc9.png](https://i-blog.csdnimg.cn/blog_migrate/b4b8ab54b5812693732b820e01935108.png)
优点:
1.隐藏了明文模式;
2.分组密码转化为流模式;
3.可以及时加密传送小于分组的数据;
缺点:
1.不利于并行计算;
2.对明文的主动攻击是可能的;
3.误差传送:一个明文单元损坏影响多个单元 。
-
CTR计数器模式
描述:计算器模式不常见,在CTR模式中,有一个自增的算子,这个算子用密钥加密之后的输出和明文异或的结果得到密文,相当于一次一密。这种加密方式简单快速,安全可靠,而且可以并行加密,但是在计算器不能维持很长的情况下,密钥只能使用一次。
优点:并行、一次一密、不传递误差
缺点:主动攻击(改明文,后续内容不影响,只要误差不传递该缺点就存在)
![d6f8c79974a7b574a4fa378f4e4e6fa8.png](https://i-blog.csdnimg.cn/blog_migrate/2c1d73df348a4751fcab76e49967e7c6.png)
![fb0cd2643efbe2c59dd75a070ec1a4d8.png](https://i-blog.csdnimg.cn/blog_migrate/bb26ac37615ed7f668212860b3e6d525.png)
![7ac40560c8cec02baae57260ef71944a.png](https://i-blog.csdnimg.cn/blog_migrate/30de7d4304b59b8ffc39ec3bd1173f34.png)
AES的算法特点
![7dd23613d5dcc555c11a39b6f3ffe08b.png](https://i-blog.csdnimg.cn/blog_migrate/612fae413fbf107a7c949f9c793b340f.png)
优点:AES对内存的需求非常低,运算速度快,在有反馈模式、无反馈模式的软硬件中,表现出非常好的性能,
缺点:密钥传输不安全
应用场景
-
AES加密速度快,适合大量数据,处理数据后可复原。
Demo
package com.lett.symenc.improve;
import com.sun.org.apache.xerces.internal.impl.dv.util.Base64;
import org.bouncycastle.jce.provider.BouncyCastleProvider;
import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import java.security.Provider;
import java.security.SecureRandom;
import java.security.Security;
public class AesTest {
/**
* JDK AES的模式只支持128位 需要解除限制
* AES/CBC/NoPadding (128)
* AES/CBC/PKCS5Padding (128)
* AES/ECB/NoPadding (128)
* AES/ECB/PKCS5Padding (128)
*
* AES/OFB/PKCS5Padding
* AES/CFB/PKCS5Padding
* AES/CTR/PKCS5Padding
*
* AES/OFB/NoPadding
* AES/CFB/NoPadding
* AES/CTR/NoPadding
*
*/
private static final String algorithm = "AES/CBC/NoPadding";
private static final Integer keySize = 256;
public static void main(String[] args) throws Exception{
//加入BC支持
Security.addProvider(new BouncyCastleProvider());
//必须是8字节的倍数
String string = "1234567812345678";
KeyGenerator keyGenerator = KeyGenerator.getInstance("AES","BC");
keyGenerator.init(keySize);
SecretKey secretKey = keyGenerator.generateKey();
byte[] key = secretKey.getEncoded();
// byte[] key = new SecureRandom().generateSeed(24);
// String output = encryption(string,key);
// System.out.println("密文:"+output+" "+output.length());
// System.out.println("明文:"+decrypt(output,key));
// 向量为16个字节
IvParameterSpec iv = get16ByteIV();
String s = encryptionIV(string, key, iv);
System.out.println(s);
String s1 = decryptIV(s, key, iv);
System.out.println(s1);
}
/**
* AES ECB 加密
* @param input 明文
* @param key 密钥
* @return 密文
* @throws Exception
*/
public static String encryption(String input,byte[] key) throws Exception {
Cipher cipher = Cipher.getInstance(algorithm,"BC");
SecretKeySpec secretKeySpec = new SecretKeySpec(key,"AES");
cipher.init(Cipher.ENCRYPT_MODE,secretKeySpec);
byte[] bytes = cipher.doFinal(input.getBytes());
String encode = Base64.encode(bytes);
return encode;
}
/**
* DES ECB 解密
* @param string 密文
* @param key 密钥
* @return 明文
* @throws Exception
*/
public static String decrypt(String string,byte[] key) throws Exception{
Cipher cipher = Cipher.getInstance(algorithm,"BC");
SecretKeySpec secretKeySpec = new SecretKeySpec(key, "AES");
cipher.init(Cipher.DECRYPT_MODE,secretKeySpec);
byte[] decode = Base64.decode(string);
byte[] bytes = cipher.doFinal(decode);
return new String(bytes);
}
/**
* CBC模式 AES加密
* @param string 明文
* @param key 密钥
* @param ivParameterSpec 8字节向量
* @return Base64后的密文
* @throws Exception
*/
public static String encryptionIV(String string, byte [] key, IvParameterSpec ivParameterSpec) throws Exception{
Cipher cipher = Cipher.getInstance(algorithm,"BC");
SecretKeySpec secretKeySpec = new SecretKeySpec(key, "AES");
cipher.init(Cipher.ENCRYPT_MODE,secretKeySpec,ivParameterSpec);
byte[] result = cipher.doFinal(string.getBytes());
return Base64.encode(result);
}
/**
* CBC模式 AES解密
* @param string Base64后的密文
* @param key 密钥
* @param ivParameterSpec 8字节向量
* @return 明文
* @throws Exception
*/
public static String decryptIV(String string,byte[] key,IvParameterSpec ivParameterSpec)throws Exception{
Cipher cipher = Cipher.getInstance(algorithm,"BC");
Provider provider = cipher.getProvider();
System.out.println(provider.getName());
SecretKeySpec secretKeySpec = new SecretKeySpec(key,"AES");
cipher.init(Cipher.DECRYPT_MODE,secretKeySpec,ivParameterSpec);
byte[] decode = Base64.decode(string);
byte[] bytes = cipher.doFinal(decode);
return new String(bytes);
}
/**
* 生成16字节向量
* @return
*/
public static IvParameterSpec get16ByteIV(){
SecureRandom secureRandom = new SecureRandom();
byte[] seed = secureRandom.generateSeed(16);
IvParameterSpec spec = new IvParameterSpec(seed);
return spec;
}
}
AES Demo在密钥长度为192,256位加密时出现的问题
![8fb00dd36b160c8e7c20d79d7366afa4.png](https://i-blog.csdnimg.cn/blog_migrate/a9ce3d623e5498faa72ab7de6ecc67f2.png)
解决:
JDK8的加密策略存在限制版本和无限制版本,随着越来越多的第三方工具只支持 JDK8,业务环境中,发现有些方法会报异常:
1、使用AES加解密
java.security.InvalidKeyException: Illegal key size
2、安全性机制导致的访问https会报错:
Received fatal alert: handshake_failure; nested exception is javax.net.ssl.SSLHandshakeException:
Received fatal alert: handshake_failure,accessUrl
这是因为某些国家的进口管制限制,JDK默认的加解密有一定的限制。
比如默认不允许 256 位密钥的 AES 加解密,解决方法就下载官方JCE无限制强度加密策略文件,覆盖即可。
官方网站提供了JCE无限制权限策略文件的下载:
JDK8的下载地址:
http://www.oracle.com/technetwork/java/javase/downloads/jce8-download-2133166.html
但从**Java 1.8.0_151和1.8.0_152 **开始,为JVM启用无限制强度管辖策略 有了一种新的更简单的方法。如果不启用此功能,则不能使用AES-256:
在 jre/lib/security 文件夹中查找文件 java.security,现在用文本编辑器打开java.security,并找到定义java安全性属性crypto.policy的行,它可以有两个值limited或unlimited - 默认值是limited。将其设置为:
crypto.policy=unlimited
现在重新启动指向JVM的Java应用程序即可。
非对称加密(公钥加密)
![f5764d656c26ac1659128a9da2c6c1c7.png](https://i-blog.csdnimg.cn/blog_migrate/1f9fa5e3afd2340959c4f95d479b4751.png)
![1055b8ff6b76ce155555721ade3467d4.png](https://i-blog.csdnimg.cn/blog_migrate/db45e089a6c8676df171caaf181673d2.png)
国际常用非对称加密算法
名称 | 密钥模长 | 成熟度 | 安全性(取决于密钥模长) | 运算速度 | 资源的消耗 |
RSA | 1024,2048,3072,4096 | 高 | 高 | 慢 | 高 |
DSA |
| 高 | 高 | 慢 | 只能用于数字签名 |
ECC | 160,224,256, | 低 | 高 | 快 | 低(计算量小,存储空间占用小,带宽要求低) |
1.RSA (目前安全的已经是在2048位以上了,不推荐1024位的了)
RSA密钥是(公钥+模值)、(私钥+模值)分组分发的,单独给对方一个公钥或私钥是没有任何用处,所以我们说的“密钥”其实是它们两者中的其中一组。但我们说的“密钥长度”一般只是指模值的位长度。目前主流可选值:1024、2048、3072、4096...
每次加密的字节数,不能超过密钥的长度值减去11,而每次加密得到的密文长度,却恰恰是密钥的长度。所以,如果要加密较长的数据,可以采用数据截取的方法,分段加密。
算法特点
优点:安全性好
缺点:运算速度慢,资源消耗高
RSA作为一种低效的加密方法,用在加密大量数据上面是不合适的,即使是签名之类的地方,能尽量少用也要少用,否则对性能影响很大。
RSA产生密钥很麻烦,受到素数产生技术的限制
用途
适用于少量数据的加密
用来加密对称算法的密钥,而密文多用对称加密算法加密传输。
数字签名
Demo
package com.lett.asyenc;
import com.sun.org.apache.xerces.internal.impl.dv.util.Base64;
import javax.crypto.Cipher;
import java.security.*;
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;
/**
* RSA demo
* @author let
* @createTime 2020/07/30 9:49
*/
public class RSADemo {
/**
* Integer 1为公钥 0为私钥
* String BASE64得到公私钥
*/
private static Map<Integer,String> keyMap = new HashMap<Integer, String>();
/**
* JDK
* RSA/ECB/PKCS1Padding ( 1024,2048 )
* RSA/ECB/OAEPWithSHA-1AndMGF1Padding ( 1024,2048 )
* RSA/ECB/OAEPWithSHA-256AndMGF1Padding ( 1024,2048 )
*/
private static final String algorithm = "RSA/ECB/OAEPWithSHA-1AndMGF1Padding";
/**
* JDK
* RSA/ECB/PKCS1Padding
* 模长 -> 明文长度
* 1024 -> 117
* 2048 -> 245
*/
private static final Integer keySize = 2048;
public static void main(String[] args) throws Exception {
//Data must not be longer than 117 bytes
// SecureRandom secureRandom = new SecureRandom();
// byte[] bytes = secureRandom.generateSeed(118);
// System.out.println(bytes.length);
// String string = new String(bytes);
String string = "123456781234567812234567812345678123456781234567812345678123456781234567812345678";
getKeyPair(keySize);
System.out.println("随机公钥:"+keyMap.get(0));
System.out.println("随机私钥:"+keyMap.get(1));
String temp = rsaEncrypt(string, keyMap.get(0));
System.out.println("密文:"+ temp);
System.out.println("明文:"+rsaDecrypt(temp, keyMap.get(1)));
}
/**
* 随机生成的密钥对
* @param keySize 密钥大小
* @throws NoSuchAlgorithmException
*/
public static void getKeyPair(Integer keySize) throws NoSuchAlgorithmException {
//返回生成指定算法的公钥/私钥对的KeyPairGenerator对象
KeyPairGenerator rsa = KeyPairGenerator.getInstance("RSA");
//初始化密钥对生成器
rsa.initialize(keySize);
// 生成一个密钥对,保存在keyPair对象中
KeyPair keyPair = rsa.generateKeyPair();
// 得到公钥,私钥
//PublicKey aPublic = keyPair.getPublic();
//PrivateKey aPrivate = keyPair.getPrivate();
RSAPublicKey rsaPublicKey = (RSAPublicKey) keyPair.getPublic();
RSAPrivateKey rsaPrivateKey = (RSAPrivateKey) keyPair.getPrivate();
byte[] rsaPublicKeyEncoded = rsaPublicKey.getEncoded();
byte[] rsaPrivateKeyEncoded = rsaPrivateKey.getEncoded();
System.out.println("公钥长度:"+rsaPublicKeyEncoded.length * 8);
System.out.println("私钥长度:"+rsaPrivateKeyEncoded.length * 8);
// 得到Base64的密钥字符串
String publicKeyString = new String(Base64.encode(rsaPublicKeyEncoded));
String privateKeyString = new String(Base64.encode(rsaPrivateKeyEncoded));
keyMap.put(0,publicKeyString);
keyMap.put(1,privateKeyString);
}
/**
* RSA 加密
* @param string 原始数据
* @param publicKey 公钥
* @return 密文
* @throws Exception
*/
public static String rsaEncrypt(String string,String publicKey) throws Exception {
byte[] bytes = Base64.decode(publicKey);
//Cipher rsa = Cipher.getInstance("RSA");
Cipher rsa = Cipher.getInstance(algorithm);
RSAPublicKey key = (RSAPublicKey) KeyFactory.getInstance("RSA").generatePublic(new X509EncodedKeySpec(bytes));
rsa.init(Cipher.PUBLIC_KEY,key);
byte[] result = rsa.doFinal(string.getBytes());
System.out.println("密文长度:"+result.length*8);
String encode = Base64.encode(result);
return encode;
}
/**
* RSA 解密
* @param string 密文
* @param privateKey 私钥
* @return 原始数据
* @throws Exception
*/
public static String rsaDecrypt(String string,String privateKey) throws Exception{
// 密文base64解码
byte[] decode = Base64.decode(string);
// 密钥base64解码
byte[] keyBytes = Base64.decode(privateKey);
// Only RSAPrivate(Crt)KeySpec and PKCS8EncodedKeySpec supported for RSA private keys
// 注意公钥钥的材料规范不同
RSAPrivateKey key = (RSAPrivateKey) KeyFactory.getInstance("RSA").generatePrivate(new PKCS8EncodedKeySpec(keyBytes));
//Cipher rsa = Cipher.getInstance("RSA");
Cipher rsa = Cipher.getInstance(algorithm);
rsa.init(Cipher.PRIVATE_KEY,key);
byte[] result = rsa.doFinal(decode);
System.out.println("原文长度:"+result.length);
return new String(result);
}
}
使用BC,BC支持一些JDK不支持模式
package com.lett.asyenc.improve;
import com.sun.org.apache.xerces.internal.impl.dv.util.Base64;
import org.bouncycastle.jce.provider.BouncyCastleProvider;
import javax.crypto.Cipher;
import java.security.*;
import java.security.spec.PKCS8EncodedKeySpec;
import java.security.spec.X509EncodedKeySpec;
import java.util.HashMap;
import java.util.Map;
public class RsaTest {
/**
* Integer 1为公钥 0为私钥
* String 公私钥
*/
private static Map<Integer,Key> keyMap = new HashMap<>();
private static final String algorithm = "RSA/ECB/PKCS1Padding";
private static final Integer keySize = 4096;
public static void main(String[] args) throws Exception{
Security.addProvider(new BouncyCastleProvider());
String string = "12345678";
try {
getKeyPair();
} catch (Exception e) {
System.out.println("密钥生成错误!");
e.printStackTrace();
}
String s = rsaEncrypt(string);
System.out.println(s);
String result = rsaDecrypt(s);
System.out.println(result);
}
public static void getKeyPair() throws Exception{
KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("RSA", "BC");
keyPairGenerator.initialize(keySize);
KeyPair keyPair = keyPairGenerator.generateKeyPair();
byte[] pri = keyPair.getPrivate().getEncoded();
byte[] pub = keyPair.getPublic().getEncoded();
KeyFactory keyFactory = KeyFactory.getInstance("RSA", "BC");
PrivateKey privateKey = keyFactory.generatePrivate(new PKCS8EncodedKeySpec(pri));
PublicKey publicKey = keyFactory.generatePublic(new X509EncodedKeySpec(pub));
keyMap.put(0,privateKey);
keyMap.put(1,publicKey);
}
/**
* RSA 加密
* @param string 原始数据
* @return 密文
* @throws Exception
*/
public static String rsaEncrypt(String string) throws Exception {
Cipher rsa = Cipher.getInstance(algorithm,"BC");
rsa.init(Cipher.PUBLIC_KEY,keyMap.get(1));
byte[] result = rsa.doFinal(string.getBytes());
System.out.println("密文长度:"+result.length*8);
String encode = Base64.encode(result);
return encode;
}
/**
* RSA 解密
* @param string 密文
* @return 原始数据
* @throws Exception
*/
public static String rsaDecrypt(String string) throws Exception{
//密文base64解码
byte[] decode = Base64.decode(string);
Cipher rsa = Cipher.getInstance(algorithm,"BC");
rsa.init(Cipher.PRIVATE_KEY,keyMap.get(0));
byte[] result = rsa.doFinal(decode);
System.out.println("原文长度:"+result.length);
return new String(result);
}
}
注意
// RSA的加密密钥规范和解密密钥规范标准是不一样的
// RSA加密密钥规范使用x509EncodedKeySpec
// RSA解密密钥规范使用pkcs8EncodedKeySpec
根据代码的运行的速度可知,密钥模长越长则RSA 加解密的速度越慢。
数据需要分段加密
![3c9690b29d1bd4a9f2a67f531bfc351f.png](https://i-blog.csdnimg.cn/blog_migrate/77d078c3da90855830afb5e71be4f754.png)
2.ECC
ECC(Elliptic Curves Cryptography,椭圆曲线密码编码学)也属于公开密钥算法。
JDK 中Chipher不支持EC算法 Chipher、Signature、KeyPairGenerator、KeyAgreement、SecretKey均不支持EC算法。
ECC是椭圆曲线算法,其加密算法叫ECIES,签名算法叫ECDSA。
算法特点
RSA的优点:JDK自己支持。不需要第三方库。同时支持RSA的开发库也很多.
EC的缺点:需要第三方库,支持的广度比不上RSA。
EC的优点:
1,在达到相同加密程度下,EC需要的秘钥长度比RSA要短得多
2,bouncycastle实现的EC加密算法,对密文长度的限制比较松。在下面的测试程序中构造了一个长字符串加密,没有报错。
RSA的加密则是有限制的,必须分片。
用途
国密算法SM基于ECC(椭圆曲线算法)实现的。
Demo
package com.lett.asyenc.improve;
import org.bouncycastle.jce.provider.BouncyCastleProvider;
import javax.crypto.Cipher;
import java.security.*;
import java.security.interfaces.ECPrivateKey;
import java.security.interfaces.ECPublicKey;
import java.util.Base64;
/**
* ECC demo
* @author let
*/
public class EccTest{
/**
* BC 仅支持的密钥位数
* 192, 224, 239, 256, 384, 521
*/
private final static int KEY_SIZE = 224;//bit
static {
Security.addProvider(new BouncyCastleProvider());
}
public static void main(String[] args) {
try {
KeyPair keyPair = getKeyPair();
ECPublicKey pubKey = (ECPublicKey) keyPair.getPublic();
ECPrivateKey priKey = (ECPrivateKey) keyPair.getPrivate();
//这里可以加长测试的长度
String content = "12345678";
//加密
byte[] cipherTxt = encrypt(content.getBytes(), pubKey);
//解密
byte[] clearTxt = decrypt(cipherTxt, priKey);
//打印
System.out.println("明文:" + content);
System.out.println("密文["+cipherTxt.length+"]:" + Base64.getEncoder().encodeToString(cipherTxt));
System.out.println("解密后:" + new String(clearTxt));
} catch (Exception e) {
e.printStackTrace();
System.out.println("[main]-Exception:" + e.toString());
}
}
//生成秘钥对
public static KeyPair getKeyPair() throws Exception {
KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("EC", "BC");
keyPairGenerator.initialize(KEY_SIZE, new SecureRandom());
KeyPair keyPair = keyPairGenerator.generateKeyPair();
return keyPair;
}
//公钥加密
public static byte[] encrypt(byte[] content, ECPublicKey pubKey) throws Exception {
Cipher cipher = Cipher.getInstance("ECIES", "BC");
cipher.init(Cipher.ENCRYPT_MODE, pubKey);
return cipher.doFinal(content);
}
//私钥解密
public static byte[] decrypt(byte[] content, ECPrivateKey priKey) throws Exception {
Cipher cipher = Cipher.getInstance("ECIES", "BC");
cipher.init(Cipher.DECRYPT_MODE, priKey);
return cipher.doFinal(content);
}
}
国密算法
![432d7d04ad69168a35d4a8970e0208cd.png](https://i-blog.csdnimg.cn/blog_migrate/918c9fc7f0a04dfa7d6a43c4e8c12f8b.png)
-
SM1对称密码(不公开)
SM1 算法是分组对称密码算法 ,分组长度为128位,密钥长度都为 128 比特 ,算法安全保密强度及相关软硬件实现性能与 AES 相当,算法不公开,仅以IP核的形式存在于芯片中。采用该算法已经研制了系列芯片、智能IC卡、智能密码钥匙、加密卡、加密机等安全产品,广泛应用于电子政务、电子商务及国民经济的各个应用领域(包括国家政务通、警务通等重要领域)。
1. SM2
SM2算法和RSA算法都是公钥密码算法,SM2算法是一种更先进安全的算法.
随着密码技术和计算机技术的发展,目前常用的1024位RSA算法面临严重的安全威胁,
我们国家密码管理部门经过研究,决定采用SM2椭圆曲线算法替换RSA算法。
SM2椭圆曲线公钥密码算法是我国自主设计的**公钥密码算法 ,包括SM2-1椭圆曲线数字签名算法,SM2-2椭圆曲线密钥交换协议,SM2-3椭圆曲线公钥加密算法,分别用于实现数字签名密钥协商和数据加密等功能。SM2算法与RSA算法不同的是,SM2算法是基于椭圆曲线上点群离散对数难题,相对于RSA算法,256位的SM2密码强度已经比2048位的RSA密码强度要高。**
类型
非对称加密
算法特点
SM2性能更优更安全:密码复杂度高、处理速度快、机器性能消耗更小
![cdda5bad2f92249ec4d1d36309c189cb.png](https://i-blog.csdnimg.cn/blog_migrate/c661823295c4ccdbd1eb077d732d7011.png)
用途
![228f290fd4232b2e07c30c41caf0cc41.png](https://i-blog.csdnimg.cn/blog_migrate/ae65974f029f364ec30b070d9e9491b8.png)
加解密 Demo
package com.lett.asyenc.improve;
import com.sun.org.apache.xerces.internal.impl.dv.util.Base64;
import org.bouncycastle.jce.provider.BouncyCastleProvider;
import javax.crypto.Cipher;
import java.security.*;
import java.security.spec.PKCS8EncodedKeySpec;
import java.security.spec.X509EncodedKeySpec;
import java.util.HashMap;
import java.util.Map;
public class Sm2Test {
/**
* Integer 1为公钥 0为私钥
* String 公私钥
*/
private static Map<Integer,Key> keyMap = new HashMap<>();
private static final String algorithm = "SM2";
private static final Integer keySize = 256;
static {
Security.addProvider(new BouncyCastleProvider());
}
public static void main(String[] args) throws Exception{
String string = "1234123456781234567812345678123456781234567812345678123456785678123456781234567812345678123456781234567812345678";
getKeyPair();
String s = rsaEncrypt(string);
System.out.println(s);
String result = rsaDecrypt(s);
System.out.println(result);
}
/**
* 生成密钥对
* @throws Exception
*/
public static void getKeyPair() throws Exception{
KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("EC", "BC");
keyPairGenerator.initialize(keySize);
KeyPair keyPair = keyPairGenerator.generateKeyPair();
byte[] pri = keyPair.getPrivate().getEncoded();
byte[] pub = keyPair.getPublic().getEncoded();
KeyFactory keyFactory = KeyFactory.getInstance("EC", "BC");
PrivateKey privateKey = keyFactory.generatePrivate(new PKCS8EncodedKeySpec(pri));
PublicKey publicKey = keyFactory.generatePublic(new X509EncodedKeySpec(pub));
keyMap.put(0,privateKey);
keyMap.put(1,publicKey);
}
/**
* Sm2 加密
* @param string 原始数据
* @return 密文
* @throws Exception
*/
public static String rsaEncrypt(String string) throws Exception {
Cipher rsa = Cipher.getInstance(algorithm,"BC");
rsa.init(Cipher.PUBLIC_KEY,keyMap.get(1));
byte[] result = rsa.doFinal(string.getBytes());
System.out.println("密文长度:"+result.length*8);
String encode = Base64.encode(result);
return encode;
}
/**
* SM2 解密
* @param string 密文
* @return 原始数据
* @throws Exception
*/
public static String rsaDecrypt(String string) throws Exception{
Cipher rsa = Cipher.getInstance(algorithm,"BC");
//密文base64解码
byte[] decode = Base64.decode(string);
rsa.init(Cipher.PRIVATE_KEY,keyMap.get(0));
byte[] result = rsa.doFinal(decode);
System.out.println("原文长度:"+result.length);
return new String(result);
}
}
2. SM3
SM3杂凑算法是我国自主设计的密码**杂凑算法 ,适用于商用密码应用中的数字签名和验证消息认证码的生成与验证以及随机数的生成,可满足多种密码应用的安全需求。为了保证杂凑算法的安全性,其产生的杂凑值的长度不应太短**,例如MD5输出128比特杂凑值,输出长度太短,影响其安全性SHA-1算法的输出长度为160比特,SM3算法的输出长度为256比特 ,因此SM3算法的安全性要高于MD5算法和SHA-1算法。
类型
国产杂凑算法,摘要算法
算法特点
一个理想的密码散列函数应该有四个主要的特性:
-
对于任何一个给定的消息,它都很容易就能运算出散列数值。
-
难以由一个已知的散列数值,去推算出原始的消息。
-
在不更动散列数值的前提下,修改消息内容是不可行的。
-
对于两个不同的消息,它不能给与相同的散列数值。
用途
商用密码体系中,SM3主要用于数字签名及验证,消息认证码生成及验证、随机数生成等,其算法公开。
据国家密码管理局表示,其安全性及效率与SHA-256相当。
Demo
package com.lett.digest;
import com.sun.org.apache.xerces.internal.impl.dv.util.Base64;
import org.bouncycastle.crypto.digests.SM3Digest;
import org.bouncycastle.jce.provider.BouncyCastleProvider;
import java.security.MessageDigest;
/**
* 国产SM3摘要算法,使用BC
* @author lett
* @date 2020/07/30 16:47
*/
public class SM3Demo {
public static void main(String[] args) throws Exception{
String msg = "hello world!dsdsddasdasdads";
byte[] bytes = sm3(msg.getBytes());
System.out.println("SM3摘要长度:"+bytes.length*8);
System.out.println(Base64.encode(bytes));
byte[] digest = sm3JDK(msg.getBytes());
System.out.println("Sm3摘要长度:"+digest.length*8);
System.out.println(Base64.encode(digest));
}
/**
* 直接使用BC
* @param data 原始数据
* @return 256位的摘要数据
*/
public static byte[] sm3(byte[] data){
SM3Digest sm3Digest = new SM3Digest();
sm3Digest.update(data,0,data.length);
byte[] result = new byte[sm3Digest.getDigestSize()];
//摘要
sm3Digest.doFinal(result,0);
return result;
}
/**
* BC结合JDK 实现SM3
* @param data
* @return
*/
public static byte[] sm3JDK(byte[] data) throws Exception{
MessageDigest sm3 = MessageDigest.getInstance("SM3", new BouncyCastleProvider());
byte[] digest = sm3.digest(data);
return digest;
}
}
3. SM4
SM4分组密码算法是我国自主设计的分组对称密码算法 ,用于实现数据的加密/解密运算,以保证数据和信息的机密性。要保证一个对称密码算法的安全性的基本条件是其具备足够的密钥长度,SM4算法与AES算法具有相同的密钥长度分组长度128比特 ,因此在安全性上高于3DES算法。
类型
对称加密算法
Demo
package com.lett.symenc;
import org.bouncycastle.jce.provider.BouncyCastleProvider;
import org.bouncycastle.util.encoders.Base64;
import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import java.security.SecureRandom;
import java.security.Security;
public class SM4Demo {
static {
Security.addProvider(new BouncyCastleProvider());
}
public static final String SM4 = "SM4";
/**
* 加密/解密算法/工作模式/填充方式
* ECB CBC OFB CFB CTR
* NOPadding 模式下,明文必须是128位或者倍数 --->分组加密
*/
public static final String CIPHER_ALGORITHM="SM4/OFB/PKCS5Padding";
public static void main(String[] args) throws Exception {
String msg = "hello world!";
byte[] key = initKey();
System.out.println("密钥长度:"+key.length*8);
IvParameterSpec iv = getByteIv();
byte[] encrypted = sm4Encrypt(msg.getBytes(), key,iv);
System.out.println(new String(Base64.encode(encrypted)));
byte[] decrypted = decrypted(encrypted, key,iv);
System.out.println(new String(decrypted));
}
public static byte[] initKey() throws Exception{
KeyGenerator keyGenerator = KeyGenerator.getInstance(SM4);
System.out.println(keyGenerator.getProvider().getName());
keyGenerator.init(128);
SecretKey key = keyGenerator.generateKey();
return key.getEncoded();
}
public static byte[] sm4Encrypt(byte[] data,byte[] key,IvParameterSpec ivParameterSpec) throws Exception{
SecretKeySpec secretKeySpec = new SecretKeySpec(key,SM4);
Cipher cipher = Cipher.getInstance(CIPHER_ALGORITHM);
cipher.init(Cipher.ENCRYPT_MODE,secretKeySpec,ivParameterSpec);
byte[] bytes = cipher.doFinal(data);
return bytes;
}
public static byte[] decrypted(byte[] data,byte[] secretKey,IvParameterSpec ivParameterSpec) throws Exception{
SecretKeySpec secretKeySpec = new SecretKeySpec(secretKey,SM4);
Cipher cipher = Cipher.getInstance(CIPHER_ALGORITHM);
cipher.init(Cipher.DECRYPT_MODE,secretKeySpec,ivParameterSpec);
byte[] bytes = cipher.doFinal(data);
return bytes;
}
public static IvParameterSpec getByteIv(){
byte[] bytes = new SecureRandom().generateSeed(16);
return new IvParameterSpec(bytes);
}
}
消息认证
![9a6dd2d10e1caed1ceef9cf58ef2df70.png](https://i-blog.csdnimg.cn/blog_migrate/9b1c515f8b42d0c2f936e9a5040f38e9.png)
杂凑算法
又称单向散列函数。消息摘要是把任意长度的输入揉和而产生长度固定的伪随机输入的算法。单向散列函数的输出值也成为数字摘要或者指纹。
算法原理
散列函数
算法特点
①无论输入的消息有多长,计算出来的消息摘要的长度总是固定的。
例如应用MD5算法摘要的消息有128个比特位,用SHA-1算法摘要的消息最终有160比特位的输出,SHA-1的变体可以产生192比特位和256比特位的消息摘要。一般认为,摘要的最终输出越长,该摘要算法就越安全。
②消息摘要看起来是“随机的”。这些比特看上去是胡乱的杂凑在一起的。
可以用大量的输入来检验其输出是否相同,一般,不同的输入会有不同的输出,而且输出的摘要消息可以通过随机性检验。但是,一个摘要并不是真正随机的,因为用相同的算法对相同的消息求两次摘要,其结果必然相同;而若是真正随机的,则无论如何都是无法重现的。因此消息摘要是“伪随机的”。
③一般地,只要输入的消息不同,对其进行摘要以后产生的摘要消息也必不相同;但相同的输入必会产生相同的输出。
这正是好的消息摘要算法所具有的性质:输入改变了,输出也就改变了;两条相似的消息的摘要确不相近,甚至会大相径庭。
④消息摘要函数是无陷门的单向函数,即只能进行正向的信息摘要,而无法从摘要中恢复出任何的消息,甚至根本就找不到任何与原信息相关的信息。
当然,可以采用强力攻击的方法,即尝试每一个可能的信息,计算其摘要,看看是否与已有的摘要相同,如果这样做,最终肯定会恢复出摘要的消息。但实际上,要得到的信息可能是无穷个消息之一,所以这种强力攻击几乎是无效的。
⑤好的摘要算法,没有人能从中找到“碰撞”,虽然“碰撞”是肯定存在的。即对于给定的一个摘要,不可能找到一条信息使其摘要正好是给定的。或者说,无法找到两条消息,使它们的摘要相同。
缺点:
无法保证数据的真实性,即不能确定数据和散列值是来自发送方的,因为攻击者完全可以将数据和散列值一起替换。
常见算法
MD系列,SHA系列,国密SM3
应用
-
检测数据的完整性(一致性)
-
配合数据签名一起使用。
几乎所有的数字签名方案都要和快速高效的摘要算法(Hash函数)一起使用,当公钥算法与摘要算法结合起来使用时,便构成了一种有效地数字签名方案。
Demo
package com.lett.digest;
import com.sun.org.apache.xerces.internal.impl.dv.util.Base64;
import org.bouncycastle.jce.provider.BouncyCastleProvider;
import org.bouncycastle.util.encoders.Hex;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.security.Security;
public class MD5Demo {
public static final String algorithm = "SHA256";
public static void main(String[] args) {
Security.addProvider(new BouncyCastleProvider());
String input = "举头忘明月,低头思故乡";
byte[] bytes = input.getBytes();
//不同的编码汉字的字节数不同 utf-8 一般是3个字节
System.out.println("==>明文长度"+bytes.length+"字节");
MD5(bytes);
MD2(bytes);
MD4(bytes);
SHA(bytes);
}
public static void MD5(byte[] bytes){
try {
//得到MD5加密对象
MessageDigest md5 = MessageDigest.getInstance("MD5");
byte[] digest = md5.digest(bytes);
print(md5,digest);
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
}
}
public static void MD2(byte[] bytes){
try {
//得到MD5加密对象
MessageDigest md = MessageDigest.getInstance("MD2");
byte[] digest = md.digest(bytes);
print(md,digest);
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
}
}
public static void MD4(byte[] bytes){
// MD4Digest md4Digest = new MD4Digest();
// md4Digest.update(bytes,0,bytes.length);
// byte[] output = new byte[md4Digest.getDigestSize()];
// md4Digest.doFinal(output,0);
// System.out.println("MD4密文:"+Hex.toHexString(output));
try {
MessageDigest md = MessageDigest.getInstance("MD4","BC");
byte[] digest = md.digest(bytes);
print(md,digest);
} catch (Exception e) {
e.printStackTrace();
}
}
public static void SHA(byte[] bytes){
try {
MessageDigest md = MessageDigest.getInstance(algorithm,"BC");
byte[] digest = md.digest(bytes);
print(md,digest);
} catch (Exception e) {
e.printStackTrace();
}
}
public static void print(MessageDigest messageDigest,byte[] digest){
String name = messageDigest.getProvider().getName();
String algorithm = messageDigest.getAlgorithm();
System.out.println(name+"----> "+algorithm);
System.out.println(algorithm+"密文转Base64:"+Base64.encode(digest));
System.out.println(algorithm+"密文转16进制:"+ Hex.toHexString(digest));
System.out.println(algorithm+"密文长度:"+digest.length*8);
System.out.println();
}
}
![d15e2dcd6da75f9226a4a3539a0ee532.png](https://i-blog.csdnimg.cn/blog_migrate/fbb910c265f151b79951af085f00233b.png)
![4bfd1f39a4cd0788985c4ac8b9efcc9d.png](https://i-blog.csdnimg.cn/blog_migrate/855ce5d2e395e023ab03d1011ea4faab.png)
![a60059fd02de767a5296c2e68f358412.png](https://i-blog.csdnimg.cn/blog_migrate/e7257fbcbc0e1032ad0165b72fd4ee38.png)
![9dac286b0e4f796999d31fbeb592cee2.png](https://i-blog.csdnimg.cn/blog_migrate/65d0aaec872f5f5205cd54d4d4b5bbc2.png)
消息认证码
在密码学中,消息认证码(英语:Message authentication code,缩写为MAC),又译为消息鉴别码、文件消息认证码、讯息鉴别码、信息认证码,是经过特定算法后产生的一小段信息,检查某段消息的完整性以及作身份验证。它可以用来检查在消息传递过程中,其内容是否被更改过,不管更改的原因是来自意外或是蓄意攻击。同时可以作为消息来源的身份验证,确认消息的来源。
![238b425836f288b00e2157793830d31c.png](https://i-blog.csdnimg.cn/blog_migrate/ec710faeb10980d45ce52ccce01551ed.png)
![99d6267df4e6a2d4941214447569b156.png](https://i-blog.csdnimg.cn/blog_migrate/5a69db0315069006fbc3be4ad4a2be27.png)
算法特点
消息认证码可以简单理解为一种与密钥相关的单向散列函数。
优点:
可以保证数据的完整性和真实性。
缺点:
接收方虽然可以确定消息的完整性和真实性,解决篡改和伪造消息的问题,但不能防止A否认发送过消息。
Demo
package com.lett.digest.improve;
import org.bouncycastle.jce.provider.BouncyCastleProvider;
import javax.crypto.KeyGenerator;
import javax.crypto.Mac;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
import java.security.SecureRandom;
import java.security.Security;
import java.util.Base64;
public class MacDemo {
/**
* HmacSHA1
* HmacSHA256
* HmacMD5
*
* Mac: AESCMAC
* Mac: AESCCMMAC
* Mac: AES-GMAC
* KeyGenerator: AES-GMAC
* Mac: ARIA-GMAC
* KeyGenerator: ARIA-GMAC
*Mac: HMACSHA384
* KeyGenerator: HMACSHA384
* Mac: OLDHMACSHA512
* Mac: PBEWITHHMACSHA512
* Mac: HMACSHA512
* KeyGenerator: HMACSHA512
* Mac: HMACSHA512/224
* KeyGenerator: HMACSHA512/224
* Mac: HMACSHA512/256
* KeyGenerator: HMACSHA512/256
* Mac: HMACSHA3-224
* KeyGenerator: HMACSHA3-224
* Mac: HMACSHA3-256
* KeyGenerator: HMACSHA3-256
* Mac: HMACSHA3-384
* KeyGenerator: HMACSHA3-384
* Mac: HMACSHA3-512
* KeyGenerator: HMACSHA3-512
* Mac: HMACSkein-256-128
*
* BC ----->密钥长度
* SM4-CMAC 128
*/
private static final String algorithm = "AES-GMAC";
public static void main(String[] args) {
Security.addProvider(new BouncyCastleProvider());
String string ="123456781234567812345678123456781234567812345678123456781234567812345678123456781234567812345678";
try {
String enc = enc(string, getSecretKeySpec());
System.out.println(enc);
} catch (Exception e) {
e.printStackTrace();
}
}
public static SecretKeySpec getSecretKeySpec() throws Exception{
//一个秘密(对称)密钥生成器
KeyGenerator keyGenerator = KeyGenerator.getInstance(algorithm,"BC");
//keyGenerator.init(128,new SecureRandom());
//产生密钥
SecretKey secretKey = keyGenerator.generateKey();
//密钥字节数组
byte[] key = secretKey.getEncoded();
System.out.println("密钥长度:" + key.length * 8);
//将密钥规范
SecretKeySpec secretKeySpec = new SecretKeySpec(key, algorithm);
return secretKeySpec;
}
public static String enc(String string,SecretKeySpec secretKeySpec) throws Exception{
Mac mac = Mac.getInstance(algorithm, "BC");
System.out.println(mac.getAlgorithm());
mac.init(secretKeySpec);
byte[] bytes = mac.doFinal(string.getBytes());
return Base64.getEncoder().encodeToString(bytes);
}
}
数字签名
Demo
-
SM2 数字签名
package com.lett.sign;
import com.sun.org.apache.xml.internal.security.utils.Base64;
import org.bouncycastle.jce.provider.BouncyCastleProvider;
import java.security.*;
public class SignTest {
/**
* 密钥的获取,算法名称是EC
* "SHA1WithSM2"
* "SHA224WithSM2"
* "SHA256WithSM2"
* "SM3WithSM2"
*/
private static final String algorithmKey = "EC";
private static final String algorithm = "SHA256WITHSM2";
private static final Integer keySize = 256;
static {
Security.addProvider(new BouncyCastleProvider());
}
public static void main(String[] args) throws Exception {
//初始化密钥
KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance(algorithmKey);
keyPairGenerator.initialize(keySize);
KeyPair keyPair = keyPairGenerator.generateKeyPair();
String msg = "hello";
//进行签名
String s = sign(msg.getBytes(), keyPair.getPrivate());
System.out.println(s);
//验签
boolean b = verifySign(msg.getBytes(), keyPair.getPublic(),Base64.decode(s) );
System.out.println(b);
}
//数字签名算法
//一般是要对原始数据进行摘要 然后非对称私钥加密
public static String sign(byte[] data, PrivateKey privateKey) throws Exception{
Signature sha1withRSA = Signature.getInstance(algorithm,"BC");
sha1withRSA.initSign(privateKey);
sha1withRSA.update(data);
byte[] sign = sha1withRSA.sign();
return Base64.encode(sign);
}
//验签
public static boolean verifySign(byte[] data, PublicKey publicKey,byte[] singed) throws Exception{
Signature sha1withRSA = Signature.getInstance(algorithm,"BC");
sha1withRSA.initVerify(publicKey);
sha1withRSA.update(data);
boolean verify = sha1withRSA.verify(singed);
return verify;
}
}
package com.lett.sign;
import com.sun.org.apache.xml.internal.security.utils.Base64;
import org.bouncycastle.jce.provider.BouncyCastleProvider;
import java.security.*;
public class SignTest {
/**
* "SHA1WithRSA"
* "SHA224WithRSA"
* "SHA256WithRSA"
* "SHA384WithRSA"
* "SHA512WithRSA"
* "MD2WithRSA"
* "MD4WithRSA"
* "MD5WithRSA"
*/
private static final String algorithmKey = "RSA";
private static final String algorithm = "SHA1WithRSA";
/**
* Rsa 的密钥长度1024起
*/
private static final Integer keySize = 1024;
static {
Security.addProvider(new BouncyCastleProvider());
}
public static void main(String[] args) throws Exception {
//初始化密钥
KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance(algorithmKey);
keyPairGenerator.initialize(keySize);
KeyPair keyPair = keyPairGenerator.generateKeyPair();
String msg = "hello";
//进行签名
String s = sign(msg.getBytes(), keyPair.getPrivate());
System.out.println(s);
//验证
boolean b = verifySign(msg.getBytes(), keyPair.getPublic(),Base64.decode(s) );
System.out.println(b);
}
//数字签名算法
//一般是要对原始数据进行摘要 然后使用非对称私钥加密
public static String sign(byte[] data, PrivateKey privateKey) throws Exception{
Signature sha1withRSA = Signature.getInstance(algorithm,"BC");
sha1withRSA.initSign(privateKey);
sha1withRSA.update(data);
byte[] sign = sha1withRSA.sign();
return Base64.encode(sign);
}
//验证
public static boolean verifySign(byte[] data, PublicKey publicKey,byte[] singed) throws Exception{
Signature sha1withRSA = Signature.getInstance(algorithm,"BC");
sha1withRSA.initVerify(publicKey);
sha1withRSA.update(data);
boolean verify = sha1withRSA.verify(singed);
return verify;
}
}