package com.doubleca.android.sample.gmjce;
import com.doubleca.b146.c16.util.encoders.Base64;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.math.BigInteger;
import java.security.InvalidKeyException;
import java.security.Key;
import java.security.KeyFactory;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.KeyStore;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.security.SecureRandom;
import java.security.SignatureException;
import java.security.cert.Certificate;
import java.security.cert.CertificateFactory;
import java.security.cert.X509Certificate;
import java.security.spec.InvalidKeySpecException;
import java.security.spec.PKCS8EncodedKeySpec;
import java.security.spec.X509EncodedKeySpec;
import java.util.Enumeration;
import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import doubleca.security.provider.DoubleCA;
import doubleca.security.provider.jdk7.sm4.SM4KeySpec;
public class GmTest
{
private static final String SIGNATURE_KEY_ALGORITHM = "SM2";
private static final String SIGNATURE_ALGORITHM = "SM3withSM2";
private static final int SIGNATURE_KEY_SIZE = 256;
private static final String DIGEST_ALGORITHM = "SM3";
private static final String CIPHER_SM4_KEY_ALGORITHM = "SM4";
// private static final String CIPHER_SM4_ALGORITHM = "SM4/ECB/PKCS5Padding";
private static final String CIPHER_SM4_ALGORITHM = "SM4/CBC/PKCS5Padding";
// private static final String CIPHER_SM4_ALGORITHM = "SM4/ECB/NOPadding";
// private static final String CIPHER_SM4_ALGORITHM = "SM4/CBC/NOPadding";
private static final String CIPHER_SM2_ALGORITHM = "SM2/NONE/NOPadding";
public static SecretKey TestSM4Cipher()
{
try
{
String plainText1 = "软件(库)授权与防复制、防盗版,全平台支持:";
String plainText2 = "https://www.PPLIC.com ";
byte[] plain1 = plainText1.getBytes();
byte[] plain2 = plainText2.getBytes();
SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(CIPHER_SM4_KEY_ALGORITHM, DoubleCA.PROVIDER_NAME);
SM4KeySpec keySpec = new SM4KeySpec("1234567812345678".getBytes("utf-8"));
IvParameterSpec iv = new IvParameterSpec("1234567812345678".getBytes());
SecretKey key = keyFactory.generateSecret(keySpec);
BigInteger keyBuffer = new BigInteger(1, key.getEncoded());
System.out.println("SM4 Key :" + keyBuffer.toString(16).toUpperCase());
// KeyGenerator kgen = KeyGenerator.getInstance("OID.1.2.156.10197.1.104");
// KeyGenerator kgen = KeyGenerator.getInstance("1.2.156.10197.1.104");
// KeyGenerator kgen = KeyGenerator.getInstance("SMS4");
KeyGenerator kgen = KeyGenerator.getInstance(CIPHER_SM4_KEY_ALGORITHM);
kgen.init(128, new SecureRandom("12345678".getBytes()));
SecretKey secretKey = kgen.generateKey();
byte[] enCodeFormat = secretKey.getEncoded();
SecretKeySpec sm4keySpec = new SecretKeySpec(enCodeFormat, CIPHER_SM4_KEY_ALGORITHM);
keyBuffer = new BigInteger(1, sm4keySpec.getEncoded());
System.out.println("SM4KeySpec :" + keyBuffer.toString(16).toUpperCase());
Cipher cipher = Cipher.getInstance(CIPHER_SM4_ALGORITHM);// 创建密码器
// cipher.init(Cipher.ENCRYPT_MODE, sm4keySpec);// 初始化
cipher.init(Cipher.ENCRYPT_MODE, sm4keySpec, iv);// 初始化
byte[] sm4ResultBuffer = new byte[cipher.getOutputSize(plain1.length + plain2.length)];
int bufferLen = cipher.update(plain1, 0, plain1.length, sm4ResultBuffer, 0);
bufferLen += cipher.update(plain2, 0, plain2.length, sm4ResultBuffer, bufferLen);
bufferLen += cipher.doFinal(sm4ResultBuffer, bufferLen);
byte[] result = new byte[bufferLen];
System.arraycopy(sm4ResultBuffer, 0, result, 0, sm4ResultBuffer.length);
BigInteger buffer = new BigInteger(1, result);
System.out.println("原文1:" + plainText1);
System.out.println("原文2:" + plainText2);
System.out.println("密文:" + new String(Base64.encode(result)));
// cipher.init(Cipher.DECRYPT_MODE, sm4keySpec);// 初始化
cipher.init(Cipher.DECRYPT_MODE, sm4keySpec, iv);// 初始化
sm4ResultBuffer = new byte[cipher.getOutputSize(result.length)];
bufferLen = cipher.update(result, 0, result.length / 2, sm4ResultBuffer, 0);
bufferLen += cipher.update(result, result.length / 2, result.length - (result.length / 2), sm4ResultBuffer, bufferLen);
bufferLen += cipher.doFinal(sm4ResultBuffer, bufferLen);
result = new byte[bufferLen];
System.arraycopy(sm4ResultBuffer, 0, result, 0, result.length);
System.out.println("原文:" + new String(result));
return key;
}
catch (Exception ex)
{
ex.printStackTrace();
return null;
}
}
public static boolean TestSM3Digest()
{
try
{
MessageDigest md = MessageDigest.getInstance(DIGEST_ALGORITHM);
byte[] result;
md.update("大宝CA 国密SSL(v1.1)算法库:https://www.DoubleCA.com ".getBytes());
// byte[] result = md.digest("1234512345".getBytes());
result = md.digest();
System.out.println(md.getAlgorithm() + " MessageDigest : " + new BigInteger(1, result).toString(16).toUpperCase());
return true;
}
catch (Exception ex)
{
ex.printStackTrace();
return false;
}
}
public static boolean TestSM2Signature(KeyPair key) throws NoSuchAlgorithmException, SignatureException, InvalidKeyException
{
String content = "大宝CA 国密SSL(v1.1)Tomcat:https://www.DoubleCA.com ";
System.out.println("原文:" + content);
java.security.Signature signature = java.security.Signature.getInstance(SIGNATURE_ALGORITHM);
signature.initSign(key.getPrivate());
signature.update(content.getBytes());
byte[] signValue = signature.sign();
System.out.println("签名值:" + new String(Base64.encode(signValue)));
signature.initVerify(key.getPublic());
signature.update(content.getBytes());
boolean result = signature.verify(signValue);
System.out.println("签名验证结果 :" + result);
return result;
}
public static boolean TestSM2AsymmetricCipher(KeyPair key)
{
try
{
Cipher cipher = Cipher.getInstance(CIPHER_SM2_ALGORITHM);
cipher.init(Cipher.ENCRYPT_MODE, key.getPublic());
String plainText = "软件(库)授权与防复制:https://www.PPLIC.com ";
System.out.println("原文:" + plainText);
cipher.update(plainText.getBytes());
byte[] cipherByte = cipher.doFinal();
System.out.println("密文:" + new String(Base64.encode(cipherByte)));
cipher.init(Cipher.DECRYPT_MODE, key.getPrivate());
byte[] plainByte = cipher.doFinal(cipherByte);
System.out.println("原文:" + new String(plainByte));
return true;
}
catch (Exception ex)
{
ex.printStackTrace();
return false;
大宝CA国密SSL国密TOMCAT
- 粉丝: 66
- 资源: 16
最新资源
- 多微网优化模型matlab 采用粒子群算法分析两个微网的优化调度,得到蓄电池,发电机以及微网间功率传输,程序有参考资料
- 潮流计算程序matlab 牛拉法 采用matlab对9节点进行潮流计算,采用牛拉法,程序运行可靠
- 微网优化调度matlab 采用matlab+yalmip编制含分布式和储能的微网优化模型,程序采用15分钟为采集节点,利用cplex求解,程序考虑发电机的启停约束,程序运行可靠
- PMSM永磁同步电机仿真三电平SVPWM矢量控制matlab PMSM双环矢量控制传统三电平
- 路径规划人工势场法以及改进人工势场法matlab代码,包含了
- MobaXterm 是一款功能强大且实用的终端仿真器软件.docx
- 三菱FX3U底层源码,PLSR源码 总体功能和指令可能支持在RUN中下载程序,支持注释的写入和读取,支持脉冲输出与定位指令(包括PLSY PWM PLSR PLSV DRVI DRVA 等指令
- Oracle Database Gateways for Win32-11gR2
- python071基于RSA加密算法软件的研究设计
- 成熟量产低压无刷伺服驱动 方案 全套软硬件资料 源码 原理图 需要的直接拿 基于28035平台
- 欧姆龙PLC ST语言6轴伺服RS232C通讯板CP1W-C IF0 真实项目程序,ST语言写的FB块 PLC用是两台CP1H-X40DT-D配置4块RS232C通讯板CP1W-CIF01 触摸屏是N
- 欧姆龙CP1H与力士乐VFC-x610变频器通讯程序功能:原创程序,可直接用于现场程序 欧姆龙CP1H的CIF11通讯板,实现对力士乐VFC-x610变频器 设定频率,控制正反转,读取实际频率,读取
- 级联型电力电子变压器,高压直流MMC控制系统,级联数可选,调 制方式有移相载波,nlm及混合调制,拥有冒泡排序,递归排序等方法,可控制三相不平衡与环流
- 西门子PLC双轴定位算法电池焊接控制程序-S7-1200 +威纶通触摸屏 S7-1200PLC做的电池焊接程序,电池包里面有n*m行列个电池 程序设计灵活SCL语言+梯形图,采用了位置试教与定位路径规
- 变压器副边突然短路simulink仿真
- MATLAB代码:基于模型预测控制的楼宇负荷需求响应研究 关键词:楼宇负荷 空调 模型预测控制 需求响应 仿真平台:MATLAB+CVX平台 主要内容:代码主要做的是一个建筑楼宇的需求响应问题,首
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈