欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Java KeyGenerator.generateKey的19个方法代码示例

程序员文章站 2022-03-02 16:01:25
目录示例1: encryptbyaes示例2: main示例3: wrapperpublicprivitekeytest示例4: inithmacsha224key示例5: run示例6: inith...

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

相关标签: Java KeyGenerator