RSA加密长度不一致的结果

时间:2019-01-11 03:12:53

标签: ios objective-c openssl rsa

我正在使用外部库OpenSSL-Universal使用RSA_PKCS1_PADDING加密密码。不幸的是,char编码结果的输出长度不一致。假设我有2048位模数,我期望的长度结果是128,否则它将无法解密回纯文本。

BIGNUM *xponent = BN_new();
    BIGNUM *modulus = BN_new();
    BN_hex2bn(&xponent,xponentInHex);
    BN_hex2bn(&modulus,modInHex);

    RSA *rsa = RSA_new();
    rsa->e = xponent;
    rsa->n = modulus;
    char encoded[1024] = {0};

    RSA_public_encrypt(
        (int)strlen(charString),// from len
        (const unsigned char *)charString, // from
        (unsigned char *)encoded, // to
        rsa,
        RSA_PKCS1_PADDING
    );

    RSA_free(rsa);
NSLog(@"%lu", strlen(encoded));

如果我的工具有任何问题,或者您对长度不一致的结果有任何解释,请告诉我

2 个答案:

答案 0 :(得分:2)

来自UPDATE tickets SET is_send = CAST('1' AS TEXT); 的加密输出是不是字符串,因此您无法使用 <script type="text/javascript"> $(function () { var ss_schoolClassificationCD = sessionStorage.getItem("schoolClassificationCD"); if($.trim(ss_schoolClassificationCD)=="" || ss_schoolClassificationCD==="undefined"!="" || schoolClassificationCD!=null){ $("#schoolClassificationCD").val(ss_schoolClassificationCD); } var ss_constructionSpecificity = sessionStorage.getItem("constructionSpecificity"); if($.trim(ss_constructionSpecificity)=="" || ss_constructionSpecificity==="undefined"!="" || ss_constructionSpecificity!=null){ $("#constructionSpecificity").val(ss_constructionSpecificity); } var ss_playingStyle = sessionStorage.getItem("playingStyle"); if($.trim(ss_playingStyle)=="" || ss_playingStyle==="undefined"!="" || ss_playingStyle!=null){ $("#playingStyle").val(ss_playingStyle); } var ss_workingMethod = sessionStorage.getItem("workingMethod"); if($.trim(ss_workingMethod)=="" || ss_workingMethod==="undefined"!="" || ss_workingMethod!=null){ $("#workingMethod").val(ss_workingMethod); } $("#calculate").click(function(){ var careerHistory = $("#careerHistory").val(); var wholeBookNumber = $("#wholeBookNumber").val(); var reflectionBookNumber = $("#reflectionBookNumber").val(); var schoolClassificationCD = $("#schoolClassificationCD").val(); var jobsNumber = $("#jobsNumber").val(); var constructionSpecificity = $("#constructionSpecificity").val(); var playingStyle = $("#playingStyle").val(); var workingMethod = $("#workingMethod").val(); var workOvertimeAverage = $("#workOvertimeAverage").val(); if($.trim(careerHistory)=="" || careerHistory==="undefined"){ return; }else{ sessionStorage.setItem("careerHistory",careerHistory); sessionStorage.setItem("wholeBookNumber",wholeBookNumber); sessionStorage.setItem("reflectionBookNumber",reflectionBookNumber); sessionStorage.setItem("schoolClassificationCD",schoolClassificationCD); sessionStorage.setItem("jobsNumber",jobsNumber); sessionStorage.setItem("constructionSpecificity",constructionSpecificity); sessionStorage.setItem("playingStyle",playingStyle); sessionStorage.setItem("workingMethod",workingMethod); sessionStorage.setItem("workOvertimeAverage",workOvertimeAverage); $("#form1").submit(); } }); }); </script> 检查其长度。我希望您确实意识到strlen会将在加密输出中看到的任何'\ 0'视为字符串的结尾,并将返回直到该点的字节数作为字符串的长度。但是,加密缓冲区的有效字节可以为'\ 0'。

另外,请注意,RSA_public_encrypt返回您应该使用的加密数据的长度。

答案 1 :(得分:0)

您可以尝试使用Objective-C-RSA第三方库,或者阅读源代码,然后自己实现。