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 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 92 int getType() 93 { 94 return type; 95 } 96 97 int getDigest() 98 { 99 return digest; 100 } 101 102 int getKeySize() 103 { 104 return keySize; 105 } 106 107 public int getIvSize() 108 { 109 return ivSize; 110 } 111 112 public CipherParameters getParam() 113 { 114 return param; 115 } 116 117 /* (non-Javadoc) 118 * @see javax.crypto.interfaces.PBEKey#getPassword() 119 */ 120 public char[] getPassword() 121 { 122 return pbeKeySpec.getPassword(); 123 } 124 125 /* (non-Javadoc) 126 * @see javax.crypto.interfaces.PBEKey#getSalt() 127 */ 128 public byte[] getSalt() 129 { 130 return pbeKeySpec.getSalt(); 131 } 132 133 /* (non-Javadoc) 134 * @see javax.crypto.interfaces.PBEKey#getIterationCount() 135 */ 136 public int getIterationCount() 137 { 138 return pbeKeySpec.getIterationCount(); 139 } 140 141 public ASN1ObjectIdentifier getOID() 142 { 143 return oid; 144 } 145 146 public void setTryWrongPKCS12Zero(boolean tryWrong) 147 { 148 this.tryWrong = tryWrong; 149 } 150 151 boolean shouldTryWrongPKCS12() 152 { 153 return tryWrong; 154 } 155} 156