Rust p256:如何从字符串获取公钥

huangapple go评论62阅读模式
英文:

Rust p256: How to get a PublicKey from a String

问题

I have a public key stored as a String.
我的目标是使用ecdsa验证消息。

I tried to use the from_str function but get an Err each time
我尝试使用from_str函数,但每次都收到错误消息。

let public_key: String = "...my_public_key..."
公钥:String = "...我的公钥..."

PublicKey::from_str(&public_key)
PublicKey::from_str(&public_key)

The documentation provided by p256 is very bad. How could I implement this?
p256提供的文档非常糟糕。我应该如何实现这个?

EDIT:

Using the from_str function from PublicKey, I always end up getting an Err (crypto error). Would you have any idea of how I could create a PublicKey from a String?
使用PublicKey的from_str函数,我总是收到一个Err(加密错误)。您有没有办法从字符串创建一个PublicKey的想法?

use p256::elliptic_curve::PublicKey;
使用p256::elliptic_curve::PublicKey;

match PublicKey::from_str(public_key)) {
match PublicKey::from_str(public_key)) {
Ok(pub_k) => {},
Err(e) => println!("{}", e)
}

I tried using this public_key (but does not seem to work for any): 0458962a508fc3648f30cc81f56d4d5ad79d09b7f8d1f88e254c8e915e4f669c5c738f05ad657f25a2f1afb7396fc4239de9b869e71dc3dbfd4f2e137c68871fe3
我尝试使用这个public_key(但似乎对任何人都不起作用):0458962a508fc3648f30cc81f56d4d5ad79d09b7f8d1f88e254c8e915e4f669c5c738f05ad657f25a2f1afb7396fc4239de9b869e71dc3dbfd4f2e137c68871fe3

I also tried adding '0x' at the beginning of my String but it didn't work.
我还尝试在字符串开头添加'0x',但没有成功。

英文:

I have a public key stored as a String.
My goal is to verify a message using ecdsa.

I tried to use the from_str function but get an Err each time

let public_key: String = "...my_public_key..."
PublicKey::from_str(&public_key)

The documentation provided by p256 is very bad. How could I implement this?

EDIT:

Using the from_str function from PublicKey, I always end up getting an Err (crypto error). Would you have any idea of how I could create a PublicKey from a String?

use p256::elliptic_curve::PublicKey;

match PublicKey::from_str(public_key)) {
        Ok(pub_k) => {},
        Err(e) => println!("{}", e)
}

I tried using this public_key (but does not seem to work for any): 0458962a508fc3648f30cc81f56d4d5ad79d09b7f8d1f88e254c8e915e4f669c5c738f05ad657f25a2f1afb7396fc4239de9b869e71dc3dbfd4f2e137c68871fe3

I also tried adding '0x' at the beggining of my String but it didn't work.

答案1

得分: 2

已发布的密钥是一个十六进制编码的未压缩公共椭圆曲线密钥(用于secp256r1,也称为NIST P-256)。首先必须对其进行十六进制解码,然后可以使用p256::PublicKey::from_sec1_bytes()导入。

示例:

use p256;
use hex;

fn main() {

    // 十六进制解码密钥
    let public_key_uncompressed_hex = "0458962a508fc3648f30cc81f56d4d5ad79d09b7f8d1f88e254c8e915e4f669c5c738f05ad657f25a2f1afb7396fc4239de9b869e71dc3dbfd4f2e137c68871fe3";
    let public_key_uncompressed = hex::decode(public_key_uncompressed_hex).expect("解码错误");

    // 导入未压缩的公共密钥
    let public_key = p256::PublicKey::from_sec1_bytes(&public_key_uncompressed).expect("导入错误");
    println!("{:?}", public_key); // PublicKey { point: AffinePoint {...
}
英文:

The posted key is a hex encoded uncompressed public EC key (for secp256r1 aka NIST P-256). This must first be hex decoded and can then be imported using p256::PublicKey::from_sec1_bytes().

Example:

use p256;
use hex;

fn main() {

    // Hex decode key
    let public_key_uncompressed_hex = "0458962a508fc3648f30cc81f56d4d5ad79d09b7f8d1f88e254c8e915e4f669c5c738f05ad657f25a2f1afb7396fc4239de9b869e71dc3dbfd4f2e137c68871fe3";
    let public_key_uncompressed = hex::decode(public_key_uncompressed_hex).expect("decoding error");   

    // Import uncompressed public key
    let public_key = p256::PublicKey::from_sec1_bytes(&public_key_uncompressed).expect("import error");
    println!("{:?}", public_key); // PublicKey { point: AffinePoint {...
}

huangapple
  • 本文由 发表于 2023年5月7日 00:58:57
  • 转载请务必保留本文链接:https://go.coder-hub.com/76190101.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定