数字签名无效

时间:2012-02-27 04:53:41

标签: java csr

我为生成.req和.key文件编写了一个示例java程序,然后使用其他应用程序生成了.pfx文件。

将.pfx文件安装到Window XP。没关系。但是将.pfx文件安装到Window 7和Window Server 2008时出错。 此错误此证书的数字签名无效。 为什么会出现此错误?

Java代码:

try {
  keyGen = KeyPairGenerator.getInstance("RSA","BC");
  int keyLength = Integer.parseInt(cbKeylength.getSelectedItem().toString());
  keyGen.initialize(keyLength, new SecureRandom());
  KeyPair keypair = keyGen.generateKeyPair();
  publicKey = keypair.getPublic();
  privateKey = keypair.getPrivate();

  if (getRdSHA1WithRSA().isSelected()) {
     sigAlg = getRdSHA1WithRSA().getText();
  } else if (getRdMC4withRSA().isSelected()) {
     sigAlg = getRdMC4withRSA().getText();
  }
  StringTokenizer token = new StringTokenizer(sigAlg, " ");
  String str = "";
  while (token.hasMoreTokens()) {
    str += token.nextToken();
  }
  sigAlg = str;
  String csr = getCSR(cn, ou, org, loc, state, country,email, sigAlg);

  BufferedOutputStream bos1 = new BufferedOutputStream(
             new FileOutputStream(txtRequest.getText()));
  bos1.write(csr.getBytes());

  bos1.close();

  BufferedWriter bw = new BufferedWriter(new FileWriter(txtPrivateKey.getText()));
  PEMWriter writer = new PEMWriter(bw);
  writer.writeObject(getPrivateKey());

  writer.close();
  bw.close();
 } catch (NoSuchAlgorithmException e) {
    e.printStackTrace();
 } catch (Exception e) {
    e.printStackTrace();
 }


  public String getCSR(String CN, String OU, String O, String L, String S,
            String C, String E, String SigAlg) throws Exception {
    byte[] csr = generatePKCS10(CN, OU, O, L, S, C, E, SigAlg);
    return new String(csr);
  }

    /**
     *
     * @param CN
     *            Common Name, is X.509 speak for the name that distinguishes
     *            the Certificate best, and ties it to your Organization
     * @param OU
     *            Organizational unit
     * @param O
     *            Organization NAME
     * @param L
     *            Location
     * @param S
     *            State
     * @param C
     *            Country
     * @return
     * @throws Exception
     */
    public static byte[] generatePKCS10(String CN, String OU, String O,
            String L, String S, String C,String E, String sigAlg) throws Exception {
        // generate PKCS10 certificate request          
        PKCS10 pkcs10 = new PKCS10(publicKey);
        Signature signature = Signature.getInstance(sigAlg);
        signature.initSign(privateKey);
        // common, orgUnit, org, locality, state, country
        X500Name x500Name = new X500Name(CN, OU, O, L, S, C);

        pkcs10.encodeAndSign(new X500Signer(signature, x500Name));
        ByteArrayOutputStream bs = new ByteArrayOutputStream();
        PrintStream ps = new PrintStream(bs);
        pkcs10.print(ps);
        byte[] c = bs.toByteArray();
        try {
            if (ps != null)
                ps.close();
            if (bs != null)
                bs.close();
        } catch (Throwable th) {
        }
        return c;
    }

此代码有什么问题?请解释一下!

0 个答案:

没有答案