运维开发网

9方法代码演示示例

运维开发网 https://www.qedev.com 2022-05-11 16:15 出处:网络
在下文中一共展示了KeyGenerator.generateKey方法的19个代码示例,这些例子默认根据受欢迎程度排序

在下文中一共展示了KeyGenerator.generateKey方法的19个代码示例,这些例子默认根据受欢迎程度排序

本文总结了Java中javax . crypto . key generator . generate key方法的典型使用代码示例。如果你正苦于以下问题:Java KeyGenerator.generateKey方法的具体用法?如何使用Java KeyGenerator.generateKey?Java KeyGenerator.generateKey的使用示例?那恭喜你了。这里选择的方法代码示例可能会对您有所帮助。您还可以进一步了解这个方法所在的javax.crypto.KeyGenerator类的用法示例。

在下文中,总共显示了KeyGenerator.generateKey方法的19个代码示例。默认情况下,这些示例按照流行度排序。您可以表扬您喜欢的或觉得有用的代码,您的评价将有助于我们的系统推荐更好的Java代码示例。


示例1: EncryptByAesimport 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:钟立奎,项目名称:cardinalsSample,代码行数:20,代码来源:EncryptByAes.java


示例2: mainimport 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 gt;= clientVersion) { break; } } } System.out.println("Done.");}

开发者ID:lambdalab-mirror,项目名称:jdk8u-jdk,代码行数:27,代码来源:TestPremaster.java


示例3: wrapperPublicPriviteKeyTestimport 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: initHmacSHA224Keyimport 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:国科正,项目名称:automat,代码行数:15,代码来源:HmacCoder.java


示例5: runimport 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: initHmacSHA384Keyimport 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,项目名称:HmacCoder.java牧牧,代码行数:15,代码来源


示例7: wrapperBlowfishKeyTestimport 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 lt;= 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 lt; padKinds; k++) { String transformation = algorithm + "/" + mode + "/" + PADDING_AR[k]; if (NOPADDING.equals(PADDING_AR[k]) amp;amp; 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 amp;amp; kSize gt; LINIMITED_KEYSIZE) { out.println("keyStrength gt; 128 within " + algorithm + " will not run under global policy"); } else { wrapTest(transformation, transformation, key, key, Cipher.SECRET_KEY, false); } } } if (kSize lt;= LINIMITED_KEYSIZE) { KeyCutter = 8; } else { KeyCutter = 48; } kSize += KeyCutter; }}

开发者ID:lambdalab-mirror,项目名称:jdk8u-jdk,代码行数:55,代码来源:TestCipherKeyWrapperTest.java


示例8: generateMacSha1Keyimport 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); }}

开发者:CryptoUtils.java WKEY UAN,项目名称:DWSurvey,代码行数:15,代码来源


示例9: ReadWriteSkipimport 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: generateKeyimport 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();}

开发者: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: initHmacSHA512Keyimport 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,项目名称:牧牧核心,代码行数:15,代码来源:HmacCoder.java


示例13: initKeyimport 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: initHmacSHAKeyimport 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:国科正,项目名称:automat,代码行数:15,代码来源:HmacCoder.java


示例15: initHmacMD4Keyimport 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: initHmacMD5Keyimport 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: runTestimport 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: runTestimport 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_genkeysimport 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 . key generator . generate key方法的例子是从Github/MSDocs等源代码和文档管理平台编译而来,相关代码片段选自各路编程大神贡献的开源项目。源代码版权归原作者所有。请参考许可证;相应项目的推广和使用;未经许可,请勿转载。


0

精彩评论

暂无评论...
验证码 换一张
取 消