libnacl加密库 - 如何恢复公钥

时间:2013-10-27 19:07:03

标签: cryptography encryption-asymmetric nacl-cryptography

我使用nacl加密库,需要从中恢复公钥 密钥。这可能吗?

这是我有点天真的尝试:

#include <nacl/crypto_scalarmult_curve25519.h>
#include <nacl/crypto_sign.h>

//...

int main(int argc, char **argv)
{
    UCHAR pk[crypto_sign_PUBLICKEYBYTES];
    UCHAR sk[crypto_sign_SECRETKEYBYTES];
    char pkhexbuf[2*crypto_sign_PUBLICKEYBYTES+1];
    char skhexbuf[2*crypto_sign_SECRETKEYBYTES+1];

    if( crypto_sign_keypair( pk, sk ) != 0) {
        printf( "Failed to generate keys." );
        return 1;
    }

    printf( "public key: %s\n", bytes_to_hex( pkhexbuf, pk, sizeof(pk) ) );
    printf( "secret key: %s\n", bytes_to_hex( skhexbuf, sk, sizeof(sk) ) );

    //reset the public key
    memset(pk, '\0', sizeof(pk));

    //recover public key
    if( crypto_scalarmult_curve25519_base(pk, sk) != 0 ) {
        printf( "Failed to derive public key from secret key." );
        return 1;
    }

    printf( "public key: %s\n", bytes_to_hex( pkhexbuf, pk, sizeof(pk) ) );
    return 0;
}

公钥不匹配。有任何想法应该怎么做?

0 个答案:

没有答案