blob: 542208bb1e7ab0e554f63d7e5479d53ff694d780 (
plain) (
blame)
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
|
package org.uic.barcode.utils;
import java.security.KeyFactory;
import java.security.NoSuchAlgorithmException;
import java.security.PrivateKey;
import java.security.Provider;
import java.security.PublicKey;
import java.security.Security;
import java.security.spec.InvalidKeySpecException;
import java.security.spec.PKCS8EncodedKeySpec;
import java.security.spec.X509EncodedKeySpec;
public class SecurityUtils {
public static KeyFactory findKeyFactory(String oid, byte[] keyBytes) {
X509EncodedKeySpec keySpec = new X509EncodedKeySpec(keyBytes);
String name = null;
try {
name = AlgorithmNameResolver.getName(AlgorithmNameResolver.TYPE_KEY_GENERATOR_ALG, oid);
} catch (Exception e2) {
return null;
}
if (name == null || name.length() == 0) {
return null;
}
KeyFactory keyFactory = null;
Provider[] provs = Security.getProviders();
for (Provider provider : provs) {
try {
keyFactory = KeyFactory.getInstance(name,provider);
} catch (NoSuchAlgorithmException e1) {
//try next
}
if (keyFactory != null) {
try {
keyFactory.generatePublic(keySpec);
return keyFactory;
} catch (Exception e) {
//try next
}
}
}
return null;
}
public static Provider findPrivateKeyProvider(PrivateKey key) {
String name = key.getAlgorithm();
byte[] keyBytes = key.getEncoded();
PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(keyBytes);
KeyFactory keyFactory = null;
Provider[] provs = Security.getProviders();
for (Provider provider : provs) {
try {
keyFactory = KeyFactory.getInstance(name,provider);
} catch (NoSuchAlgorithmException e1) {
//try next
}
if (keyFactory != null) {
try {
keyFactory.generatePrivate(keySpec);
return provider;
} catch (Exception e) {
provider = null;
//try next
}
}
}
return null;
}
public static PublicKey convertPublicKey(PublicKey key) {
PublicKey publicKey;
try {
publicKey = KeyFactory.getInstance("RSA").generatePublic(new X509EncodedKeySpec(key.getEncoded()));
} catch (InvalidKeySpecException | NoSuchAlgorithmException e) {
return key;
}
return publicKey;
}
public static PublicKey convert(PublicKey key, Provider provider) {
PublicKey publicKey;
try {
publicKey = KeyFactory.getInstance("RSA", provider).generatePublic(new X509EncodedKeySpec(key.getEncoded()));
} catch (InvalidKeySpecException | NoSuchAlgorithmException e) {
return key;
}
return publicKey;
}
public static PrivateKey convertPrivateKey(PrivateKey key) {
PrivateKey privateKey;
try {
privateKey = KeyFactory.getInstance("RSA").generatePrivate(new PKCS8EncodedKeySpec(key.getEncoded()));
} catch (InvalidKeySpecException | NoSuchAlgorithmException e) {
return key;
}
return privateKey;
}
}
|