使用HarmonyOS生成的SM2key加密成功,加密后的内容传到服务器报解密失败

需要将SM2加密后的字符串传到服务器,服务器验证通过后,会返回客户端相应的数据,但将HarmonyOS加密的字符串传到服务器后,服务器返回解密失败。

HarmonyOS
2024-11-08 10:53:30
浏览
收藏 0
回答 1
回答 1
按赞同
/
按时间
FengTianYa

在HarmonyOS端进行加密得到secreteBlob结果后,需要转换格式相关代码如下:

// this.secreteBlob是HarmonyOS加密成功doFinal返回的值;可以把b发送到服务端进行解密 
let b = new SM2_Ciphertext().d2i_SM2_Ciphertext(this.uint8ArrayToHex (this.secreteBlob.data)) 
 
// 相关方法如下: 
uint8ArrayToHex (uint8Array: Uint8Array) { 
  return buffer.from(uint8Array).toString('hex') 
} 
// sm2Ciphertext.ets 第一部分 
import hilog from '@ohos.hilog'; 
import { SM2_SEQUENCE } from './sm2Sequence'; 
 
export class ASN1Util { 
  static BOOLEAN: string = "01"; 
  static INTEGER: string = "02"; 
  static BIT_STRING: string = "03"; 
  static OCTET_STRING: string = "04"; 
  static NULL: string = "05"; 
  static REAL: string = "09"; 
  static ENUMERATED: string = "0a"; 
  static SEQUENCE: string = "30"; 
  static SET: string = "31"; 
} 
 
export class SM2_Ciphertext { 
  /** 
   * 用于将SM2裸密文数据序列化 
   * @param primal_data SM2裸密钥数据,长度为96+明文长度(字节),输入格式为C1C3C2的Hex字符串 
   * @returns 返回序列化后的标准密文数据,输出格式为Hex字符串 
   */ 
  i2d_SM2_Ciphertext(primal_data: string): string { 
    let sm2_sequence = new SM2_SEQUENCE(); 
    sm2_sequence.C1x = primal_data.slice(0, 64); 
    primal_data = primal_data.slice(64, primal_data.length); 
    sm2_sequence.C1y = primal_data.slice(0, 64); 
    primal_data = primal_data.slice(64, primal_data.length); 
    sm2_sequence.C3 = primal_data.slice(0, 64); 
    primal_data = primal_data.slice(64, primal_data.length); 
    sm2_sequence.C2 = primal_data; 
 
    let C1x_title: string = (Number.parseInt("0x" + sm2_sequence.C1x.slice(0, 2)) > 127) ? "022100" : "0220"; 
    let C1y_title: string = (Number.parseInt("0x" + sm2_sequence.C1y.slice(0, 2)) > 127) ? "022100" : "0220"; 
    let C3_title: string = "0420"; 
    let C2_title: string = "04" + this.genLenHex(sm2_sequence.C2); 
    let sequence_message: string = C1x_title + sm2_sequence.C1x + C1y_title + sm2_sequence.C1y + C3_title + sm2_sequence.C3 + C2_title + sm2_sequence.C2; 
    let sequence_lenHex:string = this.genLenHex(sequence_message); 
    let standard_data = "30" + sequence_lenHex + sequence_message; 
    return standard_data; 
  } 
  // sm2Ciphertext.ets 第二部分 
  /** 
   * 用于将标准SM2密文数据解码 
   * @param standard_data 标准SM2密文数据,符合ASN.1编码标准,输入格式为Hex字符串 
   * @returns 返回ASN.1解码后的SM2密文数据 
   */ 
  d2i_SM2_Ciphertext(standard_data: string): string { 
    let message: string = standard_data; 
    // 起始标识为03 
    if (!message.startsWith(ASN1Util.SEQUENCE)) { 
      this.ciphertextErr(); 
    } 
    message = message.slice(ASN1Util.SEQUENCE.length, message.length); 
 
    // SM2 sequence内容的长度 
    let sequence_lenHex: string = this.getLenHex(message); 
    message = message.slice(sequence_lenHex.length, message.length); 
    let sequence_len: number = this.lenHex2number(sequence_lenHex); 
    if (sequence_len != message.length / 2) { 
      this.ciphertextErr(); 
    } 
 
    let sm2_sequence = new SM2_SEQUENCE(); 
    message = this.readC1(sm2_sequence, message); 
    message = this.readC3(sm2_sequence, message); 
    message = this.readC2(sm2_sequence, message); 
 
    // C1C3C2格式 
    // let primal_data: string = sm2_sequence.C1x + sm2_sequence.C1y + sm2_sequence.C3 + sm2_sequence.C2; 
 
    // C1C2C3格式 
    let primal_data: string = sm2_sequence.C1x + sm2_sequence.C1y + sm2_sequence.C2 + sm2_sequence.C3; 
    return primal_data; 
  } 
 
  // 生成传入内容的长度域 
  genLenHex(content: string): string { 
    let size: number = content.length / 2; 
    let lenHex: string = ("00" + size.toString(16)).slice(-2); 
    if (size < 0x80) { 
      return lenHex; 
    } 
    let lenHex_size: number = lenHex.length / 2; 
    return (lenHex_size | 0x80).toString(16) + lenHex; 
  } 
 
  // 提取长度域的Hex字符串 
  getLenHex(data: string): string { 
    let byte: number = Number.parseInt("0x" + data.slice(0, 2)); 
    let len_size: number = byte > 127 ? byte - 0x80 + 1 : 1; 
    return data.slice(0, len_size * 2); 
  } 
 
  // 将长度域的Hex字符串转为整型 
  lenHex2number(lenHex: string): number { 
    if (lenHex.length == 2) { 
      return Number.parseInt("0x" + lenHex); 
    } 
    return Number.parseInt("0x" + lenHex.slice(2, lenHex.length)); 
  } 
  // sm2Ciphertext.ets 第三部分 
  ciphertextErr() { 
    hilog.error(0, "d2i_SM2_Ciphertext", "密文格式错误"); 
    throw new Error("SM2 ciphertext error!") 
  } 
 
  readC1(sm2_sequence: SM2_SEQUENCE, data: string): string { 
    let xy: string[] = []; 
    for (let i = 0; i < 2; i++) { 
      if (data.startsWith("0220")) { 
        xy[i] = data.slice(4, 68); 
        data = data.slice(68, data.length); 
      } else if (data.startsWith("022100")) { 
        xy[i] = data.slice(6, 70); 
        data = data.slice(70, data.length); 
      } else { 
        this.ciphertextErr(); 
      } 
    } 
    sm2_sequence.C1x = xy[0]; 
    sm2_sequence.C1y = xy[1]; 
    return data; 
  } 
 
  readC2(sm2_sequence: SM2_SEQUENCE, data: string): string { 
    if (data.startsWith(ASN1Util.OCTET_STRING)) { 
      data = data.slice(ASN1Util.OCTET_STRING.length, data.length); 
      let C2_lenHex = this.getLenHex(data); 
      data = data.slice(C2_lenHex.length, data.length) 
      if (this.lenHex2number(C2_lenHex) != data.length / 2) { 
        this.ciphertextErr() 
      } 
      sm2_sequence.C2 = data; 
    } else { 
      this.ciphertextErr(); 
    } 
    return data; 
  } 
 
  readC3(sm2_sequence: SM2_SEQUENCE, data: string): string { 
    if (data.startsWith("0420")) { 
      sm2_sequence.C3 = data.slice(4, 68); 
      data = data.slice(68, data.length); 
    } else { 
      this.ciphertextErr(); 
    } 
    return data; 
  } 
} 
// sm2Sequence.ets 
export class SM2_SEQUENCE{ 
  private _C1x: string = ""; 
  private _C1y: string = ""; 
  private _C2: string = ""; 
  private _C3: string = ""; 
 
  public set C1x(value: string) { 
    this._C1x = value; 
  } 
 
  public get C1x(): string { 
    return this._C1x; 
  } 
 
  public set C1y(value: string) { 
    this._C1y = value; 
  } 
 
  public get C1y(): string { 
    return this._C1y; 
  } 
 
  public set C2(value: string) { 
    this._C2 = value; 
  } 
 
  public get C2(): string { 
    return this._C2; 
  } 
 
  public set C3(value: string) { 
    this._C3 = value; 
  } 
 
  public get C3(): string { 
    return this._C3; 
  } 
 
  public toString():string{ 
    return JSON.stringify(this); 
  } 
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.
  • 46.
  • 47.
  • 48.
  • 49.
  • 50.
  • 51.
  • 52.
  • 53.
  • 54.
  • 55.
  • 56.
  • 57.
  • 58.
  • 59.
  • 60.
  • 61.
  • 62.
  • 63.
  • 64.
  • 65.
  • 66.
  • 67.
  • 68.
  • 69.
  • 70.
  • 71.
  • 72.
  • 73.
  • 74.
  • 75.
  • 76.
  • 77.
  • 78.
  • 79.
  • 80.
  • 81.
  • 82.
  • 83.
  • 84.
  • 85.
  • 86.
  • 87.
  • 88.
  • 89.
  • 90.
  • 91.
  • 92.
  • 93.
  • 94.
  • 95.
  • 96.
  • 97.
  • 98.
  • 99.
  • 100.
  • 101.
  • 102.
  • 103.
  • 104.
  • 105.
  • 106.
  • 107.
  • 108.
  • 109.
  • 110.
  • 111.
  • 112.
  • 113.
  • 114.
  • 115.
  • 116.
  • 117.
  • 118.
  • 119.
  • 120.
  • 121.
  • 122.
  • 123.
  • 124.
  • 125.
  • 126.
  • 127.
  • 128.
  • 129.
  • 130.
  • 131.
  • 132.
  • 133.
  • 134.
  • 135.
  • 136.
  • 137.
  • 138.
  • 139.
  • 140.
  • 141.
  • 142.
  • 143.
  • 144.
  • 145.
  • 146.
  • 147.
  • 148.
  • 149.
  • 150.
  • 151.
  • 152.
  • 153.
  • 154.
  • 155.
  • 156.
  • 157.
  • 158.
  • 159.
  • 160.
  • 161.
  • 162.
  • 163.
  • 164.
  • 165.
  • 166.
  • 167.
  • 168.
  • 169.
  • 170.
  • 171.
  • 172.
  • 173.
  • 174.
  • 175.
  • 176.
  • 177.
  • 178.
  • 179.
  • 180.
  • 181.
  • 182.
  • 183.
  • 184.
  • 185.
  • 186.
  • 187.
  • 188.
  • 189.
  • 190.
  • 191.
  • 192.
  • 193.
  • 194.
  • 195.
  • 196.
  • 197.
  • 198.
  • 199.
  • 200.
分享
微博
QQ
微信
回复
2024-11-08 15:28:21


相关问题
HarmonyOS 如何进行SM2key初始化
612浏览 • 1回复 待解决
HarmonyOS SM2加密算法
836浏览 • 1回复 待解决
HarmonyOS 选择图片上传到服务器demo
922浏览 • 1回复 待解决
HarmonyOS 将相册中图片上传到服务器
1666浏览 • 1回复 待解决
HarmonyOS SM2加密数据与Java不一致
924浏览 • 1回复 待解决
HarmonyOS 如何使用SM3加密
1053浏览 • 1回复 待解决
HarmonyOS RAS加密,AES加密解密
707浏览 • 1回复 待解决
HarmonyOS 使用AES加密失败
810浏览 • 1回复 待解决
HarmonyOS SM4加密
457浏览 • 1回复 待解决