BCPBEKey.java revision e1142c149e244797ce73b0e7fad40816e447a817
1package org.bouncycastle.jcajce.provider.symmetric.util;
2
3import javax.crypto.interfaces.PBEKey;
4import javax.crypto.spec.PBEKeySpec;
5
6import org.bouncycastle.asn1.ASN1ObjectIdentifier;
7import org.bouncycastle.crypto.CipherParameters;
8import org.bouncycastle.crypto.PBEParametersGenerator;
9import org.bouncycastle.crypto.params.KeyParameter;
10import org.bouncycastle.crypto.params.ParametersWithIV;
11
12public class BCPBEKey
13    implements PBEKey
14{
15    String              algorithm;
16    ASN1ObjectIdentifier oid;
17    int                 type;
18    int                 digest;
19    int                 keySize;
20    int                 ivSize;
21    CipherParameters    param;
22    PBEKeySpec          pbeKeySpec;
23    boolean             tryWrong = false;
24
25    /**
26     * @param param
27     */
28    public BCPBEKey(
29        String algorithm,
30        ASN1ObjectIdentifier oid,
31        int type,
32        int digest,
33        int keySize,
34        int ivSize,
35        PBEKeySpec pbeKeySpec,
36        CipherParameters param)
37    {
38        this.algorithm = algorithm;
39        this.oid = oid;
40        this.type = type;
41        this.digest = digest;
42        this.keySize = keySize;
43        this.ivSize = ivSize;
44        this.pbeKeySpec = pbeKeySpec;
45        this.param = param;
46    }
47
48    public String getAlgorithm()
49    {
50        return algorithm;
51    }
52
53    public String getFormat()
54    {
55        return "RAW";
56    }
57
58    public byte[] getEncoded()
59    {
60        if (param != null)
61        {
62            KeyParameter    kParam;
63
64            if (param instanceof ParametersWithIV)
65            {
66                kParam = (KeyParameter)((ParametersWithIV)param).getParameters();
67            }
68            else
69            {
70                kParam = (KeyParameter)param;
71            }
72
73            return kParam.getKey();
74        }
75        else
76        {
77            if (type == PBE.PKCS12)
78            {
79                return PBEParametersGenerator.PKCS12PasswordToBytes(pbeKeySpec.getPassword());
80            }
81            else
82            {
83                return PBEParametersGenerator.PKCS5PasswordToBytes(pbeKeySpec.getPassword());
84            }
85        }
86    }
87
88    int getType()
89    {
90        return type;
91    }
92
93    int getDigest()
94    {
95        return digest;
96    }
97
98    int getKeySize()
99    {
100        return keySize;
101    }
102
103    public int getIvSize()
104    {
105        return ivSize;
106    }
107
108    public CipherParameters getParam()
109    {
110        return param;
111    }
112
113    /* (non-Javadoc)
114     * @see javax.crypto.interfaces.PBEKey#getPassword()
115     */
116    public char[] getPassword()
117    {
118        return pbeKeySpec.getPassword();
119    }
120
121    /* (non-Javadoc)
122     * @see javax.crypto.interfaces.PBEKey#getSalt()
123     */
124    public byte[] getSalt()
125    {
126        return pbeKeySpec.getSalt();
127    }
128
129    /* (non-Javadoc)
130     * @see javax.crypto.interfaces.PBEKey#getIterationCount()
131     */
132    public int getIterationCount()
133    {
134        return pbeKeySpec.getIterationCount();
135    }
136
137    public ASN1ObjectIdentifier getOID()
138    {
139        return oid;
140    }
141
142    public void setTryWrongPKCS12Zero(boolean tryWrong)
143    {
144        this.tryWrong = tryWrong;
145    }
146
147    boolean shouldTryWrongPKCS12()
148    {
149        return tryWrong;
150    }
151}
152