package com.fzzy.api.utils;
|
|
|
import cn.lxem.scde.SM2Crypto;
|
import sun.misc.BASE64Encoder;
|
|
import javax.imageio.ImageIO;
|
|
import java.awt.image.BufferedImage;
|
import java.io.*;
|
|
@SuppressWarnings("restriction")
|
public class SM2Utils{
|
|
|
public static String encrypt(String publicKeyHex,String data){
|
return SM2Crypto.encrypt(publicKeyHex,data,2);
|
}
|
|
public static void main(String[] args) throws Exception {
|
|
// String p = SM2Crypto.encrypt("042DBA45E7B03394F603CADAFCDDEC854D3E01A4E9C52CD799B85B1A14BDB970137AE58BA553D79F058604DC1CD4B77DE5408BA3308E767584100C2B663510C819","LSJ12345678@",2);
|
// System.out.println("加密:"+p);
|
|
|
// Api1101 api1101 = new Api1101();
|
// api1101.setKqdm("11111");
|
// api1101.setZcrq(new Date());
|
// String js = JSON.toJSONString(api1101);
|
// System.out.println("json:"+js);
|
|
|
//将图片文件转化为字节数组字符串,并对其进行Base64编码处理
|
InputStream inputStream = null;
|
byte[] buffer = null;
|
//读取图片字节数组
|
try {
|
inputStream = new FileInputStream("C:\\Users\\75787\\Desktop\\image\\qiche.jpg");
|
int count = 0;
|
while (count == 0) {
|
count = inputStream.available();
|
}
|
buffer = new byte[count];
|
inputStream.read(buffer);
|
} catch (IOException e) {
|
//log.error(e.getMessage(),e);
|
} finally {
|
if (inputStream != null) {
|
try {
|
// 关闭inputStream流
|
inputStream.close();
|
} catch (IOException e) {
|
e.printStackTrace();
|
}
|
}
|
}
|
// // 对字节数组Base64编码
|
// return new BASE64Encoder().encode(buffer);
|
System.out.println(new BASE64Encoder().encodeBuffer(buffer));
|
System.out.println("-------------------------------------------------------------------------------------------------------------");
|
BASE64Encoder encoder = new sun.misc.BASE64Encoder();
|
File f = new File("C:\\Users\\75787\\Desktop\\image\\qiche.jpg");
|
BufferedImage bi;
|
try {
|
bi = ImageIO.read(f);
|
ByteArrayOutputStream baos = new ByteArrayOutputStream();
|
ImageIO.write(bi, "jpg", baos);
|
byte[] bytes = baos.toByteArray();
|
|
System.out.println(encoder.encodeBuffer(bytes).trim());
|
} catch (IOException e) {
|
e.printStackTrace();
|
}
|
}
|
|
}
|