导入BC库
// 导入库
static {
Security.addProvider(new BouncyCastleProvider());
}
查看算法
@Test
public void getAlgorithms() {
Security.getAlgorithms("Signature")//填入操作名
.forEach(System.out::println);
}
杂凑
// 杂凑
@Test
public void digest() throws Exception {
MessageDigest md = MessageDigest.getInstance("MD5", BouncyCastleProvider.PROVIDER_NAME);
byte[] digest = md.digest("lvtian".getBytes());
System.out.println(Hex.toHexString(digest));
md.update("lv".getBytes());
md.update("tian".getBytes());
byte[] digest1 = md.digest();
System.out.println(Hex.toHexString(digest1));
}
签名验签
// 签名
@Test
public void sign() throws Exception{
// 使用到非对称的时候 EC
KeyPairGenerator key = KeyPairGenerator.getInstance("EC", BouncyCastleProvider.PROVIDER_NAME);
// 非对称使用generateKeyPair
KeyPair keyPair = key.generateKeyPair();
byte[] inDate = "lvtian".getBytes();
Signature signature = Signature.getInstance(GMObjectIdentifiers.sm2sign_with_sm3.getId(), BouncyCastleProvider.PROVIDER_NAME);
// 先给私钥再update
signature.initSign(keyPair.getPrivate());
signature.update(inDate);
byte[] sign = signature.sign();
System.out.println(Hex.toHexString(sign));
// 验签
signature.initVerify(keyPair.getPublic());
signature.update("lvtian".getBytes());
System.out.println(signature.verify(sign));
}
对称加密
// 对称加密解密
@Test
public void symCipher() throws Exception{
KeyGenerator ky = KeyGenerator.getInstance("SM4", BouncyCastleProvider.PROVIDER_NAME);
// 对称使用generateKey
SecretKey key = ky.generateKey();
byte[] inDate = "lvtian".getBytes();
Cipher cipher = Cipher.getInstance("SM4", BouncyCastleProvider.PROVIDER_NAME);
cipher.init(Cipher.ENCRYPT_MODE,key);
byte[] bytes = cipher.doFinal(inDate);
System.out.println(Hex.toHexString(bytes));
cipher.init(Cipher.DECRYPT_MODE,key);
byte[] bytes1 = cipher.doFinal(bytes);
// System.out.println(new String(bytes1));
}
非对称加密解密
// 非对称
@Test
public void asymCipher() throws Exception{
KeyPairGenerator kpg = KeyPairGenerator.getInstance("EC", BouncyCastleProvider.PROVIDER_NAME);
KeyPair keyPair = kpg.generateKeyPair();
System.out.println(Hex.toHexString(keyPair.getPublic().getEncoded()));
byte[] encoded = keyPair.getPublic().getEncoded();
String s = new String(encoded);
String sNew = s;
byte[] bytes2 = sNew.getBytes();
System.out.println(encoded.equals(bytes2));
byte[] inDate = "lvtian".getBytes();
Cipher cipher = Cipher.getInstance("SM2", BouncyCastleProvider.PROVIDER_NAME);
cipher.init(Cipher.ENCRYPT_MODE, keyPair.getPublic());
byte[] bytes = cipher.doFinal(inDate);
// System.out.println(Hex.toHexString(bytes));
cipher.init(Cipher.DECRYPT_MODE, keyPair.getPrivate());
byte[] bytes1 = cipher.doFinal(bytes);
// System.out.println(bytes1.toString());
// System.out.println(new String(bytes1));
}
检验证书合法性
//测试证书检查合法
@Test
public void certificate(){
try {
//读取证书文件
FileInputStream fis = new FileInputStream("D:/_.csdn.net.crt");
FileInputStream fatherFile = new FileInputStream("D:/GeoTrust CN RSA CA G1.crt");
//加载证书文件
CertificateFactory factory = CertificateFactory.getInstance("X.509");
X509Certificate certificate = (X509Certificate) factory.generateCertificate(fis);
X509Certificate certificate1 = (X509Certificate) factory.generateCertificate(fatherFile);
certificate1.checkValidity();
//验证数字证书的有效性
certificate.checkValidity();
//获取证书颁发机构的公钥
PublicKey publicKey = certificate1.getPublicKey();
//密文
byte[] signature = certificate.getSignature();
//明文
byte[] inData = certificate.getTBSCertificate();
System.out.println(Hex.toHexString(publicKey.getEncoded()));
System.out.println(Hex.toHexString(signature));
System.out.println(Hex.toHexString(inData));
//获得签名算法
// String sigAlgName = certificate.getSigAlgName();
// System.out.println(sigAlgName);
// //验签
// Signature instance = Signature.getInstance(sigAlgName, BouncyCastleProvider.PROVIDER_NAME);
// instance.initVerify(publicKey);
// instance.update(inData);
// System.out.println(instance.verify(signature));
} catch (Exception e) {
throw new RuntimeException(e);
}
}
MAC杂凑
@Test
public void hmac() throws Exception{
KeyGenerator bcKG = KeyGenerator.getInstance("SM4", "BC");
SecretKey bcSK = bcKG.generateKey();
Mac bcMAC = Mac.getInstance("SM4-CMAC", "BC");
bcMAC.init(bcSK);
byte[] bytes = bcMAC.doFinal("lvtian".getBytes());
System.out.println(Arrays.toString(bytes));
}
初始化自己的公钥存入数据库 将私钥已文件的形式保存
/**
* 初始化系统密钥对
*/
@Override
public void initializeMyKey() {
try {
KeyPairGenerator keyGenerator = KeyPairGenerator.getInstance("EC", BouncyCastleProvider.PROVIDER_NAME);
KeyPair keyPair = keyGenerator.generateKeyPair();//生成密钥对
PublicKey keyPublic = keyPair.getPublic();
PrivateKey keyPrivate = keyPair.getPrivate();
//获取公钥二进制数组
byte[] encodedPub = keyPublic.getEncoded();
//编码为base64字符串
String pubKey = Base64.getEncoder().encodeToString(encodedPub);
//获取私钥二进制数组
// byte[] encodedPri = keyPrivate.getEncoded();
byte[] encodedPri = Base64Utils.encode(keyPrivate.getEncoded());
FileUtil.save2File(FILE_PATH, encodedPri);
Key key = new Key();
key.setPublicKey(pubKey);
keyDao.insert(key);
} catch (NoSuchAlgorithmException e) {
throw new RuntimeException(e);
} catch (NoSuchProviderException e) {
throw new RuntimeException(e);
}
}