This commit is contained in:
haozq 2025-01-16 19:57:57 +08:00
parent 08e9b2326e
commit 745eb89b4e
4 changed files with 688 additions and 11 deletions

File diff suppressed because one or more lines are too long

View File

@ -41,7 +41,7 @@ if (typeof KJUR.crypto == "undefined" || !KJUR.crypto) KJUR.crypto = {};
* </ul> * </ul>
* </p> * </p>
*/ */
KJUR.crypto.ECDSA = function(params) { KJUR.crypto.ECDSA =function(params) {
var curveName = "secp256r1"; // curve name default var curveName = "secp256r1"; // curve name default
var ecparams = null; var ecparams = null;
var prvKeyHex = null; var prvKeyHex = null;

View File

@ -8,7 +8,7 @@ function SM2Cipher(cipherMode) {
if (typeof (cipherMode) != 'undefined') { if (typeof (cipherMode) != 'undefined') {
this.cipherMode = cipherMode this.cipherMode = cipherMode
} else { } else {
this.cipherMode = SM2CipherMode.C1C2C3//默认0 this.cipherMode = SM2CipherMode.C1C2C3//Ĭ<EFBFBD><EFBFBD>0
} }
} }
SM2Cipher.prototype = { SM2Cipher.prototype = {
@ -83,6 +83,7 @@ SM2Cipher.prototype = {
var c3 = new Array(32); var c3 = new Array(32);
this.Dofinal(c3); this.Dofinal(c3);
var hexString = bytesToHex(c1.getEncoded(false)) + bytesToHex(data) + bytesToHex(c3); var hexString = bytesToHex(c1.getEncoded(false)) + bytesToHex(data) + bytesToHex(c3);
console.log(this.cipherMode )
if (this.cipherMode == SM2CipherMode.C1C3C2) { if (this.cipherMode == SM2CipherMode.C1C3C2) {
hexString = bytesToHex(c1.getEncoded(false)) + bytesToHex(c3) + bytesToHex(data) hexString = bytesToHex(c1.getEncoded(false)) + bytesToHex(c3) + bytesToHex(data)
} }
@ -125,7 +126,7 @@ SM2Cipher.prototype = {
GetBigIntegerByteArray : function(bi) { GetBigIntegerByteArray : function(bi) {
//BigIntger.toByteArray()转为有符号的二进制此方法转为无符号的二进制才可以与后台Java端的加解密等效 //BigIntger.toByteArray()תΪ<EFBFBD>з<EFBFBD><EFBFBD>ŵĶ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ƣ<EFBFBD><EFBFBD>˷<EFBFBD><EFBFBD><EFBFBD>תΪ<EFBFBD>޷<EFBFBD><EFBFBD>ŵĶ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ʋſ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>̨Java<EFBFBD>˵ļӽ<EFBFBD><EFBFBD>ܵ<EFBFBD>Ч
var tmpd = []; var tmpd = [];
if (bi == null) { if (bi == null) {
return tmpd; return tmpd;
@ -155,7 +156,7 @@ window.SM2CipherMode = {
function SM2CipherUtil() { function SM2CipherUtil() {
var cipher = new SM2Cipher(); var cipher = new SM2Cipher();
//获取公钥与私钥 //<EFBFBD><EFBFBD>ȡ<EFBFBD><EFBFBD>Կ<EFBFBD><EFBFBD>˽Կ
this.sm2GengenerateKeys = function() { this.sm2GengenerateKeys = function() {
var ec = new KJUR.crypto.ECDSA({"curve": "sm2"}); var ec = new KJUR.crypto.ECDSA({"curve": "sm2"});
var keypair = ec.generateKeyPairHex(); var keypair = ec.generateKeyPairHex();
@ -164,7 +165,6 @@ function SM2CipherUtil() {
this.sm2Encrypt = function(publicKey, text) { this.sm2Encrypt = function(publicKey, text) {
var userKey = cipher.CreatePoint(publicKey); var userKey = cipher.CreatePoint(publicKey);
var msgData = strToUtf8Bytes(text); var msgData = strToUtf8Bytes(text);
//返回16进制字符串
return cipher.Encrypt(userKey, msgData); return cipher.Encrypt(userKey, msgData);
}; };
this.sm2Decrypt = function(privateKey, cipherText) { this.sm2Decrypt = function(privateKey, cipherText) {

View File

@ -1,4 +1,4 @@
//二进制数组转16进掉 //???????????16????
function bytesToHex(arr) { function bytesToHex(arr) {
var str = ""; var str = "";
for (var i = 0; i < arr.length; i++) { for (var i = 0; i < arr.length; i++) {
@ -7,7 +7,7 @@ function bytesToHex(arr) {
return str; return str;
}; };
//16进掉转二进制数组 //16???????????????
function hexToBytes(str) { function hexToBytes(str) {
var arr = []; var arr = [];
var hexStrLength = str.length; var hexStrLength = str.length;
@ -17,7 +17,7 @@ function hexToBytes(str) {
return arr; return arr;
}; };
//字符转UTF-8的二进制数组 //????UTF-8???????????
function strToUtf8Bytes(str) { function strToUtf8Bytes(str) {
var bytes = new Array(); var bytes = new Array();
var len, c; var len, c;
@ -43,7 +43,7 @@ function strToUtf8Bytes(str) {
return bytes; return bytes;
}; };
//二进制数组转UTF-8的字符 //???????????UTF-8?????
function bytesToUtf8Str(arr) { function bytesToUtf8Str(arr) {
try { try {
var str = '', _arr = arr; var str = '', _arr = arr;
@ -63,7 +63,7 @@ function bytesToUtf8Str(arr) {
} }
return str; return str;
} catch(e) { } catch(e) {
alert("转UTF8出错非UTF8的二进制数组"); alert("?UTF8??????UTF8???????????");
} }
}; };