1 package org.bouncycastle.jcajce.provider.symmetric.util; 2 3 import javax.crypto.interfaces.PBEKey; 4 import javax.crypto.spec.PBEKeySpec; 5 6 import org.bouncycastle.asn1.ASN1ObjectIdentifier; 7 import org.bouncycastle.crypto.CipherParameters; 8 import org.bouncycastle.crypto.PBEParametersGenerator; 9 import org.bouncycastle.crypto.params.KeyParameter; 10 import org.bouncycastle.crypto.params.ParametersWithIV; 11 12 public 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 */ BCPBEKey( String algorithm, ASN1ObjectIdentifier oid, int type, int digest, int keySize, int ivSize, PBEKeySpec pbeKeySpec, CipherParameters param)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 getAlgorithm()48 public String getAlgorithm() 49 { 50 return algorithm; 51 } 52 getFormat()53 public String getFormat() 54 { 55 return "RAW"; 56 } 57 getEncoded()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 if (type == PBE.PKCS5S2_UTF8) 82 { 83 return PBEParametersGenerator.PKCS5PasswordToUTF8Bytes(pbeKeySpec.getPassword()); 84 } 85 else 86 { 87 return PBEParametersGenerator.PKCS5PasswordToBytes(pbeKeySpec.getPassword()); 88 } 89 } 90 } 91 getType()92 int getType() 93 { 94 return type; 95 } 96 getDigest()97 int getDigest() 98 { 99 return digest; 100 } 101 getKeySize()102 int getKeySize() 103 { 104 return keySize; 105 } 106 getIvSize()107 public int getIvSize() 108 { 109 return ivSize; 110 } 111 getParam()112 public CipherParameters getParam() 113 { 114 return param; 115 } 116 117 /* (non-Javadoc) 118 * @see javax.crypto.interfaces.PBEKey#getPassword() 119 */ getPassword()120 public char[] getPassword() 121 { 122 return pbeKeySpec.getPassword(); 123 } 124 125 /* (non-Javadoc) 126 * @see javax.crypto.interfaces.PBEKey#getSalt() 127 */ getSalt()128 public byte[] getSalt() 129 { 130 return pbeKeySpec.getSalt(); 131 } 132 133 /* (non-Javadoc) 134 * @see javax.crypto.interfaces.PBEKey#getIterationCount() 135 */ getIterationCount()136 public int getIterationCount() 137 { 138 return pbeKeySpec.getIterationCount(); 139 } 140 getOID()141 public ASN1ObjectIdentifier getOID() 142 { 143 return oid; 144 } 145 setTryWrongPKCS12Zero(boolean tryWrong)146 public void setTryWrongPKCS12Zero(boolean tryWrong) 147 { 148 this.tryWrong = tryWrong; 149 } 150 shouldTryWrongPKCS12()151 boolean shouldTryWrongPKCS12() 152 { 153 return tryWrong; 154 } 155 156 // BEGIN android-added getPbeKeySpec()157 public PBEKeySpec getPbeKeySpec() { 158 return pbeKeySpec; 159 } 160 // END android-added 161 } 162