增加二维码、条形码签名验签测试

This commit is contained in:
yihzhou
2024-11-21 10:56:01 +08:00
parent 25803c9d4f
commit db80b1dd83
17 changed files with 1353 additions and 68 deletions
@@ -68,6 +68,11 @@ public class SydApiException extends RuntimeException{
break;
}
case 0x8004 : {
msg = "证书已过期";
break;
}
case 32770:
msg = "签名验证错误(P7) ";
break;
@@ -151,6 +151,8 @@ public interface SydCertApi {
RetWrap importCert(byte[] certData, byte[] organizationId, CertUsage... us);
RetWrap importCert(byte[] certData, byte[] organizationId);
String importCert(String cert);
/**
* 从加密设备中删除指定 DN 的公钥证书
@@ -240,6 +240,9 @@ public interface SydGenApi {
*/
RetWrap generateDEByDn(String certSerial, byte[] oData);
RetWrap generateDEByDnICBC(String certSerial,String iv, byte[] oData,int roundmod);
RetWrap generateDEByDnRSA(int alg,String certSerial, byte[] oData);
/**
@@ -323,6 +326,8 @@ public interface SydGenApi {
* orgCode 如果有机构码的话会返回机构码
*/
RetWrap decryptDEByDN(String certSerial, byte[] pCipherKey);
RetWrap decryptDEByDNICBC(String certSerial, String IV,String pCipherKey,int roundmod);
RetWrap decryptDEByDNRSA(int alg,String certSerial, byte[] pCipherKey);
/**
* 接收用私钥解密数字信封,还原对称密钥。
+23 -2
View File
@@ -46,12 +46,14 @@ public class CertUtil {
return (X509Certificate) CF.generateCertificate(new ByteArrayInputStream(certificateString.getBytes())); // 将文件流的证书转化为证书类
}
public static String convertToPem(X509Certificate cert) throws CertificateEncodingException {
public static String convertToPem(String cert) throws CertificateEncodingException {
String cert_begin = "-----BEGIN CERTIFICATE-----\n";
String end_cert = "\n-----END CERTIFICATE-----";
byte[] derCert = cert.getEncoded();
// byte[] derCert = cert.getEncoded();
byte[] derCert = cert.getBytes();
String pemCertPre = new String(Base64.encode(derCert, true));
String pemCert = cert_begin + pemCertPre + end_cert;
return pemCert;
@@ -98,6 +100,25 @@ public class CertUtil {
return signAlg.toUpperCase().indexOf("SM2") >= 0;
}
public static String getDN(String cert) {
String pemCert;
if(cert.contains("-----BEGIN CERTIFICATE-----")){
pemCert =cert;
}else {
String cert_begin = "-----BEGIN CERTIFICATE-----\n";
String end_cert = "\n-----END CERTIFICATE-----";
pemCert = cert_begin + cert + end_cert;
}
X509Certificate certificate = null;
try {
certificate = convertToX509Cert(pemCert);
} catch (Exception e) {
throw new SydApiException( 0xC101, e );
}
return certificate.getSubjectDN().toString();
}
/**
* 判断 p7 是否是国密证书签的
*
@@ -0,0 +1,140 @@
package com.sunyard.util;
import com.google.zxing.*;
import com.google.zxing.client.j2se.BufferedImageLuminanceSource;
import com.google.zxing.client.j2se.MatrixToImageWriter;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.common.HybridBinarizer;
import com.google.zxing.qrcode.QRCodeReader;
import com.sunyard.SydApiException;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.Hashtable;
public class QRCodeUtil {
public static void genQRCode(String data,String filepath){
// 图片的尺寸(宽度和高度)
int width = 300;
int height = 300;
// 设置二维码的相关参数
Hashtable<EncodeHintType, Object> hintMap = new Hashtable<>();
hintMap.put(EncodeHintType.MARGIN, 1); // 设置二维码的空白边距
try {
// 创建二维码矩阵
BitMatrix bitMatrix = new MultiFormatWriter().encode(
data,
BarcodeFormat.QR_CODE,
width,
height,
hintMap
);
// 将二维码矩阵转换为图片,并保存
File outputFile = new File(filepath);
MatrixToImageWriter.writeToFile(bitMatrix, "PNG", outputFile);
} catch (Exception e) {
System.err.println("Error occurred while generating QR code: " + e.getMessage());
throw new SydApiException(1001,e);
}
}
public static void genBarCode(String data,String filepath){
// 图片的尺寸(宽度和高度)
int width = 300;
int height = 300;
// 设置二维码的相关参数
Hashtable<EncodeHintType, Object> hintMap = new Hashtable<>();
hintMap.put(EncodeHintType.MARGIN, 1); // 设置二维码的空白边距
try {
// 生成 Code 128 条形码
BitMatrix bitMatrix= new MultiFormatWriter().encode(
data,
BarcodeFormat.PDF_417,
width,
height,
hintMap
);
// 将二维码矩阵转换为图片,并保存
File outputFile = new File(filepath);
MatrixToImageWriter.writeToFile(bitMatrix, "PNG", outputFile);
} catch (Exception e) {
System.err.println("Error occurred while generating QR code: " + e.getMessage());
throw new SydApiException(1001,e);
}
}
public static String readQRCode(String filepath){
// 加载二维码图像
File qrCodeImage = new File(filepath);
// 读取二维码图像
BufferedImage bufferedImage = null;
try {
bufferedImage = ImageIO.read(qrCodeImage);
} catch (IOException e) {
throw new RuntimeException(e);
}
// 使用 ZXing 的 QRCodeReader 进行解码
QRCodeReader reader = new QRCodeReader();
LuminanceSource source = new BufferedImageLuminanceSource(bufferedImage);
BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source));
try {
// 解码二维码
Result result = reader.decode(bitmap);
// 提取二维码中的数据
return result.getText();
} catch (NotFoundException e) {
throw new SydApiException(1003,e);
} catch (ChecksumException e) {
throw new SydApiException(1004,e);
} catch (FormatException e) {
throw new SydApiException(1005,e);
}
}
public static String readBarCode(String filepath){
// 加载二维码图像
File BarCodeImage = new File(filepath);
try {
// 读取条形码图像
BufferedImage bufferedImage = ImageIO.read(BarCodeImage);
// 使用 ZXing 的 MultiFormatReader 进行解码
MultiFormatReader reader = new MultiFormatReader();
LuminanceSource source = new BufferedImageLuminanceSource(bufferedImage);
BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source));
// 解码条形码
Result result = reader.decode(bitmap);
// 提取条形码中的数据
return result.getText();
} catch (NotFoundException e) {
throw new SydApiException(1006,e);
} catch (IOException e) {
throw new SydApiException(1007,e);
}
}
}