Java KeyGenerator.generateKey的19個方法代碼示例
本文整理匯總瞭Java中javax.crypto.KeyGenerator.generateKey方法的典型用法代碼示例。如果您正苦於以下問題:Java KeyGenerator.generateKey方法的具體用法?Java KeyGenerator.generateKey怎麼用?Java KeyGenerator.generateKey使用的例子?那麼恭喜您, 這裡精選的方法代碼示例或許可以為您提供幫助。您也可以進一步瞭解該方法所在類javax.crypto.KeyGenerator的用法示例。
在下文中一共展示瞭KeyGenerator.generateKey方法的19個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點贊,您的評價將有助於我們的系統推薦出更棒的Java代碼示例。
示例1: EncryptByAes
import javax.crypto.KeyGenerator; //導入方法依賴的package包/類 public EncryptByAes() { try { //生成key KeyGenerator keyGenerator = KeyGenerator.getInstance("AES"); keyGenerator.init(256); //產生秘鑰 SecretKey secretKey = keyGenerator.generateKey(); //獲取秘鑰 byte[] keyBytes = secretKey.getEncoded(); // Logger.e("AES Key=" + Base64.encodeToString(keyBytes, Base64.URL_SAFE)); //還原秘鑰 key = new SecretKeySpec(keyBytes, "AES"); } catch (NoSuchAlgorithmException e) { e.printStackTrace(); } }
開發者ID:zhonglikui,項目名稱:cardinalsSample,代碼行數:20,代碼來源:EncryptByAes.java
示例2: main
import javax.crypto.KeyGenerator; //導入方法依賴的package包/類 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."); }
開發者ID:lambdalab-mirror,項目名稱:jdk8u-jdk,代碼行數:27,代碼來源:TestPremaster.java
示例3: wrapperPublicPriviteKeyTest
import javax.crypto.KeyGenerator; //導入方法依賴的package包/類 private void wrapperPublicPriviteKeyTest(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); } }
開發者ID:lambdalab-mirror,項目名稱:jdk8u-jdk,代碼行數:22,代碼來源:TestCipherKeyWrapperTest.java
示例4: initHmacSHA224Key
import javax.crypto.KeyGenerator; //導入方法依賴的package包/類 /** * 初始化HmacSHA224密鑰 * * @return byte[] 密鑰 * @throws Exception */ public static byte[] initHmacSHA224Key() throws Exception { // 初始化KeyGenerator KeyGenerator keyGenerator = KeyGenerator.getInstance("HmacSHA224"); // 產生秘密密鑰 SecretKey secretKey = keyGenerator.generateKey(); // 獲得密鑰 return secretKey.getEncoded(); }
開發者ID:guokezheng,項目名稱:automat,代碼行數:15,代碼來源:HmacCoder.java
示例5: run
import javax.crypto.KeyGenerator; //導入方法依賴的package包/類 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"); } } }
開發者ID:lambdalab-mirror,項目名稱:jdk8u-jdk,代碼行數:35,代碼來源:P12SecretKey.java
示例6: initHmacSHA384Key
import javax.crypto.KeyGenerator; //導入方法依賴的package包/類 /** * 初始化HmacSHA384密鑰 * * @return * @throws Exception */ public static byte[] initHmacSHA384Key() throws Exception { // 初始化KeyGenerator KeyGenerator keyGenerator = KeyGenerator.getInstance("HmacSHA384"); // 產生秘密密鑰 SecretKey secretKey = keyGenerator.generateKey(); // 獲得密鑰 return secretKey.getEncoded(); }
開發者ID:babymm,項目名稱:mumu,代碼行數:15,代碼來源:HmacCoder.java
示例7: wrapperBlowfishKeyTest
import javax.crypto.KeyGenerator; //導入方法依賴的package包/類 private void wrapperBlowfishKeyTest() throws InvalidKeyException, NoSuchAlgorithmException, NoSuchPaddingException, IllegalBlockSizeException, InvalidAlgorithmParameterException { // how many kinds of padding mode int padKinds; // Keysize should be multiple of 8 bytes. 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 <= BLOWFISH_MAX_KEYSIZE) { for (String mode : MODEL_AR) { // PKCS5padding is meaningful only for ECB, CBC, PCBC if (mode.equalsIgnoreCase(MODEL_AR[0]) || mode.equalsIgnoreCase(MODEL_AR[1]) || mode.equalsIgnoreCase(MODEL_AR[2])) { padKinds = PADDING_AR.length; } else { padKinds = 1; } // Initialization KeyGenerator kg = KeyGenerator.getInstance(algorithm); for (int k = 0; k < padKinds; k++) { String transformation = algorithm + "/" + mode + "/" + PADDING_AR[k]; if (NOPADDING.equals(PADDING_AR[k]) && kSize % 64 != 0) { out.println(transformation + " will not run if input length not multiple" + " of 8 bytes when padding is " + NOPADDING); continue; } kg.init(kSize); key = kg.generateKey(); // only run the tests on longer key lengths if unlimited // version of JCE jurisdiction policy files are installed if (!unLimitPolicy && 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 <= LINIMITED_KEYSIZE) { KeyCutter = 8; } else { KeyCutter = 48; } kSize += KeyCutter; } }
開發者ID:lambdalab-mirror,項目名稱:jdk8u-jdk,代碼行數:55,代碼來源:TestCipherKeyWrapperTest.java
示例8: generateMacSha1Key
import javax.crypto.KeyGenerator; //導入方法依賴的package包/類 /** * 生成HMAC-SHA1密鑰,返回字節數組,長度為160位(20字節). * HMAC-SHA1算法對密鑰無特殊要求, RFC2401建議最少長度為160位(20字節). */ public static byte[] generateMacSha1Key() { try { KeyGenerator keyGenerator = KeyGenerator.getInstance(HMACSHA1); keyGenerator.init(DEFAULT_HMACSHA1_KEYSIZE); SecretKey secretKey = keyGenerator.generateKey(); return secretKey.getEncoded(); } catch (GeneralSecurityException e) { throw ExceptionUtils.unchecked(e); } }
開發者ID:wkeyuan,項目名稱:DWSurvey,代碼行數:15,代碼來源:CryptoUtils.java
示例9: ReadWriteSkip
import javax.crypto.KeyGenerator; //導入方法依賴的package包/類 ReadWriteSkip(int keyLength, int textLength, int AADLength) throws Exception { this.keyLength = keyLength; this.textLength = textLength; // init AAD this.AAD = Helper.generateBytes(AADLength); // init a secret Key KeyGenerator kg = KeyGenerator.getInstance(AES, PROVIDER); kg.init(this.keyLength); this.key = kg.generateKey(); this.plaintext = Helper.generateBytes(textLength); }
開發者ID:lambdalab-mirror,項目名稱:jdk8u-jdk,代碼行數:16,代碼來源:ReadWriteSkip.java
示例10: generateKey
import javax.crypto.KeyGenerator; //導入方法依賴的package包/類 public static byte[] generateKey( byte[] seed ) throws Exception { KeyGenerator keyGenerator = KeyGenerator.getInstance( CIPHER_ALGORITHM ); SecureRandom secureRandom = SecureRandom.getInstance( RANDOM_GENERATOR_ALGORITHM ); secureRandom.setSeed( seed ); keyGenerator.init( RANDOM_KEY_SIZE, secureRandom ); SecretKey secretKey = keyGenerator.generateKey(); return secretKey.getEncoded(); }
開發者ID:sdrausty,項目名稱:buildAPKsSamples,代碼行數:10,代碼來源:StringCryptor.java
示例11: generateEncryptionSecret
import javax.crypto.KeyGenerator; //導入方法依賴的package包/類 private static byte[] generateEncryptionSecret() { try { KeyGenerator generator = KeyGenerator.getInstance("AES"); generator.init(128); SecretKey key = generator.generateKey(); return key.getEncoded(); } catch (NoSuchAlgorithmException ex) { Log.w("keyutil", ex); return null; } }
開發者ID:CableIM,項目名稱:Cable-Android,代碼行數:13,代碼來源:MasterSecretUtil.java
示例12: initHmacSHA512Key
import javax.crypto.KeyGenerator; //導入方法依賴的package包/類 /** * 初始化HmacSHA512密鑰 * * @return * @throws Exception */ public static byte[] initHmacSHA512Key() throws Exception { // 初始化KeyGenerator KeyGenerator keyGenerator = KeyGenerator.getInstance("HmacSHA512"); // 產生秘密密鑰 SecretKey secretKey = keyGenerator.generateKey(); // 獲得密鑰 return secretKey.getEncoded(); }
開發者ID:mumucommon,項目名稱:mumu-core,代碼行數:15,代碼來源:HmacCoder.java
示例13: initKey
import javax.crypto.KeyGenerator; //導入方法依賴的package包/類 /** * 生成密鑰 * * @param keysize * @return */ public byte[] initKey(int keysize) { KeyGenerator keyGen = null; try { keyGen = KeyGenerator.getInstance("AES"); keyGen.init(keysize); //192 256 } catch (NoSuchAlgorithmException e) { e.printStackTrace(); } SecretKey secretKey = keyGen.generateKey(); return secretKey.getEncoded(); }
開發者ID:jopenbox,項目名稱:android-lite-utils,代碼行數:18,代碼來源:AESUtils.java
示例14: initHmacSHAKey
import javax.crypto.KeyGenerator; //導入方法依賴的package包/類 /** * 初始化HmacSHA1密鑰 * * @return * @throws Exception */ public static byte[] initHmacSHAKey() throws Exception { // 初始化KeyGenerator KeyGenerator keyGenerator = KeyGenerator.getInstance("HMacTiger"); // 產生秘密密鑰 SecretKey secretKey = keyGenerator.generateKey(); // 獲得密鑰 return secretKey.getEncoded(); }
開發者ID:guokezheng,項目名稱:automat,代碼行數:15,代碼來源:HmacCoder.java
示例15: initHmacMD4Key
import javax.crypto.KeyGenerator; //導入方法依賴的package包/類 /** * 初始化HmacMD4密鑰 * * @return byte[] 密鑰 * @throws Exception */ public static byte[] initHmacMD4Key() throws Exception { // 初始化KeyGenerator KeyGenerator keyGenerator = KeyGenerator.getInstance("HmacMD4"); // 產生秘密密鑰 SecretKey secretKey = keyGenerator.generateKey(); // 獲得密鑰 return secretKey.getEncoded(); }
開發者ID:iBase4J,項目名稱:iBase4J-Common,代碼行數:15,代碼來源:HmacCoder.java
示例16: initHmacMD5Key
import javax.crypto.KeyGenerator; //導入方法依賴的package包/類 /** * 初始化HmacMD5密鑰 * * @return * @throws Exception */ public static byte[] initHmacMD5Key() throws Exception { // 初始化KeyGenerator KeyGenerator keyGenerator = KeyGenerator.getInstance("HmacMD5"); // 產生秘密密鑰 SecretKey secretKey = keyGenerator.generateKey(); // 獲得密鑰 return secretKey.getEncoded(); }
開發者ID:iBase4J,項目名稱:iBase4J-Common,代碼行數:15,代碼來源:HmacCoder.java
示例17: runTest
import javax.crypto.KeyGenerator; //導入方法依賴的package包/類 private static void runTest(Provider p, String algo, String mo, String pad, ReadModel whichRead) throws GeneralSecurityException, IOException { // Do initialization byte[] plainText = TestUtilities.generateBytes(TEXT_LENGTH); byte[] iv = TestUtilities.generateBytes(IV_LENGTH); AlgorithmParameterSpec aps = new IvParameterSpec(iv); try { KeyGenerator kg = KeyGenerator.getInstance(algo, p); out.println(algo + "/" + mo + "/" + pad + "/" + whichRead); SecretKey key = kg.generateKey(); Cipher ci1 = Cipher.getInstance(algo + "/" + mo + "/" + pad, p); if ("CFB72".equalsIgnoreCase(mo) || "OFB20".equalsIgnoreCase(mo)) { throw new RuntimeException( "NoSuchAlgorithmException not throw when mode" + " is CFB72 or OFB20"); } Cipher ci2 = Cipher.getInstance(algo + "/" + mo + "/" + pad, p); if ("ECB".equalsIgnoreCase(mo)) { ci1.init(Cipher.ENCRYPT_MODE, key); ci2.init(Cipher.DECRYPT_MODE, key); } else { ci1.init(Cipher.ENCRYPT_MODE, key, aps); ci2.init(Cipher.DECRYPT_MODE, key, aps); } ByteArrayOutputStream baOutput = new ByteArrayOutputStream(); try (CipherInputStream cInput = new CipherInputStream( new ByteArrayInputStream(plainText), ci1); CipherOutputStream ciOutput = new CipherOutputStream(baOutput, ci2);) { // Read from the input and write to the output using 2 types // of buffering : byte[] and int whichRead.read(cInput, ciOutput, ci1, plainText.length); } // Verify input and output are same. if (!Arrays.equals(plainText, baOutput.toByteArray())) { throw new RuntimeException("Test failed due to compare fail "); } } catch (NoSuchAlgorithmException nsaEx) { if ("CFB72".equalsIgnoreCase(mo) || "OFB20".equalsIgnoreCase(mo)) { out.println("NoSuchAlgorithmException is expected for CFB72 and OFB20"); } else { throw new RuntimeException("Unexpected exception testing: " + algo + "/" + mo + "/" + pad + "/" + whichRead, nsaEx); } } }
開發者ID:lambdalab-mirror,項目名稱:jdk8u-jdk,代碼行數:48,代碼來源:CICODESFuncTest.java
示例18: runTest
import javax.crypto.KeyGenerator; //導入方法依賴的package包/類 private static void runTest(DataTuple dataTuple) throws NoSuchAlgorithmException, NoSuchProviderException, InvalidKeyException { Mac mcAlgorithm = Mac.getInstance(dataTuple.algorithm, PROVIDER_NAME); Mac mcOid = Mac.getInstance(dataTuple.oid, PROVIDER_NAME); if (mcAlgorithm == null) { throw new RuntimeException(String.format( "Test failed: Mac using algorithm " + "string %s getInstance failed.%n", dataTuple.algorithm)); } if (mcOid == null) { throw new RuntimeException(String.format( "Test failed: Mac using OID %s getInstance failed.%n", dataTuple.oid)); } if (!mcAlgorithm.getAlgorithm().equals(dataTuple.algorithm)) { throw new RuntimeException(String.format( "Test failed: Mac using algorithm string %s getInstance " + "doesn't generate expected algorithm.%n", dataTuple.algorithm)); } KeyGenerator kg = KeyGenerator.getInstance(dataTuple.algorithm, PROVIDER_NAME); SecretKey key = kg.generateKey(); mcAlgorithm.init(key); mcAlgorithm.update(INPUT); mcOid.init(key); mcOid.update(INPUT); // Comparison if (!Arrays.equals(mcAlgorithm.doFinal(), mcOid.doFinal())) { throw new RuntimeException("Digest comparison failed: " + "the two MACs are not the same"); } }
開發者ID:lambdalab-mirror,項目名稱:jdk8u-jdk,代碼行數:44,代碼來源:TestHmacSHAOids.java
示例19: s_genkeys
import javax.crypto.KeyGenerator; //導入方法依賴的package包/類 /** * 通過種子生成二進制密鑰 * * @param _arg * 生成私鑰的種子 * @return * @throws Exception */ public static byte[] s_genkeys( String _arg ) throws Exception { KeyGenerator key_generator = KeyGenerator.getInstance( ALGORITHM ); SecureRandom secure_random = new SecureRandom( Base64.decode( _arg.getBytes() ) ); key_generator.init( secure_random ); SecretKey secret_key = key_generator.generateKey(); return secret_key.getEncoded(); }
開發者ID:aiyoyoyo,項目名稱:jeesupport,代碼行數:16,代碼來源:DESUtils.java
註:本文中的javax.crypto.KeyGenerator.generateKey方法示例整理自Github/MSDocs等源碼及文檔管理平臺,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。