HarmonyOS 如何将Java的中rsa签名翻译过来

/** 
 * RSA验证签名 
 * 
 * @param plainBytes  需要签名的数据 
 * @param signedBytes 已经签名的数据 
 * @param PublicKey   公钥 
 * @return 
 * @throws Exception 
 */ 
public static boolean verifyRSA(byte[] plainBytes, byte[] signedBytes, String PublicKey) throws Exception { 
  String SIGNATURE_ALGORITHM = "SHA1withRSA"; 
  Signature signature = Signature.getInstance(SIGNATURE_ALGORITHM); 
  signature.initVerify(getPublicKey(PublicKey)); 
  signature.update(plainBytes); 
 
  //Base64编码 
 
  return signature.verify(android.util.Base64.decode(new String(signedBytes, APP_UNICODE_TYPE), android.util.Base64.NO_WRAP)); 
}

这是Java代码,在HarmonyOS上我没看到这种SHA1withRSA格式的

HarmonyOS
2024-08-13 15:33:21
浏览
收藏 0
回答 1
待解决
回答 1
按赞同
/
按时间
zxjiu

参考示例如下:

import { cryptoFramework } from '@kit.CryptoArchitectureKit'; 
import { buffer, util } from '@kit.ArkTS'; 
 
@Entry 
@Component 
struct CryptPage { 
  @State message: string = '点击开始'; 
 
  build() { 
    Row() { 
      Column() { 
        Text(this.message) 
          .fontSize(50) 
          .fontWeight(FontWeight.Bold) 
          .onClick(() => { 
            convertAsyKey() 
          }) 
      } 
      .width('100%') 
    } 
    .height('100%') 
  } 
} 
 
 
export async function signMessagePromise(content: cryptoFramework.DataBlob, priKey: cryptoFramework.PriKey) { 
  let signAlg = "RSA2048|PKCS1|SHA1"; // 签名算法规格 
  let signer = cryptoFramework.createSign(signAlg); 
  await signer.init(priKey); 
  let signData = await signer.sign(content); 
  return signData; 
} 
 
export async function verifyMessagePromise(content: cryptoFramework.DataBlob, signMessageBlob: cryptoFramework.DataBlob, pubKey: cryptoFramework.PubKey) { 
  let verifyAlg = "RSA2048|PKCS1|SHA1"; // 签名算法规格 
  let verifier = cryptoFramework.createVerify(verifyAlg); 
  await verifier.init(pubKey); 
  let res = await verifier.verify(content, signMessageBlob); 
  console.info("verify result is " + res); 
  return res; 
} 
 
export async function convertAsyKey() { 
  let rsaGenerator = cryptoFramework.createAsyKeyGenerator('RSA2048'); 
  // 随机生成的公私钥对,Base64格式 
  let pubKeyVal = ""; 
  let priKeyVal = "MIICdgIBADANBgkqhkiG9w0BAxxxxxAgEAAoGBAIb6iT4b3FZeLkNnAEgYnbdHgKqFQ+i4m28zxxxxxIkJP3+zd1OdhE8KS1xxxxwwLV7lQgai+0z6/SexxxxxLLk/Wdn9NqkxxxxxRk3hdvYkIDBAYSixxxxxzbAlza6uIHpRGIGVteeagMHoxxxxTRxxxxxAgMBAAECgYAjhP5kp7Q9zh7igdJjJocqWdiUBumxxxxxfn79UT58g46rSMkCruQZLA7Y9ospKvRT0ZOJaZEqtWMEg20/ZUtQfWUOpI0V1zp1PFysSwwL9SWJs9p3xxxxx5Ra4lC+OcsCaSLmbnk1/BhqFXcOIL0xxxxxIdWA0cUQJBALwmJXOEy7riox0TU9Mv6Rw/QfJIXSwrMDgnt62VEAfdH0xxxxxupytNVJy43Rpd/uxI/Qw5e6dT70CQQCxxxxxxxYYPZ9v5YHuDZBtsQxxxxjUE6GxsNb6aWjSa/lDk96t38uIoxxxxxWqFAAa7i+LHAjwHLAkBMIUtKINOmvQmvPCDQ9TPnwBjZBjrdImSSdE+4Jvajl++Mf7F/XDkgOxxxxS8ylneM3OW3ZI+YeaqK/GFwpAkB+Er/cy4DlRqPx3W0vcuE3IDixxxxxt3XMbyILGR4DGvZJTBEPmGwrqonuDEMBZ2K8aqtgmjxxxxxxl6pTp4El5lgqZw5VR7czmXLDWGFZTAaTP/VLSNa5ppFFTxxxxxx9GuwhWkivMo8o6pCQZIsLFw=="; 
  // let pkBlob: cryptoFramework.DataBlob = { data: stringToUnit8Array(pubKeyVal) }; 
  let prBlob: cryptoFramework.DataBlob = { data: stringToUnit8Array(priKeyVal) }; 
  rsaGenerator.convertKey(null, prBlob, async (err, keyPair) => { 
    if (err) { 
      console.error(`convertKey failed, ${err.code}, ${err.message}`); 
      return; 
    } 
    console.info('convertKey success!'); 
    let plainText: string = "1234"; 
    let res = await signMessagePromise({ data: new Uint8Array(buffer.from(plainText, 'utf-8').buffer) }, keyPair.priKey) 
    console.log("convertKey sign result:" + new util.Base64Helper().encodeToStringSync(res.data)) 
    let verifyResult = await verifyMessagePromise({ 
      data: new Uint8Array(buffer.from(plainText, 'utf-8').buffer) 
    }, res, keyPair.pubKey); 
    if (verifyResult == true) { 
      console.info('convertKey verify success'); 
    } else { 
      console.error('convertKey verify failed'); 
    } 
  }); 
} 
 
export function stringToUnit8Array(str: string): Uint8Array { 
  return new util.Base64Helper().decodeSync(str) 
}
分享
微博
QQ
微信
回复
2024-08-13 22:12:16
相关问题
HarmonyOS RSA签名验签相关
12浏览 • 1回复 待解决
HarmonyOS 接口参数RSA签名+验签
14浏览 • 1回复 待解决
ets如何将图片转为byte[]?
3289浏览 • 1回复 待解决
如何将cookie同步到web
582浏览 • 1回复 待解决
如何将docker容器文件复制到主机?
2465浏览 • 1回复 待解决
HarmonyOS如何将Component转换PixelMap?
412浏览 • 1回复 待解决
RSA签名后,后端验签失败
206浏览 • 1回复 待解决
如何将PixelMap数据存储到数据库
1891浏览 • 1回复 待解决
如何将this指向改为方法外
469浏览 • 1回复 待解决
如何将ArrayBuffer转成string?
617浏览 • 1回复 待解决