[Flutter]:如何从x509证书PEM字符串获取公钥PEM字符串

iyfjxgzm  于 2023-02-09  发布在  Flutter
关注(0)|答案(2)|浏览(339)

我需要从x509证书PEM字符串中提取公钥的PEM字符串的方法。现在我使用basic_utils包,它可以从给定的PEM字符串生成x509 Certificate对象。此x509 Certificate对象具有publicKeyData字段,该字段具有bytes字段,将公钥字节表示为字符串。但是,我无法使用此bytes生成公钥的有效PEM字符串。
所以问题是-我如何提取公钥作为PEM字符串从证书?
谢谢

cbwuti44

cbwuti441#

对于RSA公钥:

import 'package:convert/convert.dart';
import 'package:basic_utils/basic_utils.dart';

final bytes = hex.decode(cert.publicKeyData.bytes!);
final key = CryptoUtils.rsaPublicKeyFromDERBytes(Uint8List.fromList(bytes));
final pem = CryptoUtils.encodeRSAPublicKeyToPemPkcs1(key);

编码器的Pkcs1版本将PEM与BEGIN RSA PUBLIC KEY括在一起,而非pkcs1版本将使用BEGIN PUBLIC KEY

dly7yett

dly7yett2#

在Java加密中

import java.io.IOException;
import java.security.InvalidKeyException;
import java.security.KeyFactory;
import java.security.NoSuchAlgorithmException;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.security.spec.InvalidKeySpecException;
import java.security.spec.PKCS8EncodedKeySpec;
import java.security.spec.X509EncodedKeySpec;
import java.util.Base64;

import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;

public static String encrypt(String request) {
    try {
        String publicKeyString = null;

        publicKeyString = "YOUR_PUBLIC_KEY_GOES_HERE==";

        PublicKey publicKey = getPublicKey(publicKeyString);
        Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding");
        cipher.init(Cipher.ENCRYPT_MODE, publicKey);
        return Base64.getEncoder().encodeToString(cipher.doFinal(request.getBytes()));
    } catch (Exception e) {
        System.out.println("Exception" + e);
        
        return e.toString();
    }
}
public static PublicKey getPublicKey(String base64PublicKey) {
    PublicKey publicKey = null;
    try {
        X509EncodedKeySpec keySpec = new X509EncodedKeySpec(Base64.getDecoder().decode(base64PublicKey.getBytes()));
        KeyFactory keyFactory = KeyFactory.getInstance("RSA");
        publicKey = keyFactory.generatePublic(keySpec);
        return publicKey;
    } catch (NoSuchAlgorithmException e) {
        System.out.println("Exception, " + e);
    } catch (InvalidKeySpecException e) {
        System.out.println("Exception, " + e);
    }
    return publicKey;
}

将Java加密格式转换为Flutter/dart

import 'package:encrypt/encrypt.dart';
import 'package:flutter/foundation.dart';
import 'dart:convert';
import 'package:basic_utils/basic_utils.dart';

encryptString(String plainText) async {
    var modulusBytes = base64.decode(
        "YOUR_PUBLIC_GOES_HERE");

    final key =
        CryptoUtils.rsaPublicKeyFromDERBytes(Uint8List.fromList(modulusBytes));
    final pem = CryptoUtils.encodeRSAPublicKeyToPemPkcs1(key);
    final publicKey = RSAKeyParser().parse(pem) as RSAPublicKey;

    final encrypter = Encrypter(
      RSA(publicKey: publicKey, encoding: RSAEncoding.PKCS1),
    );

    final encrypted = encrypter.encrypt(plainText);

    return encrypted.base64;    
}

相关问题