当前位置: 首页>>代码示例>>Java>>正文


Java Cipher.getMaxAllowedKeyLength方法代码示例

本文整理汇总了Java中javax.crypto.Cipher.getMaxAllowedKeyLength方法的典型用法代码示例。如果您正苦于以下问题:Java Cipher.getMaxAllowedKeyLength方法的具体用法?Java Cipher.getMaxAllowedKeyLength怎么用?Java Cipher.getMaxAllowedKeyLength使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在javax.crypto.Cipher的用法示例。


在下文中一共展示了Cipher.getMaxAllowedKeyLength方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: getDefaultAlg

import javax.crypto.Cipher; //导入方法依赖的package包/类
/**
 * Returns the algorithm supported for input mechanism.
 * @param mech Mechanism name
 * @param alg Algorithm name
 * @return Algorithm name
 */
private static String getDefaultAlg(String mech, String alg)
        throws NoSuchAlgorithmException {
    if (alg == null) {
        switch (mech) {
            case "Hash_DRBG":
            case "HMAC_DRBG":
                return "SHA-256";
            case "CTR_DRBG":
                return (Cipher.getMaxAllowedKeyLength("AES") < 256)
                        ? "AES-128" : "AES-256";
            default:
                throw new RuntimeException("Mechanism not supported");
        }
    }
    return alg;
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:23,代码来源:ApiTest.java

示例2: wrapperPBEKeyTest

import javax.crypto.Cipher; //导入方法依赖的package包/类
private void wrapperPBEKeyTest(Provider p) throws InvalidKeySpecException,
        InvalidKeyException, NoSuchPaddingException,
        IllegalBlockSizeException, InvalidAlgorithmParameterException,
        NoSuchAlgorithmException {
    for (String alg : PBE_ALGORITHM_AR) {
        String baseAlgo = alg.split("/")[0].toUpperCase();
        // only run the tests on longer key lengths if unlimited version
        // of JCE jurisdiction policy files are installed

        if (Cipher.getMaxAllowedKeyLength(alg) < Integer.MAX_VALUE
                && (baseAlgo.endsWith("TRIPLEDES") || alg
                        .endsWith("AES_256"))) {
            out.println("keyStrength > 128 within " + alg
                    + " will not run under global policy");
            continue;
        }
        SecretKeyFactory skf = SecretKeyFactory.getInstance(baseAlgo, p);
        SecretKey key = skf.generateSecret(new PBEKeySpec("Secret Lover"
                .toCharArray()));
        wrapTest(alg, alg, key, key, Cipher.SECRET_KEY, true);
    }
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:23,代码来源:TestCipherKeyWrapperTest.java

示例3: main

import javax.crypto.Cipher; //导入方法依赖的package包/类
public static void main(String[] args) throws Exception {
    Provider p = Security.getProvider("SunJCE");
    for (String alg : ALGORITHMS) {
        for (int keyStrength : KEY_STRENGTHS) {
            if (keyStrength > Cipher.getMaxAllowedKeyLength(alg)) {
                // skip this if this key length is larger than what's
                // configured in the JCE jurisdiction policy files
                continue;
            }
            for (int textLength : TEXT_LENGTHS) {
                for (int AADLength : AAD_LENGTHS) {
                    Encrypt test = new Encrypt(p, alg,
                            "GCM", "NoPadding", keyStrength, textLength,
                            AADLength);
                    Cipher cipher = test.createCipher(Cipher.ENCRYPT_MODE,
                            null);
                    AlgorithmParameters params = cipher.getParameters();
                    test.doTest(params);
                    System.out.println("Test " + alg + ":"
                            + keyStrength + ":" + textLength + ":"
                            + AADLength + " passed");
                }
            }
        }
    }
}
 
开发者ID:lambdalab-mirror,项目名称:jdk8u-jdk,代码行数:27,代码来源:Encrypt.java

示例4: CookieEncryption

import javax.crypto.Cipher; //导入方法依赖的package包/类
private CookieEncryption(String secret) {
    Optional<SecretKeySpec> secretKeySpec;
    try {
        int maxKeyLengthBits = Cipher.getMaxAllowedKeyLength(ALGORITHM);
        if (maxKeyLengthBits == Integer.MAX_VALUE) {
            maxKeyLengthBits = 256;
        }

        secretKeySpec = Optional.of(
                new SecretKeySpec(secret.getBytes(), 0, maxKeyLengthBits / Byte.SIZE, ALGORITHM));

        logger.info("onepiecex session encryption is using {} / {} bit.", secretKeySpec.get().getAlgorithm(), maxKeyLengthBits);

    } catch (Exception exception) {
        logger.error("Can not create class to encrypt cookie.", exception);
        throw new RuntimeException(exception);
    }
    this.secretKeySpec = secretKeySpec;
}
 
开发者ID:onepiecex,项目名称:session-share,代码行数:20,代码来源:CookieEncryption.java

示例5: main

import javax.crypto.Cipher; //导入方法依赖的package包/类
public static void main(String[] args) throws Exception {
    for (PBEAlgorithm algorithm : PBEAlgorithm.values()) {
        // int buffertin test
        String algo = algorithm.baseAlgo.toUpperCase();
        if (!algo.contains("TRIPLEDES") && !algo.contains("AES_256")
                || Cipher.getMaxAllowedKeyLength(algo) > 128) {
            // skip this if this key length is larger than what's
            // configured in the jce jurisdiction policy files
            System.out.println("Testing " + algorithm.getTransformation());
            for (String type : Arrays.asList(CICO_PBE_Test.INT_BYTE_BUFFER,
                    CICO_PBE_Test.BYTE_ARR_BUFFER)) {
                new CICO_PBE_RW_Test(algorithm)
                    .proceedTest(type);
                new CICO_PBE_SKIP_Test(algorithm)
                    .proceedTest(type);
            }
        }
    }
}
 
开发者ID:lambdalab-mirror,项目名称:jdk8u-jdk,代码行数:20,代码来源:CICOPBEFuncTest.java

示例6: validateCipherKeyLength

import javax.crypto.Cipher; //导入方法依赖的package包/类
/**
 * Ensure that we have strong cryptography extension (JCE) installed.
 */
private void validateCipherKeyLength() {
    try {
        final int keyLength = Cipher.getMaxAllowedKeyLength("AES");
        if (keyLength < MIN_KEY_LENGTH) {
            throw new IllegalStateException("MaxAllowedKeyLength for AES is " + keyLength + "! "
                + "Either install JCE for Oracle JDK or use OpenJDK.");
        }
    } catch (final NoSuchAlgorithmException e) {
        throw new IllegalStateException(e);
    }
}
 
开发者ID:osiegmar,项目名称:setra,代码行数:15,代码来源:Cryptor.java

示例7: wrapperBlowfishKeyTest

import javax.crypto.Cipher; //导入方法依赖的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:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:55,代码来源:TestCipherKeyWrapperTest.java

示例8: main

import javax.crypto.Cipher; //导入方法依赖的package包/类
public static void main(String[] args) throws Exception {
    boolean success = true;
    for (int k : KEY_LENGTHS) {
        if (k > Cipher.getMaxAllowedKeyLength(TRANSFORMATION)) {
            // skip this if this key length is larger than what's
            // allowed in the jce jurisdiction policy files
            continue;
        }
        for (int t : TAG_LENGTHS) {
            for (int n : IV_LENGTHS) {
                for (int p : DATA_LENGTHS) {
                    for (int a : AAD_LENGTHS) {
                        for (int o : OFFSETS) {
                            System.out.printf(TEMPLATE, t, n, p, a, o, k);
                            success &= new GCMParameterSpecTest(
                                    k, t, n, o, p, a).doTest();
                        }
                    }
                }
            }
        }
    }

    if (!success) {
        throw new RuntimeException("At least one test case failed");
    }
}
 
开发者ID:lambdalab-mirror,项目名称:jdk8u-jdk,代码行数:28,代码来源:GCMParameterSpecTest.java

示例9: main

import javax.crypto.Cipher; //导入方法依赖的package包/类
public static void main(String[] args) throws Exception {
    boolean success = true;
    for (int keyLength : KEY_LENGTHS) {
        if (keyLength > Cipher.getMaxAllowedKeyLength(TRANSFORM)) {
            // skip this if this key length is larger than what's
            // configured in the jce jurisdiction policy files
            continue;
        }
        for (int textLength : TXT_LENGTHS) {
            for (int AADLength : AAD_LENGTHS) {
                System.out.println("Key length = " + keyLength
                        + ", text length = " + textLength
                        + ", AAD length = " + AADLength);
                try {
                    run(keyLength, textLength, AADLength);
                    System.out.println("Test case passed");
                } catch (Exception e) {
                    System.out.println("Test case failed: " + e);
                    success = false;
                }
            }
        }
    }

    if (!success) {
        throw new RuntimeException("At least one test case failed");
    }

    System.out.println("Test passed");
}
 
开发者ID:lambdalab-mirror,项目名称:jdk8u-jdk,代码行数:31,代码来源:ReadWriteSkip.java

示例10: runTest

import javax.crypto.Cipher; //导入方法依赖的package包/类
static void runTest(Provider p, String algo, String mode,
        String padding, int keyLength, int textLength, int AADLength,
        int offset) throws Exception {
    System.out.println("Testing " + keyLength + " key length; "
            + textLength + " text lenght; " + AADLength + " AAD length; "
            + offset + " offset");
    if (keyLength > Cipher.getMaxAllowedKeyLength(algo)) {
        // skip this if this key length is larger than what's
        // configured in the jce jurisdiction policy files
        return;
    }
    SameBuffer test = new SameBuffer(p, algo, mode,
            padding, keyLength, textLength, AADLength);

    /*
     * There are four test cases:
     *   1. AAD and text are placed in separated byte arrays
     *   2. AAD and text are placed in the same byte array
     *   3. AAD and text are placed in separated byte buffers
     *   4. AAD and text are placed in the same byte buffer
     */
    Cipher ci = test.createCipher(Cipher.ENCRYPT_MODE, null);
    AlgorithmParameters params = ci.getParameters();
    test.doTestWithSeparateArrays(offset, params);
    test.doTestWithSameArrays(offset, params);
    test.doTestWithSeparatedBuffer(offset, params);
    test.doTestWithSameBuffer(offset, params);
}
 
开发者ID:lambdalab-mirror,项目名称:jdk8u-jdk,代码行数:29,代码来源:SameBuffer.java

示例11: TestCipher

import javax.crypto.Cipher; //导入方法依赖的package包/类
TestCipher(String algo, String[] modes, String[] paddings,
        boolean keyStrength) throws NoSuchAlgorithmException {
    ALGORITHM = algo;
    MODES = modes;
    PADDINGS = paddings;
    this.variousKeySize
            = keyStrength ? Cipher.getMaxAllowedKeyLength(ALGORITHM) : 0;

    IV = generateBytes(8);
    INPUT_TEXT = generateBytes(TEXT_LEN + PAD_BYTES + ENC_OFFSET);
}
 
开发者ID:lambdalab-mirror,项目名称:jdk8u-jdk,代码行数:12,代码来源:TestCipher.java

示例12: checkException

import javax.crypto.Cipher; //导入方法依赖的package包/类
/**
 * Verify the exception type either it is expected to occur or not.
 * @param alg Algorithm name
 * @param param DRBG parameter
 * @param e Exception to verify
 * @throws NoSuchAlgorithmException
 */
private static void checkException(String alg, SecureRandomParameters param,
        NoSuchAlgorithmException e) throws NoSuchAlgorithmException {

    int strength = ((Instantiation) param).getStrength();
    boolean error = true;
    switch (alg) {
        case INVALID_ALGO:
            error = false;
            break;
        case "SHA-224":
        case "SHA-512/224":
            if (strength > 192) {
                error = false;
            }
            break;
        case "SHA-256":
        case "SHA-512/256":
        case "SHA-384":
        case "SHA-512":
            if (strength > 256) {
                error = false;
            }
            break;
        case "AES-128":
        case "AES-192":
        case "AES-256":
            int algoStrength = Integer.parseInt(alg.substring("AES-".length()));
            int maxAESStrength = Cipher.getMaxAllowedKeyLength("AES");
            if (strength > algoStrength
                    || algoStrength > maxAESStrength) {
                error = false;
            }
            break;
    }
    if (error) {
        throw new RuntimeException("Unknown :", e);
    }
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:46,代码来源:ApiTest.java

示例13: getDefaultProviderKey

import javax.crypto.Cipher; //导入方法依赖的package包/类
private static String getDefaultProviderKey() {
    try {
        return "aes/gcm/" + (Cipher.getMaxAllowedKeyLength("AES") > 128 ? "256" : "128");
    } catch (NoSuchAlgorithmException e) {
        return "aes/gcm/128";
    }
}
 
开发者ID:apache,项目名称:nifi-registry,代码行数:8,代码来源:NiFiRegistryPropertiesLoader.java

示例14: runTest

import javax.crypto.Cipher; //导入方法依赖的package包/类
public boolean runTest(Provider p, String algo, PrintStream out)
        throws Exception {

    byte[] salt = new byte[8];
    int ITERATION_COUNT = 1000;
    AlgorithmParameters pbeParams = null;

    String baseAlgo
            = new StringTokenizer(algo, "/").nextToken().toUpperCase();
    boolean isAES = baseAlgo.contains("AES");

    boolean isUnlimited =
        (Cipher.getMaxAllowedKeyLength(algo) == Integer.MAX_VALUE);

    try {
        // Initialization
        new Random().nextBytes(salt);
        AlgorithmParameterSpec aps = new PBEParameterSpec(salt,
                ITERATION_COUNT);
        SecretKeyFactory skf = SecretKeyFactory.getInstance(baseAlgo, p);
        SecretKey key = skf.generateSecret(new PBEKeySpec(
                "Secret Key".toCharArray()));
        Cipher ci = Cipher.getInstance(algo);
        if (isAES) {
            ci.init(Cipher.WRAP_MODE, key);
            pbeParams = ci.getParameters();
        } else {
            ci.init(Cipher.WRAP_MODE, key, aps);
        }

        byte[] keyWrapper = ci.wrap(key);
        if (isAES) {
            ci.init(Cipher.UNWRAP_MODE, key, pbeParams);
        } else {
            ci.init(Cipher.UNWRAP_MODE, key, aps);
        }

        Key unwrappedKey = ci.unwrap(keyWrapper, algo, Cipher.SECRET_KEY);

        if ((baseAlgo.endsWith("TRIPLEDES")
                || baseAlgo.endsWith("AES_256")) && !isUnlimited) {
            out.print(
                    "Expected InvalidKeyException not thrown");
            return false;
        }

        return (Arrays.equals(key.getEncoded(), unwrappedKey.getEncoded()));

    } catch (InvalidKeyException ex) {

        if ((baseAlgo.endsWith("TRIPLEDES")
                || baseAlgo.endsWith("AES_256")) && !isUnlimited) {
            out.print(
                    "Expected InvalidKeyException thrown");
            return true;
        } else {
            throw ex;
        }
    }
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:61,代码来源:TestCipherKeyWrapperPBEKey.java

示例15: main

import javax.crypto.Cipher; //导入方法依赖的package包/类
public static void main(String[] args) throws Exception {

        TestCipherKeyWrapperTest test = new TestCipherKeyWrapperTest();
        // AESWrap and DESedeWrap test
        for (AlgorithmWrapper algoWrapper : AlgorithmWrapper.values()) {
            String algo = algoWrapper.getAlgorithm();
            String wrapper = algoWrapper.getWrapper();
            try {
                int keySize = algoWrapper.getKeySize();
                // only run the tests on longer key lengths if unlimited
                // version of JCE jurisdiction policy files are installed
                if (!(Cipher.getMaxAllowedKeyLength(algo) == Integer.MAX_VALUE)
                        && keySize > LINIMITED_KEYSIZE) {
                    out.println(algo + " will not run if unlimited version of"
                            + " JCE jurisdiction policy files are installed");
                    continue;
                }
                test.wrapperAesDESedeKeyTest(algo, wrapper, keySize);
                if (algoWrapper == AlgorithmWrapper.NegtiveWrap) {
                    throw new RuntimeException("Expected not throw when algo"
                            + " and wrapAlgo are not match:" + algo);
                }
            } catch (InvalidKeyException e) {
                if (algoWrapper == AlgorithmWrapper.NegtiveWrap) {
                    out.println("Expepted exception when algo"
                            + " and wrapAlgo are not match:" + algo);
                } else {
                    throw e;
                }
            }
        }
        test.wrapperBlowfishKeyTest();
        // PBE and public wrapper test.
        String[] publicPrivateAlgos = new String[] { "DiffieHellman", "DSA",
                "RSA" };
        Provider provider = Security.getProvider(SUN_JCE);
        if (provider == null) {
            throw new RuntimeException("SUN_JCE provider not exist");
        }

        test.wrapperPBEKeyTest(provider);
        // Public and private key wrap test
        test.wrapperPublicPriviteKeyTest(provider, publicPrivateAlgos);
    }
 
开发者ID:lambdalab-mirror,项目名称:jdk8u-jdk,代码行数:45,代码来源:TestCipherKeyWrapperTest.java


注:本文中的javax.crypto.Cipher.getMaxAllowedKeyLength方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。