如何在Java中使用X509证书签署文本文件?

发布于 2025-01-11 04:57:40 字数 1605 浏览 2 评论 0原文

我已经导出了 3 类数字证书,并且可以从名为 cert 的对象中获取详细信息,如下所示。我打算使用此证书签署一个文本文件。但是,由于我在没有私钥的情况下导出了此证书,因此无法继续。我在图像下方分享了带有标签的片段。

这是保存为 .txt 的文件和标签,将是签名过程的结果。所以基本上原始有效负载在签名过程之前只会有其他标签。

我的问题:

  1. 我是否走在从后端对文本文件进行数字签名的正确轨道上?

  2. 是否应该使用 Javascript 在客户端处理证书的访问

     try (InputStream inStream = new FileInputStream("D:\\新文件夹\\TestCertificate.cer")) {
          CertificateFactory cf = CertificateFactory.getInstance("X.509");
          X509Certificate cert = (X509Certificate)cf.generateCertificate(inStream);
    
          cert.checkValidity();
          System.out.println("这是公钥:" + cert.getPublicKey());
          System.out.println("这是证书类型:" + cert.getType());
    
          // 用于访问 txt 文件并使用证书对其进行签名的代码。
    
      } catch (CertificateException e) {
         // TODO 自动生成的 catch 块
         e.printStackTrace();
     } catch (FileNotFoundException e1) {
         // TODO 自动生成的 catch 块
         e1.printStackTrace();
     } catch (IOException e1) {
         // TODO 自动生成的 catch 块
         e1.printStackTrace();
     }
    

在此处输入图像描述

<MessageInfo>
<SenderIden>XSB000000000</SenderIden>
<SenderSP>100023193</SenderSP>
<Data><![CDATA[{IFN515}{XSB000000000}{XYZ12345} 
  {
  :16R:GENL
  :20C::SEME//A23P8W4
  :23G:NEWM
  :98A::PREP//20220221
  :22F::TRTR//TRAD
   ]]></Data>
  <SubscriberDS>MIIJuwYJKoZIhvcNAQcCoI</SubscriberDS>
  <ServiceProviderDS>MIIJuwYJKoZI</ServiceProviderDS>
  </MessageInfo>

I have exported a class 3 Digital certificate and I am able to get the details from the object named cert as seen below. I intend to sign a text file using this certificate. However since I exported this certificate without the private key, I am unable to proceed. I have shared a snippet below the image which has tags.

This is the file saved as .txt and the tags and will be the result of the signing process. So basically the raw payload will only have the other tags before the signing process.

My Questions:

  1. Am I on the right track to digitally sign the text file from backend?

  2. Should the access of the certificate be handled on the client side using Javascript

     try (InputStream inStream = new FileInputStream("D:\\New folder\\TestCertificate.cer")) {
          CertificateFactory cf = CertificateFactory.getInstance("X.509");
          X509Certificate cert = (X509Certificate)cf.generateCertificate(inStream);
    
          cert.checkValidity();
          System.out.println("This is public key :  " + cert.getPublicKey());
          System.out.println("This is certificate type: " + cert.getType());
    
          // Code to access a txt file and sign it with the certificate.
    
      } catch (CertificateException e) {
         // TODO Auto-generated catch block
         e.printStackTrace();
     } catch (FileNotFoundException e1) {
         // TODO Auto-generated catch block
         e1.printStackTrace();
     } catch (IOException e1) {
         // TODO Auto-generated catch block
         e1.printStackTrace();
     }
    

enter image description here

<MessageInfo>
<SenderIden>XSB000000000</SenderIden>
<SenderSP>100023193</SenderSP>
<Data><![CDATA[{IFN515}{XSB000000000}{XYZ12345} 
  {
  :16R:GENL
  :20C::SEME//A23P8W4
  :23G:NEWM
  :98A::PREP//20220221
  :22F::TRTR//TRAD
   ]]></Data>
  <SubscriberDS>MIIJuwYJKoZIhvcNAQcCoI</SubscriberDS>
  <ServiceProviderDS>MIIJuwYJKoZI</ServiceProviderDS>
  </MessageInfo>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文