ECDSA字节数组转换为私钥错误

时间:2018-08-29 15:29:18

标签: java encryption ecdsa

我想将私钥保存为json文件(十六进制格式),然后将其读取为私钥。

此处的键生成函数

import boto3

botoSession = boto3.Session(
    aws_access_key_id     = <your access key>,
    aws_secret_access_key = <your secret key>,
    region_name           = <your region>,
)

s3 = botoSession.resource('s3')
bucket = s3.Bucket(bucketname)
objects = bucket.objects.filter(Prefix=<your prefix>)
objects.delete()

这是我将其保存到json文件中的方式

public void generateKeyPair() {
    try {
        Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider());
        KeyPairGenerator keyGen = KeyPairGenerator.getInstance("ECDSA", "BC");
        SecureRandom random = SecureRandom.getInstance("SHA1PRNG");
        ECGenParameterSpec ecSpec = new ECGenParameterSpec("secp256k1");
        keyGen.initialize(ecSpec,random);
        KeyPair keyPair = keyGen.generateKeyPair();
        privateKey = keyPair.getPrivate();
        publicKey = keyPair.getPublic();
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}

并从文件中读取

        a.generateKeyPair();
        byte[] enc_key = a.privateKey.getEncoded();

        StringBuilder key_builder = new StringBuilder();
        for(byte b : enc_key){
            key_builder.append(String.format( "%02X",b));
        }

        String serialized_key = key_builder.toString();
        account.privateKey=serialized_key;
        try (Writer writer = new FileWriter("Output.json")) {
            Gson gson = new GsonBuilder().create();
            gson.toJson(account, writer);
        } catch (IOException e) {
            e.printStackTrace();
        }

功能失败,我报错

        Gson gson = new GsonBuilder().create();
        try (Reader read1 = new FileReader("Output.json")) {
            account=gson.fromJson(read1,account.getClass());
            byte[] encoded_key=account.privateKey.getBytes();
            a.privateKey = getPrivateKey(encoded_key);

    public static PrivateKey getPrivateKey(byte[] privkey) throws NoSuchAlgorithmException, InvalidKeySpecException {
    Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider());
    EncodedKeySpec privateKeySpec = new PKCS8EncodedKeySpec(privkey);
    KeyFactory kf = null;
    try {
        kf = KeyFactory.getInstance("ECDSA", "BC");
    } catch (NoSuchProviderException e) {
        e.printStackTrace();
    }
    PrivateKey privateKey = kf.generatePrivate(privateKeySpec);
    return privateKey;
}

1 个答案:

答案 0 :(得分:0)

您忘记了对私钥进行十六进制解码。仅执行getBytes不会这样做。

编码的字节以SEQUENCE标记0x30开始。以十六进制表示的当然是"30",或者以ASCII表示的是0x33, 0x30:这是getBytes返回的前两个字节。现在,解码器查看比特值为0b001_10011的第一个字节。最后5位编码标记值,即16 + 2 + 1 =19。因此,是特定的错误。