从加载的证书中创建publicKey对象

时间:2017-07-16 15:49:17

标签: java rsa public-key-encryption

我有一个证书,我想用它来加密某些消息。现在我正在尝试从文件加载证书,但我不知道如何创建 PublicKey对象。我想创建它使用这个方法来加密它。

我从此链接下载了证书:

https://dtc.jrc.ec.europa.eu/dtc_public_key_certificates.php

代码

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.security.PublicKey;

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


public class MakePublicKey {

    static byte[] signature = null;


    public static void main(String[] args) {

        try {
            FileInputStream ecPubKeyFIS = new FileInputStream("D__TCC40-1.bin");

            try {
                int certificateLength = ecPubKeyFIS.available();
                byte[] certificate = new byte[certificateLength];
                ecPubKeyFIS.read(certificate);

                MakePublicKey.signature = new byte[128];
                System.arraycopy(certificate, 0, MakePublicKey.signature, 0, 128);

                // How can I make of the signature byte[] an  PublicKey Object to call the method as the following: encrypte("Hellow World!", pk)?


            } catch (IOException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }
            // }
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }


    public static byte[] encrypte(String message, PublicKey pk) {

        Cipher cipher = null;
        byte[] encrypted = null;
        try {
            cipher = Cipher.getInstance("RSA");
        } catch (NoSuchAlgorithmException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (NoSuchPaddingException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        try {
            cipher.init(Cipher.ENCRYPT_MODE, pk);
        } catch (InvalidKeyException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        try {
            encrypted = cipher.doFinal(message.getBytes());
        } catch (IllegalBlockSizeException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (BadPaddingException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return encrypted;

    }

}

修改

使用以下片段时

                CertificateFactory cf = CertificateFactory.getInstance("X.509");
                X509Certificate certificate = (X509Certificate) cf.generateCertificate(ecPubKeyFIS);
            PublicKey pk = certificate.getPublicKey();

我收到以下错误:

java.security.cert.CertificateException: Could not parse certificate: java.io.IOException: Empty input
    at sun.security.provider.X509Factory.engineGenerateCertificate(X509Factory.java:104)
    at java.security.cert.CertificateFactory.generateCertificate(CertificateFactory.java:339)
    at MakePublicKey.main(MakePublicKey.java:32)

2 个答案:

答案 0 :(得分:0)

将证书作为X509Certificate.html阅读,然后您可以调用getPublicKey:

FileInputStream ecPubKeyFIS = new FileInputStream("D__TCC40-1.bin");
CertificateFactory cd = CertificateFactory.getInstance("X.509");
X509Certificate c=(X509Certificate)cf.generateCertificate(ecPubKeyFIS );
c.getPublicKey();

答案 1 :(得分:0)

您需要使用Certificate课程将数据读作CertificateFactory,然后拨打getPublicKey()

这都是记录在案的。