public static void main(String[] args) throws Exception {

    byte[] iv_1 = {
      (byte) 0x11, (byte) 0x11, (byte) 0x11, (byte) 0x11,
      (byte) 0x11, (byte) 0x11, (byte) 0x11, (byte) 0x11,
      (byte) 0x33, (byte) 0x33
    };

    // check that RC2 is supported by our provider
    AlgorithmParameters rc2Params = AlgorithmParameters.getInstance("RC2", "SunJCE");

    // check that getAlgorithm returns "RC2"
    if (!rc2Params.getAlgorithm().equals("RC2")) {
      throw new Exception(
          "getAlgorithm() returned " + rc2Params.getAlgorithm() + " instead of RC2");
    }

    // test parameters with effective key size and iv
    byte[] encoded = testParams(rc2Params, new RC2ParameterSpec(2, iv_1));

    // test parameters with just iv
    encoded = testParams(AlgorithmParameters.getInstance("RC2"), new RC2ParameterSpec(0, iv_1));

    // test vectors in RFC 2268
    runTests(tests);
  }
示例#2
0
 /**
  * Create a new <code>EncryptedPrivateKeyInfo</code> object from raw encrypted data and the
  * parameters used for encryption.
  *
  * <p>The <code>encryptedData</code> array is cloned.
  *
  * @param params The encryption algorithm parameters.
  * @param encryptedData The encrypted key data.
  * @throws java.lang.IllegalArgumentException If the <code>encryptedData</code> array is empty
  *     (zero-length).
  * @throws java.security.NoSuchAlgorithmException If the algorithm specified in the parameters is
  *     not supported.
  * @throws java.lang.NullPointerException If <code>encryptedData</code> is null.
  */
 public EncryptedPrivateKeyInfo(AlgorithmParameters params, byte[] encryptedData)
     throws IllegalArgumentException, NoSuchAlgorithmException {
   if (encryptedData.length == 0) {
     throw new IllegalArgumentException("0-length encryptedData");
   }
   this.params = params;
   algName = params.getAlgorithm();
   algOid = getOid(algName);
   this.encryptedData = (byte[]) encryptedData.clone();
 }
  /**
   * Constructs an <code>EncryptedPrivateKeyInfo</code> from the encryption algorithm parameters and
   * the encrypted data.
   *
   * <p>Note: the <code>encrypedData</code> is cloned when constructing this object.
   *
   * @param algParams the algorithm parameters for the encryption algorithm. <code>
   *     algParams.getEncoded()</code> should return the ASN.1 encoded bytes of the <code>parameters
   *     </code> field of the <code>AlgorithmIdentifer</code> component of the <code>
   *     EncryptedPrivateKeyInfo</code> type.
   * @param encryptedData encrypted data.
   * @exception NullPointerException if <code>algParams</code> or <code>encryptedData</code> is
   *     null.
   * @exception IllegalArgumentException if <code>encryptedData</code> is empty, i.e. 0-length.
   * @exception NoSuchAlgorithmException if the specified algName of the specified <code>algParams
   *     </code> parameter is not supported.
   */
  public EncryptedPrivateKeyInfo(AlgorithmParameters algParams, byte[] encryptedData)
      throws NullPointerException, IllegalArgumentException, NoSuchAlgorithmException {
    if (algParams == null || encryptedData == null) {
      throw new NullPointerException("parameters null");
    }

    org.bouncycastle.asn1.x509.AlgorithmIdentifier kAlgId = null;

    try {
      ByteArrayInputStream bIn = new ByteArrayInputStream(algParams.getEncoded());
      ASN1InputStream dIn = new ASN1InputStream(bIn);

      kAlgId =
          new AlgorithmIdentifier(
              new DERObjectIdentifier(algParams.getAlgorithm()), dIn.readObject());
    } catch (IOException e) {
      throw new IllegalArgumentException("error in encoding: " + e.toString());
    }

    infoObj =
        new org.bouncycastle.asn1.pkcs.EncryptedPrivateKeyInfo(
            kAlgId, (byte[]) encryptedData.clone());
    algP = this.getParameters();
  }