Java encryption type and algorithm name, java Encryption Algorithm
There are various encryption methods in the project, but they have never been carefully studied. Generally, it is only copy. I have encountered some problems over the past few days. I have read the encryption code and I feel a little confused.
We know that jdk has encapsulated many algorithms for us. But I have not checked which algorithms are encapsulated and how to remove them. I followed the source code today and probably got it.
Start with the following lines of code:
KeyGenerator kgen = KeyGenerator.getInstance("AES");SecureRandom secureRandom = SecureRandom.getInstance("SHA1PRNG");java.security.MessageDigest md = java.security.MessageDigest.getInstance("MD5");
For AES encryption, we use KeyGenerator kgen = KeyGenerator. getInstance ("AES");, MD5 we use java. security. messageDigest md = java. security. messageDigest. getInstance ("MD5"); here we can see from the method that the java class is found by an algorithm name, such as AES, but the source code does not wrap the algorithm name as enumeration, we cannot know that KeyGenerator not only has the AES algorithm, but also those algorithms, and how to obtain the KeyGenerator algorithm name. For example, if AES is not fully capitalized, will there be problems, and if KeyGenerator has MD5.
Open the KeyGenerator. getInstance () method and check its source code.
public static final KeyGenerator getInstance(String paramString) throws NoSuchAlgorithmException { return new KeyGenerator(paramString); }
We found that the constructor is called directly to view the constructor:
private KeyGenerator(String paramString) throws NoSuchAlgorithmException { this.algorithm = paramString; List localList = GetInstance.getServices("KeyGenerator", paramString); this.serviceIterator = localList.iterator(); this.initType = 1; if (nextSpi(null, false) == null) { throw new NoSuchAlgorithmException(paramString + " KeyGenerator not available"); } if ((!skipDebug) && (pdebug != null)) { pdebug.println("KeyGenerator." + paramString + " algorithm from: " + this.provider .getName()); } }
The constructor is actually found through GetInstance. getServices ("KeyGenerator", paramString) and continues to follow up.
public Provider.Service getService(String paramString1, String paramString2) { for (int i = 0; i < this.configs.length; i++) { Provider localProvider = getProvider(i); Provider.Service localService = localProvider.getService(paramString1, paramString2); if (localService != null) { return localService; } } return null; }
GetInstance. getServices actually traverses all providers and returns the first Provide algorithm Service in order ). We can see that two parameters are required to search for a service. The first parameter is type, such as "KeyGenerator", and the second parameter is the algorithm name, "AES ". Now we only need to know which Provide are available and which Provider. Service is available in each Provide.
In the jdk API, check the KeyGenerator. getInstance method. The following message is displayed:
We can write a local method to traverse all jdk algorithms:
Package com. hongkang. test; import java. security. provider; import java. security. security; import java. security. provider. service; public class TestSecurity {public static void main (String [] args) {Provider [] providers = Security. getProviders (); for (Provider p: providers) {System. out. println ("provider name:" + p. getName (); for (Service s: p. getServices () {System. out. println ("type:" + s. getType () + ", algorithm:" + s. getAlgorithm ();} System. out. println ("--------------------------");}}}
Output result:
Provider name: SUN type: SecureRandom, algorithm: SHA1PRNG type: Signature, algorithm: SHA1withDSA type: Signature, algorithm: NONEwithDSA type: KeyPairGenerator, algorithm: DSA type: MessageDigest, algorithm: MD2 type: MessageDigest, algorithm: MD5 type: MessageDigest, algorithm: SHA type: MessageDigest, algorithm: SHA-256 type: MessageDigest, algorithm: SHA-384 type: MessageDigest, algorithm: SHA-512 type: AlgorithmParameterGenerator, algorithm: DSA type: AlgorithmParameters, algorithm: DSA type: KeyFactory, algorithm: DSA type: CertificateFactory, algorithm: X.509 type: KeyStore, algorithm: JKS type: KeyStore, algorithm: CaseExactJKS type: policy, algorithm: javapoicy type: Configuration, algorithm: JavaLoginConfig type: CertPathBuilder, algorithm: PKIX type: CertPathValidator, algorithm: PKIX type: CertStore, algorithm: LDAP type: CertStore, algorithm: collection type: CertStore, algorithm: com. sun. security. indexedCollection receiver provider name: SunRsaSign type: KeyFactory, algorithm: RSA type: KeyPairGenerator, algorithm: RSA type: Signature, algorithm: MD2withRSA type: Signature, algorithm: MD5withRSA type: Signature, algorithm: SHA1withRSA type: Signature, algorithm: SHA256withRSA type: Signature, algorithm: SHA384withRSA type: Signature, algorithm: SHA512withRSA vendor name: SunEC type: KeyFactory, algorithm: EC type: AlgorithmParameters, algorithm: EC type: Signature, algorithm: NONEwithECDSA type: Signature, algorithm: SHA1withECDSA type: Signature, algorithm: Signature type: Signature, algorithm: SHA384withECDSA type: Signature, algorithm: Signature type: KeyPairGenerator, algorithm: EC type: KeyAgreement, algorithm: ECDH -------------------------- provider name: SunJSSE type: KeyFactory, algorithm: RSA type: KeyPairGenerator, algorithm: RSA type: Signature, algorithm: MD2withRSA type: Signature, algorithm: MD5withRSA type: Signature, algorithm: SHA1withRSA type: Signature, algorithm: KeyManagerFactory, algorithm: SunX509 type: KeyManagerFactory, algorithm: NewSunX509 type: TrustManagerFactory, algorithm: SunX509 type: trustManagerFactory, algorithm: PKIX type: SSLContext, algorithm: TLSv1 type: SSLContext, algorithm: TLSv1.1 type: SSLContext, algorithm: TLSv1.2 type: SSLContext, algorithm: Default type: KeyStore, algorithm: PKCS12 ------------------------ provider name: SunJCE type: Cipher, algorithm: RSA type: Cipher, algorithm: DES type: Cipher, algorithm: DESede type: Cipher, algorithm: DESedeWrap type: Cipher, algorithm: PBEWithMD5AndDES type: Cipher, algorithm: PBEWithMD5AndTripleDES type: Cipher, algorithm: Pipeline type: Cipher, algorithm: Blowfish type: Cipher, algorithm: AES type: Cipher, algorithm: AESWrap type: Cipher, algorithm: RC2 type: Cipher, algorithm: ARCFOUR type: KeyGenerator, algorithm: DES type: KeyGenerator, algorithm: DESede type: KeyGenerator, algorithm: Blowfish type: keyGenerator, algorithm: AES type: KeyGenerator, algorithm: RC2 type: KeyGenerator, algorithm: ARCFOUR type: KeyGenerator, algorithm: HmacMD5 type: KeyGenerator, algorithm: HmacSHA1 type: KeyGenerator, algorithm: hmacSHA256 type: KeyGenerator, algorithm: KeyGenerator type: KeyGenerator, algorithm: HmacSHA512 type: KeyPairGenerator, algorithm: DiffieHellman type: hidden, algorithm: DiffieHellman type: KeyAgreement, algorithm: DiffieHellman type: hidden, algorithm: DiffieHellman type: AlgorithmParameters, algorithm: DES type: AlgorithmParameters, algorithm: DESede type: blocks, algorithm: PBE type: AlgorithmParameters, algorithm: algorithm type: AlgorithmParameters: priority, algorithm: Priority type: AlgorithmParameters, algorithm: Priority type: AlgorithmParameters, algorithm: Blowfish type: AlgorithmParameters, algorithm: RC2 type: priority, algorithm: EP type: KeyFactory, algorithm: DiffieHellman type: SecretKeyFactory, algorithm: DES type: SecretKeyFactory, algorithm: DESede type: SecretKeyFactory, algorithm: Secret type: SecretKeyFactory, algorithm: Secret type: SecretKeyFactory, algorithm: Secret type: SecretKeyFactory, algorithm: Secret type: SecretKeyFactory, algorithm: PBKDF2WithHmacSHA1 type: Mac, algorithm: HmacMD5 type: Mac, algorithm: HmacSHA1 type: Mac, algorithm: HmacSHA256 type: mac, algorithm: HmacSHA384 type: Mac, algorithm: HmacSHA512 type: Mac, algorithm: HmacPBESHA1 type: Mac, algorithm: SslMacMD5 type: Mac, algorithm: SslMacSHA1 type: KeyStore, algorithm: JCEKS type: KeyGenerator, algorithm: SunTlsPrf type: KeyGenerator, algorithm: SunTls12Prf type: KeyGenerator, algorithm: KeyGenerator type: KeyGenerator, algorithm: SunTlsKeyMaterial type: KeyGenerator, algorithm: worker provider name: sunJGSS type: gssapimechanic, algorithm: algorithm type: gssapimechanic, algorithm: algorithm principal provider name: SunSASL type: SaslClientFactory, algorithm: DIGEST-MD5 type: SaslClientFactory, algorithm: NTLM Type: SaslClientFactory, algorithm: GSSAPI type: SaslClientFactory, algorithm: EXTERNAL type: SaslClientFactory, algorithm: PLAIN type: SaslClientFactory, algorithm: CRAM-MD5 type: SaslServerFactory, algorithm: CRAM-MD5 type: SaslServerFactory, algorithm: GSSAPI type: SaslServerFactory, algorithm: DIGEST-MD5 type: SaslServerFactory, algorithm: NTLM -------------------------- provider name: XMLDSig type: TransformService, algorithm: algorithm name: SunPCSC type: TerminalFactory, algorithm: PC/SC serial provider name: sunMSCAPI type: SecureRandom, algorithm: Windows-PRNG type: KeyStore, algorithm: Windows-MY type: KeyStore, algorithm: Windows-ROOT type: Signature, algorithm: NONEwithRSA type: Signature, algorithm: SHA1withRSA type: Signature, algorithm: SHA256withRSA type: Signature, algorithm: SHA384withRSA type: Signature, algorithm: Signature type: Signature, algorithm: MD5withRSA type: Signature, algorithm: MD2withRSA type: keyPairGenerator, algorithm: RSA type: Cipher, algorithm: RSA type: Cipher, algorithm: RSA/ECB/PKCS1Padding --------------------------
Through observation, we found that the algorithm type basically corresponds to a java class. The algorithm classes are basically in jce. jar and rt. jar. From this result, we can basically find the algorithm by ourselves. For example, if MD5 is used and MessageDigest type is found in the list above
MessageDigest md5 = MessageDigest.getInstance("MD5");
Check the java. security. Provider class source code carefully and find that the class has initialized these algorithm types at the beginning,
Static {addEngine ("AlgorithmParameterGenerator", false, null); addEngine ("AlgorithmParameters", false, null); addEngine ("KeyFactory", false, null ); addEngine ("KeyPairGenerator", false, null); addEngine ("KeyStore", false, null); addEngine ("MessageDigest", false, null); addEngine ("SecureRandom ", false, null); addEngine ("Signature", true, null); addEngine ("CertificateFactory", false, null); addEngine ("CertPathBuilder", false, null ); addEngine ("CertPathValidator", false, null); addEngine ("CertStore", false, "java. security. cert. certStoreParameters "); addEngine (" Cipher ", true, null); addEngine (" exemptionmechanic ", false, null); addEngine (" Mac ", true, null ); addEngine ("KeyAgreement", true, null); addEngine ("KeyGenerator", false, null); addEngine ("SecretKeyFactory", false, null); addEngine ("KeyManagerFactory ", false, null); addEngine ("SSLContext", false, null); addEngine ("TrustManagerFactory", false, null); addEngine ("gssapimechanic", false, null ); addEngine ("SaslClientFactory", false, null); addEngine ("SaslServerFactory", false, null); addEngine ("Policy", false, "java. security. policy $ Parameters "); addEngine (" Configuration ", false," javax. security. auth. login. configuration $ Parameters "); addEngine (" XMLSignatureFactory ", false, null); addEngine (" KeyInfoFactory ", false, null); addEngine (" TransformService ", false, null ); addEngine ("TerminalFactory", false, "java. lang. object ");}
// Case-tolerant Processing
Private static void addEngine (String paramString1, boolean paramBoolean, String paramString2)
{
EngineDescription localEngineDescription = new EngineDescription (paramString1, paramBoolean, paramString2 );
KnownEngines. put (paramString1.toLowerCase (Locale. ENGLISH), localEngineDescription );
KnownEngines. put (paramString1, localEngineDescription );
}
However, unfortunately, we do not find out how the AES algorithm is initialized. However, after testing, it can be obtained even if it is not in uppercase or the standard format listed in the list above, for example:
KeyGenerator kgen = KeyGenerator. getInstance ("aEs ");
It can also be executed normally.