当前位置: 首页 > 面试题库 >

如何从我们创建的密钥库中检索我的公钥和私钥

齐威
2023-03-14
问题内容

我的任务如下:

  • 从我创建的密钥库中检索我的公钥和私钥。
  • 使用这些密钥可以使用我的RSA 2048位公共密钥对段落进行加密。
  • 使用DSA-SHA-1签名算法对结果进行数字签名。
  • 将数字签名输出保存到名为的文件output.dat

下面的程序引发错误:“
java.security.InvalidKeyException:没有安装的提供程序支持此密钥:sun.security.provider.DSAPublicKeyImpl”。

import java.security.*;
import java.security.KeyStore.*;
import java.io.*;
import java.security.PublicKey;
import java.security.PrivateKey;
import javax.crypto.Cipher;
import java.nio.charset.*;
import sun.security.provider.*;
import  javax.crypto.*;

public class Code {

/**
 * @param args the command line arguments
 */
    public static void main(String[] args) {

        try {

            /* getting data for keystore */

            File file = new File(System.getProperty("user.home") + File.separatorChar + ".keystore");
            FileInputStream is = new FileInputStream(file);
            KeyStore keystore = KeyStore.getInstance(KeyStore.getDefaultType());

            /*Information for certificate to be generated */ 
            String password = "abcde";
            String alias = "mykeys";
            String alias1 = "skeys";

            String filepath ="C:\\email.txt";

            /* getting the key*/
            keystore.load(is, password.toCharArray());
            PrivateKey key = (PrivateKey)keystore.getKey(alias, "bemylife".toCharArray());
            //PrivateKey key = cert1.getPrivateKey();
            //PublicKey key1= (PrivateKey)key;

            /* Get certificate of public key */
            java.security.cert.Certificate cert = keystore.getCertificate(alias);

            /* Here it prints the public key*/
            System.out.println("Public Key:");
            System.out.println(cert.getPublicKey());

            /* Here it prints the private key*/
            System.out.println("\nPrivate Key:");
            System.out.println(key);

            Cipher cipher = Cipher.getInstance("RSA");
            cipher.init(Cipher.ENCRYPT_MODE,cert.getPublicKey());

            String cleartextFile = "C:\\email.txt";
            String ciphertextFile = "D:\\ciphertextRSA.png";

            FileInputStream fis = new FileInputStream(cleartextFile);
            FileOutputStream fos = new FileOutputStream(ciphertextFile);
            CipherOutputStream cos = new CipherOutputStream(fos, cipher);

            byte[] block = new byte[32];
            int i;
            while ((i = fis.read(block)) != -1) {
                cos.write(block, 0, i);
            }
            cos.close();


            /* computing the signature*/
            Signature dsa = Signature.getInstance("SHA1withDSA", "SUN");
            dsa.initSign(key);
            FileInputStream f = new FileInputStream(ciphertextFile);
            BufferedInputStream in = new BufferedInputStream(f);
            byte[] buffer = new byte[1024];
            int len;
            while ((len = in.read(buffer)) >= 0) {
               dsa.update(buffer, 0, len);
           };
           in.close();

           /* Here it prints the signature*/
           System.out.println("Digital Signature :");
           System.out.println( dsa.sign());

           /* Now Exporting Certificate */
           System.out.println("Exporting Certificate. ");
           byte[] buffer_out = cert.getEncoded();
           FileOutputStream os = new FileOutputStream(new File("d:\\signedcetificate.cer"));
           os.write(buffer_out);
           os.close();

           /* writing signature to output.dat file */
           byte[] buffer_out1 = dsa.sign();
           FileOutputStream os1 = new FileOutputStream(new File("d:\\output.dat"));
           os1.write(buffer_out1);
           os1.close();

       } catch (Exception e) {System.out.println(e);}

   }
}

问题答案:

问题在于DSA密钥不适合RSA加密。您需要一个RSA密钥进行加密,也许您可​​以将签名算法切换为RSA / SHA1,以避免需要两个密钥。



 类似资料:
  • 我创建了一个密钥库,它包括私钥和公钥。我需要使用java代码检索我的公钥和私钥。我在这里找到了一个检索私钥的方法。但我找不到检索公钥的方法。有人能告诉我一种取出公钥的方法吗

  • 我们需要使用Azure key vault创建RSA密钥对,并将RSA公钥复制到外部系统。其要求是外部系统使用公钥加密数据,内部系统与azure key vault对话并对数据进行解密。我还没有访问Azure key vault的权限,所以需要查看文档。我有两个基本问题: > 有没有一种方法可以使用Azure portal以文本格式导出RSA公钥,而不使用API(https://docs.micr

  • 如何为密钥对创建X509证书?(我的类已经有了密钥对,我需要创建一个证书来保存我的公钥,然后将其存储在密钥库中)。 我期望有一个X509证书构造函数能够接收我的公钥,然后通过keystore.set条目(pvtkey,cert)存储它,但我没有发现任何有用的关联新证书和我的密钥对... 任何想法? 编辑:我也试图通过证书链为空,但它不起作用,它看起来像一个错误报告http://bugs.sun.c

  • 我是新来配置Jetty服务器的SSL。我按照digcert的步骤创建了私钥文件、证书请求CSR文件。 我向CA发送了证书请求,并拿回了我签名的CSR。但是CA给我发了一个有两个证书的捆绑包,一个是我的证书,由CA签名,第二个是CA证书。(1star_xyx_abc_comcrt文件,2.CRT文件)。现在我面临着从这些文件创建密钥库的麻烦。 当我按照Oracle文档的步骤4、5和6使用keytoo

  • 我正在尝试用c#签署比特币交易。我有两段代码要完成。我可以使用Bouncy castle创建一组私钥和公钥。我可以将其转换为钱包导入格式ok。 我还可以从ECDSA公钥生成比特币地址。 然而,我想签署一项交易,我所拥有的只是我的私钥。我不想把东西放进钱包里签名。那么,在只提供私钥的情况下,如何生成公钥呢? 我发现了一个javascript方法可以做到这一点: 我在充气城堡看到的唯一方法是生成随机一

  • 我试图签署一个Android应用程序,但显然原始的keystore文件是不可用的。我所得到的只有以下几点: 私钥md5 0123456789abcdef0123456789abcdef<--虚构值,但 证书md5 fedcba9876543210fedcba9876543210<-原始为32个十六进制数字 已签署的生产Android应用程序 是否有任何方法可以从这些资产中重新创建一个keystor