dongxia8656 2014-09-28 08:09
浏览 200
已采纳

RSA签名密钥长度异常长

I am currently maintaining an Alipay URL-generation library, and there are some interesting things going on in this code snippet (other than the massively confusing naming schemes and function placements)..

I have a function that builds a URL (using create_linkstring()) + its RSA-signed (sign()) counterpart:

function build_mysign($sort_array,$key,$sign_type) {
    $prestr = create_linkstring($sort_array);
    $prestr = $prestr.$key;
    $mysgin = sign($prestr,$sign_type);
    return $mysgin;
}

This is my create_linkstring() method:

function create_linkstring($array) {
    $arg  = "";
    while (list ($key, $val) = each ($array)) {
        $arg.=$key."=".$val."&";
    }
    $arg = substr($arg,0,count($arg)-2);
    return $arg;
}

This is my sign() function:

function sign($prestr){
    $fprivate = fopen(dirname(__FILE__) . '/rsa_private_key.pem', 'r');
    $priv_key = fread($fprivate, 123456);
    fclose($fprivate);

    $privatekeyid = openssl_get_privatekey($priv_key);

    // Compute signature
    openssl_sign($prestr, $signMsg, $privatekeyid, OPENSSL_ALGO_SHA1);

    // Free the key from memory
    openssl_free_key($privatekeyid);

    $signMsg = base64_encode($signMsg);

    return $signMsg;
}

It basically takes an array of keys and values and generates a URL, which then gets signed using the sign() method.

This library actually has two signing mechanisms - MD5 and RSA. I wrote the RSA-signing function, since it wasn't supported by default. Looking at the output URL of using the MD5-signing, the sign= is a 32 char string, so it looks like this sign=779d70d2b4d9b50cad3a4ce144726e9f.

However, using my RSA-signing function, the output is a 172 char string! The input looks like this:

&sign=WiYQIl0x+Vkey3SciP03HDv/7IZKoq2+OcvsSlQJb3NMElG6JawRc5b98ddJOOYjt9YK2YypwlFWgizj0b5wk+HNJB5lSYq7rDCcDGG0m0cKbW/m9P23J8gQaR1x6RovEDaWhs5zv4YTFe83hmPaf4Q/eVa2CpiixjpHd3kjybg=

If I am not wrong, RSA-signed strings should be 128 chars long. I'm not quite sure why that is happening. I'm not 100% if I am appending it with extraneous strings, and I notice that all the RSA-signed keys end with a = which definitely isn't right.

  • 写回答

1条回答 默认 最新

  • duanping5306 2014-09-28 09:05
    关注

    I guess you are confusing the byte length of the RSA signature with the string length of the base64 encoding of the signature.

    If you are using a 1024 bit RSA key (a bit short, btw.), the byte length of the signature should be 128 bytes long. The bytes can be of any value, including 0 and other characters that are not printable with UTF-8 or ASCII (and many more that are not good for use in URLs etc.). Thus your code encodes the bytes in base64. The size of 172 characters (including the padding =) is just right: 128bytes / 1024bits

    $ echo WiYQIl0x+Vkey3SciP03HDv/7IZKoq2+OcvsSlQJb3NMElG6JawRc5b98ddJOOYjt9YK2YypwlFWgizj0b5wk+HNJB5lSYq7rDCcDGG0m0cKbW/m9P23J8gQaR1x6RovEDaWhs5zv4YTFe83hmPaf4Q/eVa2CpiixjpHd3kjybg= \
    | base64 --decode | hexdump -C
    00000000  5a 26 10 22 5d 31 f9 59  1e cb 74 9c 88 fd 37 1c  |Z&."]1.Y..t...7.|
    00000010  3b ff ec 86 4a a2 ad be  39 cb ec 4a 54 09 6f 73  |;...J...9..JT.os|
    00000020  4c 12 51 ba 25 ac 11 73  96 fd f1 d7 49 38 e6 23  |L.Q.%..s....I8.#|
    00000030  b7 d6 0a d9 8c a9 c2 51  56 82 2c e3 d1 be 70 93  |.......QV.,...p.|
    00000040  e1 cd 24 1e 65 49 8a bb  ac 30 9c 0c 61 b4 9b 47  |..$.eI...0..a..G|
    00000050  0a 6d 6f e6 f4 fd b7 27  c8 10 69 1d 71 e9 1a 2f  |.mo....'..i.q../|
    00000060  10 36 96 86 ce 73 bf 86  13 15 ef 37 86 63 da 7f  |.6...s.....7.c..|
    00000070  84 3f 79 56 b6 0a 98 a2  c6 3a 47 77 79 23 c9 b8  |.?yV.....:Gwy#..|
    00000080
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
  • ¥15 帮我写一个c++工程
  • ¥30 Eclipse官网打不开,官网首页进不去,显示无法访问此页面,求解决方法
  • ¥15 关于smbclient 库的使用
  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?