FileOutputStream和PrintWriter之间的区别

时间:2013-01-22 07:50:51

标签: java fileoutputstream printwriter

我正在我的应用程序中实现SSL证书和密钥。我使用CertAndKeyGen类创建了私钥。我试图用密码加密私钥,我通过PBE和Cipher类实现了它。我想将加密的私钥写入PEM格式的文​​件中。我尝试使用FileOutputStream,但PrintWriter不能正常工作。

以下是我的代码,

    final CertAndKeyGen keypair = new CertAndKeyGen("RSA", "SHA1WithRSA", null);
    keypair.generate(1024);
    final PrivateKey privKey = keypair.getPrivateKey();
    byte[] encodedprivkey = privKey.getEncoded();
    String MYPBEALG = "PBEWithSHA1AndDESede";
    String password = "test123";
    int count = 20;// hash iteration count
    Random random = new Random();
    byte[] salt = new byte[8];
    random.nextBytes(salt);
    PBEParameterSpec pbeParamSpec = new PBEParameterSpec(salt, count);
    PBEKeySpec pbeKeySpec = new PBEKeySpec(password.toCharArray());
    SecretKeyFactory keyFac = SecretKeyFactory.getInstance(MYPBEALG);
    SecretKey pbeKey = keyFac.generateSecret(pbeKeySpec);
    Cipher pbeCipher = Cipher.getInstance(MYPBEALG);
    // Initialize PBE Cipher with key and parameters
    pbeCipher.init(Cipher.ENCRYPT_MODE, pbeKey, pbeParamSpec);
    // Encrypt the encoded Private Key with the PBE key
    byte[] ciphertext = pbeCipher.doFinal(encodedprivkey);
    // Now construct  PKCS #8 EncryptedPrivateKeyInfo object
    AlgorithmParameters algparms = AlgorithmParameters.getInstance(MYPBEALG);
    algparms.init(pbeParamSpec);
    EncryptedPrivateKeyInfo encinfo = new EncryptedPrivateKeyInfo(algparms,ciphertext);
    byte[] encryptedPkcs8 = encinfo.getEncoded();

    // Now I am writing the encrypted private key into a file.
    // Using FileOutputStream 

    FileOutputStream out = new FileOutputStream("usingOutEncrypedPrivkey");
    out.write(Base64.encodeBase64(encryptedPkcs8, true));
    out.flush();
    out.close();

    // Using PrintWriter 
    PrintWriter pw = new PrintWriter("usingPwEncryptedPrivKey");
    pw.println("-----BEGIN "+ privKey.getAlgorithm() + " PRIVATE KEY-----");
    pw.println(Base64.encodeBase64(encryptedPkcs8));
    pw.println("-----END "+ privKey.getAlgorithm() +" PRIVATE KEY-----");
    pw.close();

以下是保存的文件,

    usingOutEncrypedPrivkey  // Which was saved using FileOutputStream

    MIICoTAbBgoqhkiG9w0BDAEDMA0ECL4xgraq2hXxAgEUBIICgFENaB8EA/kR0ymSC8vcyj1fNFbP
    iR+mXkBk7aH3eF7fpP8yqCvtN0/0VqHi/w/Z2CLgiib2s/zuiVPtWI8vsRRPXmD9PYxZp3ilLpD4
                            .....
                            .....   
    9nH8HdQf584c3sKYEErDQvJR2SmbUPtNq4cB6ocUuiOTztBqRXAHeaWavnqHFxHUT7c=


    usingPwEncryptedPrivKey  // Which was saved using PrintWriter
   -----BEGIN RSA PRIVATE KEY-----
   [B@19e3118a
   -----END RSA PRIVATE KEY-----

为什么PrintWriter写为“[B @ 19e3118a”,而不是像FileOutputStream这样的字节。我想使用PrintWriter,因为我想使用PrintWriter.println()函数。请帮帮我。提前谢谢。

3 个答案:

答案 0 :(得分:7)

  • OutputStream.write(byte[])将字节写为二进制数据。
  • PrintWriter.print(byte[])在数组上调用toString()并写出结果。 [B@19e3118a是数组的toString()表示形式。

鉴于字符串只包含ASCII字符,您可以在数组上使用String(byte[]),然后打印结果。

或者,如果您正在使用Apache Commons Base64课程,则应该使用encodeBase64String()方法而不是encodeBase64() [感谢@SimonC]。

答案 1 :(得分:2)

[B@19e3118a是在字节数组上调用toString()的结果。我不知道你正在使用哪个Base64类,但如果你想使用Writer,你应该找到一个将字节数组编码为字符串,而不是字节数组到其他字节数组。

答案 2 :(得分:2)

两种方式

  1. 使用OutputStream.write(),然后自己拆分。
  2. 使用PrintWrite.println(),然后你需要将原始字节编码成字符串并自己分割行...