In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-21 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
What are the 19 methods of Java KeyGenerator.generateKey? I believe many inexperienced people don't know what to do about it. Therefore, this paper summarizes the causes and solutions of the problem. Through this article, I hope you can solve this problem.
The editor collates and summarizes code examples of typical usage of the javax.crypto.KeyGenerator.generateKey method in Java. If you are suffering from the following question: what is the specific use of the Java KeyGenerator.generateKey method? How do I use Java KeyGenerator.generateKey? What are the examples used by Java KeyGenerator.generateKey? So congratulations, the selected method code examples here may help you. You can also learn more about the usage example of the class javax.crypto.KeyGenerator that this method belongs to.
A total of 19 code examples of the KeyGenerator.generateKey method are shown below.
Example 1: EncryptByAesimport javax.crypto.KeyGenerator; / / Import method depends on package package / class public EncryptByAes () {try {/ / generate key KeyGenerator keyGenerator = KeyGenerator.getInstance ("AES"); keyGenerator.init (256); / / generate key SecretKey secretKey = keyGenerator.generateKey () / / get the key byte [] keyBytes = secretKey.getEncoded (); / / Logger.e ("AES Key=" + Base64.encodeToString (keyBytes, Base64.URL_SAFE)); / / restore the key key = new SecretKeySpec (keyBytes, "AES");} catch (NoSuchAlgorithmException e) {e.printStackTrace ();}}
Developer ID:zhonglikui, project name: cardinalsSample, number of lines of code: 20, code source: EncryptByAes.java
Example 2: mainimport javax.crypto.KeyGenerator; / / Import package package / class public static void main (String [] args) throws Exception {Provider provider = Security.getProvider ("SunJCE"); KeyGenerator kg; kg = KeyGenerator.getInstance ("SunTlsRsaPremasterSecret", provider); try {kg.generateKey (); throw new Exception ("no exception");} catch (IllegalStateException e) {System.out.println ("OK:" + e) } int [] protocolVersions = {0x0300, 0x0301, 0x0302, 0x0400}; for (int clientVersion: protocolVersions) {for (int serverVersion: protocolVersions) {test (kg, clientVersion, serverVersion); if (serverVersion > = clientVersion) {break;}} System.out.println ("Done.");}
Developer ID:lambdalab-mirror, project name: jdk8u-jdk, number of lines of code: 27, code source: TestPremaster.java
Example 3: wrapperPublicPriviteKeyTestimport javax.crypto.KeyGenerator; / / imports package packages / classes on which the method depends (Provider p, String [] algorithms) throws NoSuchAlgorithmException, InvalidKeyException, NoSuchPaddingException, IllegalBlockSizeException, InvalidAlgorithmParameterException {for (String algo: algorithms) {/ / Key pair generated System.out.println ("Generate key pair (algorithm:" + algo + ", provider:" + p.getName () + ")) KeyPairGenerator kpg = KeyPairGenerator.getInstance (algo); kpg.initialize (512); KeyPair kp = kpg.genKeyPair (); / / key generated String algoWrap = "DES"; KeyGenerator kg = KeyGenerator.getInstance (algoWrap, p); Key key = kg.generateKey (); wrapTest (algo, algoWrap, key, kp.getPrivate (), Cipher.PRIVATE_KEY, false) WrapTest (algo, algoWrap, key, kp.getPublic (), Cipher.PUBLIC_KEY, false);}}
Developer ID:lambdalab-mirror, project name: jdk8u-jdk, number of lines of code: 22, code source: TestCipherKeyWrapperTest.java
Example 4: initHmacSHA224Keyimport javax.crypto.KeyGenerator; / / Import package package / class / * * initialize HmacSHA224 key * * @ return byte [] key * @ throws Exception * / public static byte [] initHmacSHA224Key () throws Exception {/ / initialize KeyGenerator KeyGenerator keyGenerator = KeyGenerator.getInstance ("HmacSHA224"); / / generate secret key SecretKey secretKey = keyGenerator.generateKey (); / / obtain key return secretKey.getEncoded ();}
Developer ID:guokezheng, project name: automat, number of lines of code: 15, code source: HmacCoder.java
Example 5: runimport javax.crypto.KeyGenerator; / / Import package package / class private void run (String keystoreType) throws Exception {char [] pw = "password" .toCharArray (); KeyStore ks = KeyStore.getInstance (keystoreType); ks.load (null, pw); KeyGenerator kg = KeyGenerator.getInstance ("AES"); kg.init (128); SecretKey key = kg.generateKey (); KeyStore.SecretKeyEntry ske = new KeyStore.SecretKeyEntry (key) KeyStore.ProtectionParameter kspp = new KeyStore.PasswordProtection (pw); ks.setEntry (ALIAS, ske, kspp); File ksFile = File.createTempFile ("test", ".test"); try (FileOutputStream fos = new FileOutputStream (ksFile)) {ks.store (fos, pw); fos.flush ();} / / now see if we can get it back try (FileInputStream fis = new FileInputStream (ksFile)) {KeyStore ks2 = KeyStore.getInstance (keystoreType) Ks2.load (fis, pw); KeyStore.Entry entry = ks2.getEntry (ALIAS, kspp); SecretKey keyIn = ((KeyStore.SecretKeyEntry) entry). GetSecretKey (); if (Arrays.equals (key.getEncoded (), keyIn.getEncoded ()) {System.err.println ("OK: worked just fine with" + keystoreType + "keystore") } else {System.err.println ("ERROR: keys are NOT equal after storing in" + keystoreType + "keystore");}
Developer ID:lambdalab-mirror, project name: jdk8u-jdk, number of lines of code: 35, code source: P12SecretKey.java
Example 6: initHmacSHA384Keyimport javax.crypto.KeyGenerator; / / Import the package package / class on which the method depends / * initialize the HmacSHA384 key * * @ return * @ throws Exception * / public static byte [] initHmacSHA384Key () throws Exception {/ / initialize KeyGenerator KeyGenerator keyGenerator = KeyGenerator.getInstance ("HmacSHA384"); / / generate the secret key SecretKey secretKey = keyGenerator.generateKey (); / / obtain the key return secretKey.getEncoded ();}
Developer ID:babymm, project name: mumu, number of lines of code: 15, code source: HmacCoder.java
Example 7: package package / class private void wrapperBlowfishKeyTest () throws InvalidKeyException, NoSuchAlgorithmException, NoSuchPaddingException, IllegalBlockSizeException, InvalidAlgorithmParameterException {/ / how many kinds of padding mode int padKinds; / / Keysize should be multiple of 8 bytes that the wrapperBlowfishKeyTestimport javax.crypto.KeyGenerator; / / import method depends on. Int KeyCutter = 8; int kSize = BLOWFISH_MIN_KEYSIZE; String algorithm = "Blowfish"; int maxAllowKeyLength = Cipher.getMaxAllowedKeyLength (algorithm); boolean unLimitPolicy = maxAllowKeyLength = = Integer.MAX_VALUE; SecretKey key = null; while (kSize LINIMITED_KEYSIZE) {out.println ("keyStrength > 128 within" + algorithm + "will not run under global policy") } else {wrapTest (transformation, transformation, key, key, Cipher.SECRET_KEY, false);} if (kSize
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.