Java Tutorial/Security/Provider
Содержание
- 1 extends java.security.Provider
- 2 extends Provider
- 3 Get a Provider
- 4 Get cryptographic security providers
- 5 Get Provider Info
- 6 Listing All Available Cryptographic Services
- 7 List the available capabilities for ciphers, key agreement, macs, message digests, signatures and other objects in the BC provider.
- 8 Return the available implementations for a service type
- 9 Security.getProviders()
extends java.security.Provider
import java.security.AccessController;
import java.security.PrivilegedAction;
public final class ECCProvider extends java.security.Provider {
private static final String INFO = "jexp"s provider";
public ECCProvider() {
super("jexp", 1.0, INFO);
AccessController.doPrivileged(new PrivilegedAction() {
public Object run() {
put("KeyFactory.ECC", "com.jexpKeyFactory");
put("KeyPairGenerator.ECC", "com.jexpKeyPairGenerator");
return null;
}
});
}
}
extends Provider
import java.security.Provider;
public class YourOwnProvider extends Provider {
public YourOwnProvider() {
super("YourOwn", 1.0, "YourOwn Security Provider v1.0");
put("KeyGenerator.XOR", "yourPackageName.XORKeyGenerator");
put("KeyPairGenerator.YourOwn", "yourPackageName.YourOwnKeyPairGenerator");
put("KeyFactory.YourOwn", "yourPackageName.YourOwnKeyFactory");
put("MessageDigest.YourOwn", "yourPackageName.YourOwnMessageDigest");
put("Signature.YourOwnwithSHA", "yourPackageName.YourOwnSignature");
put("Cipher.XOR", "yourPackageName.XORCipher");
put("KeyManagerFactory.YourOwn", "yourPackageName.SSLKeyManagerFactory");
put("Alg.Alias.MessageDigest.SHA-1", "SHA");
}
public static final synchronized void verifyForJCE() {
throw new SecurityException("Can"t verify for JCE");
}
}
Get a Provider
import java.security.Key;
import java.security.Provider;
import java.security.SecureRandom;
import java.security.Security;
import javax.crypto.Cipher;
public final class MainClass {
String providerName = "Rot13Provider";
String algorithmName = "ROT13";
public static void main(String[] args) throws Exception {
Provider p = Security.getProvider("Rot13Provider");
System.out.println("Provider name: " + p.getName());
System.out.println("Provider version: " + p.getVersion());
System.out.println("Provider information: " + p.getInfo());
Cipher cipher = Cipher.getInstance("ROT13", "Rot13Provider");
System.out.println("Cipher: " + cipher.getAlgorithm());
String testString = "This is a test!";
cipher.init(Cipher.ENCRYPT_MODE, (Key) null, new SecureRandom());
byte[] b1 = cipher.doFinal(testString.getBytes());
cipher.init(Cipher.DECRYPT_MODE, (Key) null, new SecureRandom());
byte[] b2 = cipher.doFinal(b1);
System.out.println("Decrypted data as a String: " + new String(b2));
}
}
Get cryptographic security providers
import java.security.Provider;
import java.security.Security;
import java.util.HashSet;
import java.util.Set;
public class Main {
public static void main(String[] args) {
Set<Object> result = new HashSet<Object>();
Provider[] providers = Security.getProviders();
for (Provider provider : providers) {
Set<Object> keys = provider.keySet();
for (Object key : keys) {
String data = (String) key;
data = data.split(" ")[0];
if (data.startsWith("Alg.Alias")) {
data = data.substring(10);
}
data = data.substring(0, data.indexOf("."));
result.add(data);
}
}
for (Object o : result) {
System.out.println("Service Type = " + o);
}
}
}
Get Provider Info
import java.security.Provider;
import java.security.Security;
public class MainClass {
public static void main(String[] args) {
System.out.println("Providers installed on your system:");
Provider[] providerList = Security.getProviders();
for (int i = 0; i < providerList.length; i++) {
System.out.println("[" + (i + 1) + "] - Provider name: " + providerList[i].getName());
System.out.println("Provider version number: " + providerList[i].getVersion());
System.out.println("Provider information:\n" + providerList[i].getInfo());
}
}
}
/*Providers installed on your system:
[1] - Provider name: SUN
Provider version number: 1.6
Provider information:
SUN (DSA key/parameter generation; DSA signing; SHA-1, MD5 digests; SecureRandom; X.509 certificates; JKS keystore; PKIX CertPathValidator; PKIX CertPathBuilder; LDAP, Collection CertStores, JavaPolicy Policy; JavaLoginConfig Configuration)
[2] - Provider name: SunRsaSign
Provider version number: 1.5
Provider information:
Sun RSA signature provider
[3] - Provider name: SunJSSE
Provider version number: 1.6
Provider information:
Sun JSSE provider(PKCS12, SunX509 key/trust factories, SSLv3, TLSv1)
[4] - Provider name: SunJCE
Provider version number: 1.6
Provider information:
SunJCE Provider (implements RSA, DES, Triple DES, AES, Blowfish, ARCFOUR, RC2, PBE, Diffie-Hellman, HMAC)
[5] - Provider name: SunJGSS
Provider version number: 1.0
Provider information:
Sun (Kerberos v5, SPNEGO)
[6] - Provider name: SunSASL
Provider version number: 1.5
Provider information:
Sun SASL provider(implements client mechanisms for: DIGEST-MD5, GSSAPI, EXTERNAL, PLAIN, CRAM-MD5; server mechanisms for: DIGEST-MD5, GSSAPI, CRAM-MD5)
[7] - Provider name: XMLDSig
Provider version number: 1.0
Provider information:
XMLDSig (DOM XMLSignatureFactory; DOM KeyInfoFactory)
[8] - Provider name: SunPCSC
Provider version number: 1.6
Provider information:
Sun PC/SC provider
[9] - Provider name: SunMSCAPI
Provider version number: 1.6
Provider information:
Sun"s Microsoft Crypto API provider
*/
Listing All Available Cryptographic Services
import java.security.Provider;
import java.security.Security;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;
public class Main {
public static void main(String[] argv) throws Exception {
Set result = new HashSet();
Provider[] providers = Security.getProviders();
for (int i = 0; i < providers.length; i++) {
Set keys = providers[i].keySet();
for (Iterator it = keys.iterator(); it.hasNext();) {
String key = (String) it.next();
key = key.split(" ")[0];
if (key.startsWith("Alg.Alias.")) {
// Strip the alias
key = key.substring(10);
}
int ix = key.indexOf(".");
result.add(key.substring(0, ix));
}
}
System.out.println(result);
}
}
[KeyFactory,
TransformService,
CertPathBuilder,
Cipher,
SecureRandom,
Signature,
AlgorithmParameterGenerator,
KeyPairGenerator,
XMLSignatureFactory,
CertificateFactory,
MessageDigest,
KeyInfoFactory,
KeyAgreement,
CertStore,
Configuration,
SSLContext,
SaslServerFactory,
AlgorithmParameters,
TrustManagerFactory,
GssApiMechanism,
TerminalFactory,
Mac,
KeyGenerator,
Policy,
CertPathValidator,
SaslClientFactory,
SecretKeyFactory,
KeyManagerFactory,
KeyStore,
Provider]
List the available capabilities for ciphers, key agreement, macs, message digests, signatures and other objects in the BC provider.
import java.security.Provider;
import java.security.Security;
import java.util.Iterator;
public class MainClass {
public static void main(String[] args) {
Provider provider = Security.getProvider("BC");
Iterator it = provider.keySet().iterator();
while (it.hasNext()) {
String entry = (String) it.next();
if (entry.startsWith("Alg.Alias.")) {
entry = entry.substring("Alg.Alias.".length());
}
System.out.println(entry);
}
}
}
Return the available implementations for a service type
import java.security.Provider;
import java.security.Security;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;
public class Main {
public static void main(String[] argv) throws Exception {
Set result = new HashSet();
String serviceType = "KeyFactory";
Provider[] providers = Security.getProviders();
for (int i = 0; i < providers.length; i++) {
Set keys = providers[i].keySet();
for (Iterator it = keys.iterator(); it.hasNext();) {
String key = (String) it.next();
key = key.split(" ")[0];
if (key.startsWith(serviceType + ".")) {
result.add(key.substring(serviceType.length() + 1));
} else if (key.startsWith("Alg.Alias." + serviceType + ".")) {
result.add(key.substring(serviceType.length() + 11));
}
}
}
System.out.println(result);
}
}
[1.2.840.113549.1.3.1,
OID.1.2.840.113549.1.1,
1.2.840.113549.1.1,
OID.1.2.840.113549.1.3.1,
1.3.14.3.2.12,
DSA, DiffieHellman, RSA, DH, 1.2.840.10040.4.1]
Security.getProviders()
import java.security.Provider;
import java.security.Security;
import java.util.Enumeration;
public class MainClass {
public static void main(String args[]) throws Exception{
Provider p[] = Security.getProviders();
for (int i = 0; i < p.length; i++) {
System.out.println(p[i]);
for (Enumeration e = p[i].keys(); e.hasMoreElements();)
System.out.println("\t" + e.nextElement());
}
}
}