如何使用Rust将SEC1编码的ECDSA公钥序列化为ASN.1 DER格式?

qvsjd97n  于 2023-05-07  发布在  其他
关注(0)|答案(2)|浏览(191)

我已经编写了Python版本的转换方法,但我不知道如何编写Rust版本,
Rust不能使用openssl包编写
我查询的数据,它似乎是可用的k256="0.13.1" der = "0.7.5"替换
目前已知的公钥是Vec类型的,我不知道如何将公钥从SEC1编码转换为ASN.1 DER编码
下面是我实现的部分代码

use der::{Encoder, Length};
use k256::elliptic_curve::sec1::{ToEncodedPoint, FromEncodedPoint};
use k256::{PublicKey, EncodedPoint};
use der::asn1::{BitString, ObjectIdentifier};

fn sec1_to_der(sec1_key: &[u8]) -> Result<Vec<u8>, der::Error> {
    // Load the SEC1-encoded ECDSA public key as the public key object
    let public_key = PublicKey::from_sec1_bytes(sec1_key).expect("error");

    // Define the OID for secp256k1 (1.3.132.0.10)
    let secp256k1_oid: ObjectIdentifier = k256::pkcs8::ObjectIdentifier::new("1.3.132.0.10");

    // TODO: How to serialize SEC1-encoded ECDSA to ASN.1 DER format here

}

fn main() {
    let der_encoded_public_key:Vec<u8> = vec![3, 89, 191, 147, 51, 6, 188, 19, 231, 106, 80, 5, 56, 106, 114, 19, 115, 17, 94, 22, 217, 162, 205, 241, 154, 223, 129, 72, 143, 27, 102, 251, 246];;
    let der_key = sec1_to_der(&der_encoded_public_key);
    match der_key {
        Ok(der_key) => {
            println!("{:?}", der_key);
        }
        Err(e) => {
            println!("{:?}", e);
        }
    }
}

编辑:预期结果(见评论):

[48, 86, 48, 16, 6, 7, 42, 134, 72, 206, 61, 2, 1, 6, 5, 43, 129, 4, 0, 10, 3, 66, 0, 4, 89, 191, 147, 51, 6, 188, 19, 231, 106, 80, 5, 56, 106, 114, 19, 115, 17, 94, 22, 217, 162, 205, 241, 154, 223, 129, 72, 143, 27, 102, 251, 246, 250, 38, 107, 239, 240, 74, 233, 61, 164, 22, 115, 130, 240, 115, 219, 245, 176, 223, 219, 226, 248, 174, 2, 113, 126, 158, 15, 175, 50, 83, 25, 191]
qni6mghb

qni6mghb1#

在您的代码中,use der::{Encoder...Encoder不存在于der crate上。您可以将公钥从SEC1编码转换为ASN.1 DER编码:

main.rs文件:

use num_bigint::{BigUint, BigInt, Sign};
use simple_asn1::{to_der, ASN1Block};

fn main() {
    let sec1: Vec<u8> = vec![
        3, 89, 191, 147, 51, 6, 188, 19, 231, 106, 80, 5, 56, 106, 114, 19, 115, 17, 94, 22, 217,
        162, 205, 241, 154, 223, 129, 72, 143, 27, 102, 251,
    ];
    let x = &sec1[..32];
    let y = &sec1[32..];
    let x_val = BigUint::from_bytes_be(x);
    let y_val = BigUint::from_bytes_be(y);
    let x_bigint = BigInt::from_biguint(Sign::Plus, x_val);
    let y_bigint = BigInt::from_biguint(Sign::Plus, y_val);
    let der = to_der(&ASN1Block::Sequence(
        0,
        vec![
            ASN1Block::Integer(0, x_bigint),
            ASN1Block::Integer(0, y_bigint),
        ],
    ))
        .unwrap();
    println!("{:?}", der);
}

其输出为:

Finished dev [unoptimized + debuginfo] target(s) in 7.25s
     Running `target/debug/soTesting2`
[48, 37, 2, 32, 3, 89, 191, 147, 51, 6, 188, 19, 231, 106, 80, 5, 56, 106, 114, 19, 115, 17, 94, 22, 217, 162, 205, 241, 154, 223, 129, 72, 143, 27, 102, 251, 2, 1, 0]

Cargo.toml文件:

[package]
name = "soTesting2"
version = "0.1.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
num-bigint = "0.4"
simple_asn1 = "0.5"
bvuwiixz

bvuwiixz2#

发布的示例密钥是一个压缩的公钥,将以X.509/SPKI格式导出为ASN.1/DER编码的公钥。
导入可以使用k256::PublicKey::from_sec1_bytes()(如前所述),导出可以使用k256::pkcs8::EncodePublicKey::to_public_key_der()
请注意,没有必要指定曲线(或OID),因为crate k256对于曲线secp 256 k1是 * 显式 * 的。
示例实现:

use k256;

fn main() {
    let public_key_compressed:Vec<u8> = vec![3, 89, 191, 147, 51, 6, 188, 19, 231, 106, 80, 5, 56, 106, 114, 19, 115, 17, 94, 22, 217, 162, 205, 241, 154, 223, 129, 72, 143, 27, 102, 251, 246];    
    let public_key = k256::PublicKey::from_sec1_bytes(&public_key_compressed).expect("import error");
    let public_key_der = k256::pkcs8::EncodePublicKey::to_public_key_der(&public_key).expect("export error");
    println!("{:?}", public_key_der.to_vec()); // [48, 86, 48, 16, 6, 7, 42, 134, 72, 206, 61, 2, 1, 6, 5, 43, 129, 4, 0, 10, 3, 66, 0, 4, 89, 191, 147, 51, 6, 188, 19, 231, 106, 80, 5, 56, 106, 114, 19, 115, 17, 94, 22, 217, 162, 205, 241, 154, 223, 129, 72, 143, 27, 102, 251, 246, 250, 38, 107, 239, 240, 74, 233, 61, 164, 22, 115, 130, 240, 115, 219, 245, 176, 223, 219, 226, 248, 174, 2, 113, 126, 158, 15, 175, 50, 83, 25, 191]
}

相关问题