RSA编码错误:Bad arguments(有时出现,有时不出现)

本文详细介绍了如何在JavaScript前端利用公钥进行数据加密,并在Java后端实现安全解密的过程,包括解决数组长度异常问题的方法。

js前台使用公钥加密,后台java解密

问题是:

String result = request.getParameter("encryedPwd");  
byte[] en_result = new BigInteger(result, 16).toByteArray(); 

这里:byte[] en_result = new BigInteger(result, 16).toByteArray();  
有的时候你会发现数组长度129,第一个元素为0,这肯定是不正确的!

解决办法自己写一个hex to byte[]的方法,就不出现上面的问题了。

   /**
    * 16进制 To byte[]
    * @param hexString
    * @return byte[]
    */
   public static byte[] hexStringToBytes(String hexString) {
       if (hexString == null || hexString.equals("")) {
           return null;
       }
       hexString = hexString.toUpperCase();
       int length = hexString.length() / 2;
       char[] hexChars = hexString.toCharArray();
       byte[] d = new byte[length];
       for (int i = 0; i < length; i++) {
           int pos = i * 2;
           d[i] = (byte) (charToByte(hexChars[pos]) << 4 | charToByte(hexChars[pos + 1]));
       }
       return d;
   }
   /**
    * Convert char to byte
    * @param c char
    * @return byte
    */
    private static byte charToByte(char c) {
       return (byte) "0123456789ABCDEF".indexOf(c);
   }


int CyaSSL_RSA_do_verify(int type, const unsigned char* digest, int digest_len, const unsigned char* sig, int sig_len,CYASSL_RSA *key) { int answer = 0; unsigned char out[128] = {0}; unsigned int out_len = 128; unsigned int tmp_len = 0; word32 outLen; word32 signSz; int initTmpRng = 0; RNG* rng = NULL; int ret = 0; #ifdef CYASSL_SMALL_STACK RNG* tmpRNG = NULL; byte* encodedSig = NULL; #else RNG tmpRNG[1]; byte encodedSig[MAX_ENCODED_SIG_SZ]; #endif CYASSL_MSG("CyaSSL_RSA_Verify"); if (key == NULL) { CYASSL_MSG("Bad function arguments"); return 0; } /* 把rsa_key的外部数据同步到内部internal数据 */ if (SetRsaInternal(key)<0) { CYASSL_MSG("SetRsaInternal failed"); return 0; } if (digest == NULL || sig == NULL) CYASSL_MSG("Bad function arguments"); else if (key->inSet == 0) CYASSL_MSG("No RSA internal set"); else if (type != NID_md5 && type != NID_sha1) CYASSL_MSG("Bad md type"); else { outLen = (word32)CyaSSL_BN_num_bytes(key->n); #ifdef CYASSL_SMALL_STACK encodedSig = (byte*)XMALLOC(MAX_ENCODED_SIG_SZ, NULL, DYNAMIC_TYPE_TMP_BUFFER); if (encodedSig == NULL) { return 0; } #endif } #ifdef CYASSL_SMALL_STACK type = (type == NID_md5) ? MD5h : SHAh; signSz = EncodeSignature(encodedSig, digest, digest_len, type); /*rsa_dump_bytes("encodedSig", (char*)encodedSig, signSz);*/ #endif /*rsa_dump_bytes("digest out", (char*)digest, digest_len);*/ if (0 > (tmp_len = RsaSSL_Verify(sig, sig_len, out, out_len, (RsaKey *)key->internal))) { CYASSL_MSG("RsaSSL_Verify failed"); return 0; } /*rsa_dump_bytes("RsaSSL_Verify out", out, (int)out_len);*/ #ifdef CYASSL_SMALL_STACK printf("tmp_len: %d signSz:%d", tmp_len, signSz); if(tmp_len == signSz && 0 == memcmp(encodedSig, out, signSz)) answer = 1; XFREE(encodedSig, NULL, DYNAMIC_TYPE_TMP_BUFFER); #else printf("tmp_len: %d digest_len:%d", tmp_len, digest_len); if(tmp_len == digest_len && 0 == memcmp(digest, out, digest_len)) answer = 1; #endif printf("tmp_len: %d", answer); return answer; }类似的逻辑,帮我实现CyaSSL_ECC_do_verify函数,注意我用的是cyassl,要调用wolfssl接口!!!
最新发布
10-29
评论 3
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值