java 加解密

1、PKCS8EncodedKeySpec:  封裝私鑰的類,它的結構:

 * PrivateKeyInfo ::= SEQUENCE {
 *   version Version,
 *   privateKeyAlgorithm PrivateKeyAlgorithmIdentifier,
 *   privateKey PrivateKey,
 *   attributes [0] IMPLICIT Attributes OPTIONAL }
 *
 * Version ::= INTEGER
 *
 * PrivateKeyAlgorithmIdentifier ::= AlgorithmIdentifier
 *
 * PrivateKey ::= OCTET STRING
 *
 * Attributes ::= SET OF Attribute

構造方式:  PKCS8EncodedKeySpec pkcs8endodekeyspec = new PKCS8EncodedKeySpec(byte[] data);

轉化成Privatekey: KeyFactory factory = KeyFactory.getInstance("RSA");

   PrivateKey privatekey =  factory.generatePrivate(pkcs8endodekeyspec);

2、EncryptedPrivateKeyInfo:封裝加密後的私鑰和創建它的加密算法信息。package encrypted private key data with details of the encryption algorithm used to create it

EncryptedPrivateKeyInfo ::= SEQUENCE {
                               encryptionAlgorithm AlgorithmIdentifier,
                               encryptedData EncryptedData }
EncryptedData ::= OCTET STRING

構造方式:它有三個構造方法

    第一個:  /**
     * Constructs (i.e., parses) an <code>EncryptedPrivateKeyInfo</code> from
     * its ASN.1 encoding.
     * @param encoded the ASN.1 encoding of this object. The contents of
     * the array are copied to protect against subsequent modification.
     * @exception NullPointerException if the <code>encoded</code> is null.
     * @exception IOException if error occurs when parsing the ASN.1 encoding.
     */
    public EncryptedPrivateKeyInfo(byte[] encoded) throws IOException { }

使用ans1編碼的數組

第二個:

/**
     * Constructs an <code>EncryptedPrivateKeyInfo</code> from the
     * encryption algorithm name and the encrypted data.
     *
     * <p>Note: This constructor will use null as the value of the
     * algorithm parameters. If the encryption algorithm has
     * parameters whose value is not null, a different constructor,
     * e.g. EncryptedPrivateKeyInfo(AlgorithmParameters, byte[]),
     * should be used.
     *
     * @param algName encryption algorithm name. See Appendix A in the
     * <a href=
     *   "{@docRoot}/../technotes/guides/security/crypto/CryptoSpec.html#AppA">
     * Java Cryptography Architecture Reference Guide</a>
     * for information about standard Cipher algorithm names.
     * @param encryptedData encrypted data. The contents of
     * <code>encrypedData</code> are copied to protect against subsequent
     * modification when constructing this object.
     * @exception NullPointerException if <code>algName</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 is
     * not supported.
     */
    public EncryptedPrivateKeyInfo(String algName, byte[] encryptedData)
        throws NoSuchAlgorithmException
    { }使用加密算法的名字和加密後的數據

第三個:

/**
     * Constructs an <code>EncryptedPrivateKeyInfo</code> from the
     * encryption algorithm parameters and the encrypted data.
     *
     * @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. The contents of
     * <code>encrypedData</code> are copied to protect against
     * subsequent modification when constructing this object.
     * @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 NoSuchAlgorithmException
    { }使用加密算法參數和加密後的數據。 加密算法參數的getEncoded()方法應該返回ans1編碼格式的數據

 

使用方式:

EncryptedPrivateKeyInfo info = new EncryptedPrivateKeyInfo(cipher.getParameters(),wrapKey);

  PKCS8EncodedKeySpec pkcs8encodekeyspec = info.getKeySpec(cipher);
  KeyFactory fac = KeyFactory.getInstance("RSA");
  PrivateKey keypri = fac.generatePrivate(pkcs8encodekeyspec);

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章