EncryptedPrivateKeyInfoTest.java
上传用户:lior1029
上传日期:2013-05-07
资源大小:209k
文件大小:6k
源码类别:

CA认证

开发平台:

Java

  1. package org.bouncycastle.asn1.test;
  2. import java.io.*;
  3. import org.bouncycastle.asn1.*;
  4. import org.bouncycastle.asn1.pkcs.*;
  5. import org.bouncycastle.asn1.util.*;
  6. import org.bouncycastle.util.test.*;
  7. import org.bouncycastle.util.encoders.*;
  8. /**
  9.  * Test the reading and writing of EncryptedPrivateKeyInfo objects using
  10.  * the test vectors provided at
  11.  * <a href=http://www.rsasecurity.com/rsalabs/pkcs/pkcs-5/index.html>
  12.  * RSA's PKCS5 Page</a>
  13.  * <p>
  14.  * The vectors are Base 64 encoded and encrypted using the password "password"
  15.  * (without quotes). They should all yield the same PrivateKeyInfo object.
  16.  */
  17. public class EncryptedPrivateKeyInfoTest
  18.     implements Test
  19. {
  20.     static byte[] sample1 = Base64.decode(
  21.         "MIIBozA9BgkqhkiG9w0BBQ0wMDAbBgkqhkiG9w0BBQwwDgQIfWBDXwLp4K4CAggA"
  22.       + "MBEGBSsOAwIHBAiaCF/AvOgQ6QSCAWDWX4BdAzCRNSQSANSuNsT5X8mWYO27mr3Y"
  23.       + "9c9LoBVXGNmYWKA77MI4967f7SmjNcgXj3xNE/jmnVz6hhsjS8E5VPT3kfyVkpdZ"
  24.       + "0lr5e9Yk2m3JWpPU7++v5zBkZmC4V/MwV/XuIs6U+vykgzMgpxQg0oZKS9zgmiZo"
  25.       + "f/4dOCL0UtCDnyOSvqT7mCVIcMDIEKu8QbVlgZYBop08l60EuEU3gARUo8WsYQmO"
  26.       + "Dz/ldx0Z+znIT0SXVuOwc+RVItC5T/Qx+aijmmpt+9l14nmaGBrEkmuhmtdvU/4v"
  27.       + "aptewGRgmjOfD6cqK+zs0O5NrrJ3P/6ZSxXj91CQgrThGfOv72bUncXEMNtc8pks"
  28.       + "2jpHFjGMdKufnadAD7XuMgzkkaklEXZ4f5tU6heIIwr51g0GBEGF96gYPFnjnSQM"
  29.       + "75JE02Clo+DfcfXpcybPTwwFg2jd6JTTOfkdf6OdSlA/1XNK43FA");
  30.     static byte[] sample2 = Base64.decode(
  31.         "MIIBpjBABgkqhkiG9w0BBQ0wMzAbBgkqhkiG9w0BBQwwDgQIeFeOWl1jywYCAggA"
  32.       + "MBQGCCqGSIb3DQMHBAjUJ5eGBhQGtQSCAWBrHrRgqO8UUMLcWzZEtpk1l3mjxiF/"
  33.       + "koCMkHsFwowgyWhEbgIkTgbSViK54LVK8PskekcGNLph+rB6bGZ7pPbL5pbXASJ8"
  34.       + "+MkQcG3FZdlS4Ek9tTJDApj3O1UubZGFG4uvTlJJFbF1BOJ3MkY3XQ9Gl1qwv7j5"
  35.       + "6e103Da7Cq9+oIDKmznza78XXQYrUsPo8mJGjUxPskEYlzwvHjKubRnYm/K6RKhi"
  36.       + "5f4zX4BQ/Dt3H812ZjRXrsjAJP0KrD/jyD/jCT7zNBVPH1izBds+RwizyQAHwfNJ"
  37.       + "BFR78TH4cgzB619X47FDVOnT0LqQNVd0O3cSwnPrXE9XR3tPayE+iOB15llFSmi8"
  38.       + "z0ByOXldEpkezCn92Umk++suzIVj1qfsK+bv2phZWJPbLEIWPDRHUbYf76q5ArAr"
  39.       + "u4xtxT/hoK3krEs/IN3d70qjlUJ36SEw1UaZ82PWhakQbdtu39ZraMJB");
  40.     static byte[] sample3 = Base64.decode(
  41.         "MIIBrjBIBgkqhkiG9w0BBQ0wOzAeBgkqhkiG9w0BBQwwEQQIrHyQPBZqWLUCAggA"
  42.       + "AgEQMBkGCCqGSIb3DQMCMA0CAToECEhbh7YZKiPSBIIBYCT1zp6o5jpFlIkgwPop"
  43.       + "7bW1+8ACr4exqzkeb3WflQ8cWJ4cURxzVdvxUnXeW1VJdaQZtjS/QHs5GhPTG/0f"
  44.       + "wtvnaPfwrIJ3FeGaZfcg2CrYhalOFmEb4xrE4KyoEQmUN8tb/Cg94uzd16BOPw21"
  45.       + "RDnE8bnPdIGY7TyL95kbkqH23mK53pi7h+xWIgduW+atIqDyyt55f7WMZcvDvlj6"
  46.       + "VpN/V0h+qxBHL274WA4dj6GYgeyUFpi60HdGCK7By2TBy8h1ZvKGjmB9h8jZvkx1"
  47.       + "MkbRumXxyFsowTZawyYvO8Um6lbfEDP9zIEUq0IV8RqH2MRyblsPNSikyYhxX/cz"
  48.       + "tdDxRKhilySbSBg5Kr8OfcwKp9bpinN96nmG4xr3Tch1bnVvqJzOQ5+Vva2WwVvH"
  49.       + "2JkWvYm5WaANg4Q6bRxu9vz7DuhbJjQdZbxFezIAgrJdSe92B00jO/0Kny1WjiVO"
  50.       + "6DA=");
  51.     public String getName()
  52.     {
  53.         return "EncryptedPrivateKeyInfoTest";
  54.     }
  55.     private TestResult test(
  56.         int     id,
  57.         byte[]  sample)
  58.     {
  59.         ByteArrayInputStream    bIn = new ByteArrayInputStream(sample);
  60.         DERInputStream          dIn = new DERInputStream(bIn);
  61.         EncryptedPrivateKeyInfo info;
  62.         try
  63.         {
  64.             info = new EncryptedPrivateKeyInfo((ASN1Sequence)dIn.readObject());
  65.         }
  66.         catch (Exception e)
  67.         {
  68.             return new SimpleTestResult(false, getName() + ": test " + id + " failed construction - exception " + e.toString());
  69.         }
  70.     
  71.         ByteArrayOutputStream   bOut = new ByteArrayOutputStream();
  72.         DEROutputStream         dOut = new DEROutputStream(bOut);
  73.         try
  74.         {
  75.             dOut.writeObject(info);
  76.         }
  77.         catch (Exception e)
  78.         {
  79.             return new SimpleTestResult(false, getName() + ": test " + id + " failed writing - exception " + e.toString());
  80.         }
  81.         byte[]  bytes = bOut.toByteArray();
  82.         if (bytes.length != sample.length)
  83.         {
  84.             try
  85.             {
  86.                 bIn = new ByteArrayInputStream(bytes);
  87.                 dIn = new DERInputStream(bIn);
  88.                 DERObject   obj = (DERObject)dIn.readObject();
  89.     
  90.                 return new SimpleTestResult(false, getName() + ": test " + id + " length mismatch - expected " + sample.length + System.getProperty("line.separator") + DERDump.dumpAsString(info) + " got " + bytes.length + System.getProperty("line.separator") + DERDump.dumpAsString(obj));
  91.             }
  92.             catch (Exception e)
  93.             {
  94.                 e.printStackTrace();
  95.                 return new SimpleTestResult(false, getName() + ": test " + id + " length mismatch - exception " + e.toString());
  96.             }
  97.         }
  98.         for (int i = 0; i != bytes.length; i++)
  99.         {
  100.             if (bytes[i] != sample[i])
  101.             {
  102.                 return new SimpleTestResult(false, getName() + ": test " + id + " data mismatch");
  103.             }
  104.         }
  105.         return new SimpleTestResult(true, getName() + ": test " + id + " Okay");
  106.     }
  107.     public TestResult perform()
  108.     {
  109.         TestResult  result = test(0, sample1);
  110.         if (!result.isSuccessful())
  111.         {
  112.             return result;
  113.         }
  114.         result = test(1, sample2);
  115.         if (!result.isSuccessful())
  116.         {
  117.             return result;
  118.         }
  119.         result = test(2, sample3);
  120.         if (!result.isSuccessful())
  121.         {
  122.             return result;
  123.         }
  124.         return new SimpleTestResult(true, getName() + ": Okay");
  125.     }
  126.     public static void main(
  127.         String[]    args)
  128.     {
  129.         Test    test = new EncryptedPrivateKeyInfoTest();
  130.         TestResult  result = test.perform();
  131.         System.out.println(result.toString());
  132.     }
  133. }