blob: 2e36fefc8d0e9950ba8e90fb761d27d3298b534a (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
|
package org.bouncycastle.jcajce.provider.symmetric.util;
import java.lang.reflect.Constructor;
import java.security.InvalidKeyException;
import java.security.spec.InvalidKeySpecException;
import java.security.spec.KeySpec;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactorySpi;
import javax.crypto.spec.DESKeySpec;
import javax.crypto.spec.PBEKeySpec;
import javax.crypto.spec.SecretKeySpec;
import org.bouncycastle.asn1.ASN1ObjectIdentifier;
import org.bouncycastle.crypto.CipherParameters;
import org.bouncycastle.crypto.params.DESParameters;
import org.bouncycastle.crypto.params.KeyParameter;
import org.bouncycastle.crypto.params.ParametersWithIV;
public class BaseSecretKeyFactory
extends SecretKeyFactorySpi
implements PBE
{
protected String algName;
protected ASN1ObjectIdentifier algOid;
protected BaseSecretKeyFactory(
String algName,
ASN1ObjectIdentifier algOid)
{
this.algName = algName;
this.algOid = algOid;
}
protected SecretKey engineGenerateSecret(
KeySpec keySpec)
throws InvalidKeySpecException
{
if (keySpec instanceof SecretKeySpec)
{
return (SecretKey)keySpec;
}
throw new InvalidKeySpecException("Invalid KeySpec");
}
protected KeySpec engineGetKeySpec(
SecretKey key,
Class keySpec)
throws InvalidKeySpecException
{
if (keySpec == null)
{
throw new InvalidKeySpecException("keySpec parameter is null");
}
if (key == null)
{
throw new InvalidKeySpecException("key parameter is null");
}
if (SecretKeySpec.class.isAssignableFrom(keySpec))
{
return new SecretKeySpec(key.getEncoded(), algName);
}
try
{
Class[] parameters = { byte[].class };
Constructor c = keySpec.getConstructor(parameters);
Object[] p = new Object[1];
p[0] = key.getEncoded();
return (KeySpec)c.newInstance(p);
}
catch (Exception e)
{
throw new InvalidKeySpecException(e.toString());
}
}
protected SecretKey engineTranslateKey(
SecretKey key)
throws InvalidKeyException
{
if (key == null)
{
throw new InvalidKeyException("key parameter is null");
}
if (!key.getAlgorithm().equalsIgnoreCase(algName))
{
throw new InvalidKeyException("Key not of type " + algName + ".");
}
return new SecretKeySpec(key.getEncoded(), algName);
}
}
|