亲宝软件园·资讯

展开

Java KeyGenerator

人气:0

本文整理汇总了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;未经允许,请勿转载。

加载全部内容

相关教程
猜你喜欢
用户评论