當前位置: 首頁>>代碼示例>>Java>>正文


Java AlgorithmParameters類代碼示例

本文整理匯總了Java中java.security.AlgorithmParameters的典型用法代碼示例。如果您正苦於以下問題:Java AlgorithmParameters類的具體用法?Java AlgorithmParameters怎麽用?Java AlgorithmParameters使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。


AlgorithmParameters類屬於java.security包,在下文中一共展示了AlgorithmParameters類的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。

示例1: main

import java.security.AlgorithmParameters; //導入依賴的package包/類
public static void main(String[] args) throws Exception {
    Provider provider = Security.getProvider("SunJCE");
    if (provider == null) {
        throw new RuntimeException("SunJCE provider not exist");
    }
    // generate no-padding cipher with secret key
    Cipher c = Cipher.getInstance("DES/CBC/NoPadding", provider);
    KeyGenerator kgen = KeyGenerator.getInstance("DES", provider);
    SecretKey skey = kgen.generateKey();
    // this is the improperly padded plaintext

    c.init(Cipher.ENCRYPT_MODE, skey);
    // encrypt plaintext
    byte[] cipher = c.doFinal(PLAIN_TEXT);
    AlgorithmParameters params = c.getParameters();
    // generate cipher that enforces PKCS5 padding
    c = Cipher.getInstance("DES/CBC/PKCS5Padding", provider);
    c.init(Cipher.DECRYPT_MODE, skey, params);
    try {
        c.doFinal(cipher);
        throw new RuntimeException(
                "ERROR: Expected BadPaddingException not thrown");
    } catch (BadPaddingException expected) {
        out.println("Expected BadPaddingException thrown");
    }

}
 
開發者ID:AdoptOpenJDK,項目名稱:openjdk-jdk10,代碼行數:28,代碼來源:TextPKCS5PaddingTest.java

示例2: cbc_shortStream

import java.security.AlgorithmParameters; //導入依賴的package包/類
static void cbc_shortStream() throws Exception {
    Cipher c;
    AlgorithmParameters params;
    byte[] read = new byte[200];

    System.out.println("Running cbc_shortStream");

    // Encrypt 97 byte with AES/CBC/PKCS5Padding
    byte[] ct = encryptedText("CBC", 97);
    // Create stream with only 96 bytes of encrypted data
    CipherInputStream in = getStream("CBC", ct, 96);

    try {
        int size = in.read(read);
        in.close();
        if (size != 80) {
            throw new RuntimeException("Fail: CipherInputStream.read() " +
                    "returned " + size + ". Should have been 80");
        }
        System.out.println("  Pass.");
    } catch (IOException e) {
        System.out.println("  Fail:  " + e.getMessage());
        throw new RuntimeException(e.getCause());
    }
}
 
開發者ID:AdoptOpenJDK,項目名稱:openjdk-jdk10,代碼行數:26,代碼來源:CipherInputStreamExceptions.java

示例3: runGCMWithSeparateArray

import java.security.AlgorithmParameters; //導入依賴的package包/類
private void runGCMWithSeparateArray(int mode, byte[] AAD, byte[] text,
        int txtOffset, int lenght, int offset, AlgorithmParameters params)
        throws Exception {
    // first, generate the cipher text at an allocated buffer
    Cipher cipher = createCipher(mode, params);
    cipher.updateAAD(AAD);
    byte[] outputText = cipher.doFinal(text, txtOffset, lenght);

    // new cipher for encrypt operation
    Cipher anotherCipher = createCipher(mode, params);
    anotherCipher.updateAAD(AAD);

    // next, generate cipher text again at the same buffer of plain text
    int myoff = offset;
    int off = anotherCipher.update(text, txtOffset, lenght, text, myoff);
    anotherCipher.doFinal(text, myoff + off);

    // check if two resutls are equal
    if (!isEqual(text, myoff, outputText, 0, outputText.length)) {
        throw new RuntimeException("Two results not equal, mode:" + mode);
    }
}
 
開發者ID:lambdalab-mirror,項目名稱:jdk8u-jdk,代碼行數:23,代碼來源:SameBuffer.java

示例4: doTestWithSameArrays

import java.security.AlgorithmParameters; //導入依賴的package包/類
/**
 * Run the test in case when AAD and text are placed in the same byte
 * array.
 */
private void doTestWithSameArrays(int offset, AlgorithmParameters params)
        throws Exception {
    // prepare buffers to test
    Cipher c = createCipher(Cipher.ENCRYPT_MODE, params);
    int outputLength = c.getOutputSize(textLength);
    int outputBufSize = AADLength + outputLength + offset * 2;

    byte[] AAD_and_text = Helper.generateBytes(outputBufSize);

    // do the test
    runGCMWithSameArray(Cipher.ENCRYPT_MODE, AAD_and_text, AADLength + offset,
            textLength, params);
    int tagLength = c.getParameters()
            .getParameterSpec(GCMParameterSpec.class).getTLen() / 8;
    runGCMWithSameArray(Cipher.DECRYPT_MODE, AAD_and_text, AADLength + offset,
            textLength + tagLength, params);
}
 
開發者ID:AdoptOpenJDK,項目名稱:openjdk-jdk10,代碼行數:22,代碼來源:SameBuffer.java

示例5: doTestWithSameBuffer

import java.security.AlgorithmParameters; //導入依賴的package包/類
private void doTestWithSameBuffer(int offset, AlgorithmParameters params)
        throws Exception {
    // calculate output length
    Cipher c = createCipher(Cipher.ENCRYPT_MODE, params);
    int outputLength = c.getOutputSize(textLength);

    // prepare byte buffer contained AAD and plain text
    int bufSize = AADLength + offset + outputLength;
    byte[] AAD_and_Text = Helper.generateBytes(bufSize);
    ByteBuffer AAD_and_Text_Buf = ByteBuffer.allocate(bufSize);
    AAD_and_Text_Buf.put(AAD_and_Text, 0, AAD_and_Text.length);

    // do test
    runGCMWithSameBuffer(Cipher.ENCRYPT_MODE, AAD_and_Text_Buf, offset,
            textLength, params);
    int tagLength = c.getParameters()
            .getParameterSpec(GCMParameterSpec.class).getTLen() / 8;
    AAD_and_Text_Buf.limit(AADLength + offset + textLength + tagLength);
    runGCMWithSameBuffer(Cipher.DECRYPT_MODE, AAD_and_Text_Buf, offset,
            textLength + tagLength, params);

}
 
開發者ID:AdoptOpenJDK,項目名稱:openjdk-jdk10,代碼行數:23,代碼來源:SameBuffer.java

示例6: engineGetParameters

import java.security.AlgorithmParameters; //導入依賴的package包/類
public AlgorithmParameters engineGetParameters()
{
    if (engineParam == null && engineSpec != null)
    {
        try
        {
            engineParam = AlgorithmParameters.getInstance("IES", BouncyCastleProvider.PROVIDER_NAME);
            engineParam.init(engineSpec);
        }
        catch (Exception e)
        {
            throw new RuntimeException(e.toString());
        }
    }

    return engineParam;
}
 
開發者ID:Appdome,項目名稱:ipack,代碼行數:18,代碼來源:IESCipher.java

示例7: engineGetParameters

import java.security.AlgorithmParameters; //導入依賴的package包/類
protected AlgorithmParameters engineGetParameters()
{
    if (engineParams == null)
    {
        if (paramSpec != null)
        {
            try
            {
                engineParams = AlgorithmParameters.getInstance("OAEP", BouncyCastleProvider.PROVIDER_NAME);
                engineParams.init(paramSpec);
            }
            catch (Exception e)
            {
                throw new RuntimeException(e.toString());
            }
        }
    }

    return engineParams;
}
 
開發者ID:Appdome,項目名稱:ipack,代碼行數:21,代碼來源:CipherSpi.java

示例8: isAvailableCurve

import java.security.AlgorithmParameters; //導入依賴的package包/類
private static boolean isAvailableCurve(int curveId) {
    String oid = idToOidMap.get(curveId);
    if (oid != null) {
        AlgorithmParameters params = null;
        try {
            params = JsseJce.getAlgorithmParameters("EC");
            params.init(new ECGenParameterSpec(oid));
        } catch (Exception e) {
            return false;
        }

        // cache the parameters
        idToParams.put(curveId, params);

        return true;
    }

    return false;
}
 
開發者ID:lambdalab-mirror,項目名稱:jdk8u-jdk,代碼行數:20,代碼來源:SupportedEllipticCurvesExtension.java

示例9: engineGetParameters

import java.security.AlgorithmParameters; //導入依賴的package包/類
protected AlgorithmParameters engineGetParameters()
{
    if (engineParams == null)
    {
        if (paramSpec != null)
        {
            try
            {
                engineParams = AlgorithmParameters.getInstance("PSS", BouncyCastleProvider.PROVIDER_NAME);
                engineParams.init(paramSpec);
            }
            catch (Exception e)
            {
                throw new RuntimeException(e.toString());
            }
        }
    }

    return engineParams;
}
 
開發者ID:Appdome,項目名稱:ipack,代碼行數:21,代碼來源:PSSSignatureSpi.java

示例10: engineInit

import java.security.AlgorithmParameters; //導入依賴的package包/類
public void engineInit(
    int opmode,
    Key key,
    AlgorithmParameters params,
    SecureRandom random)
    throws InvalidKeyException, InvalidAlgorithmParameterException
{
    AlgorithmParameterSpec paramSpec = null;

    if (params != null)
    {
        try
        {
            paramSpec = params.getParameterSpec(IESParameterSpec.class);
        }
        catch (Exception e)
        {
            throw new InvalidAlgorithmParameterException("cannot recognise parameters: " + e.toString());
        }
    }

    engineParam = params;
    engineInit(opmode, key, paramSpec, random);

}
 
開發者ID:Appdome,項目名稱:ipack,代碼行數:26,代碼來源:IESCipher.java

示例11: main

import java.security.AlgorithmParameters; //導入依賴的package包/類
public static void main(String[] args) throws Exception {
    PBEKeySpec ks = new PBEKeySpec(PASSWORD);
    for (int i = 0; i < PBE_ALGOS.length; i++) {
        String algo = PBE_ALGOS[i];
        SecretKeyFactory skf = SecretKeyFactory.getInstance(algo);
        SecretKey key = skf.generateSecret(ks);
        Cipher c = Cipher.getInstance(algo, "SunJCE");
        c.init(Cipher.ENCRYPT_MODE, key);
        c.doFinal(new byte[10]); // force the generation of parameters
        AlgorithmParameters params = c.getParameters();
        if (!params.getAlgorithm().equalsIgnoreCase(algo)) {
            throw new Exception("expect: " + algo +
                                ", but got: " + params.getAlgorithm());
        }
        System.out.println(algo + "...done...");
    }
    System.out.println("Test Passed");
}
 
開發者ID:lambdalab-mirror,項目名稱:jdk8u-jdk,代碼行數:19,代碼來源:PBEParametersTest.java

示例12: permits

import java.security.AlgorithmParameters; //導入依賴的package包/類
@Override
public boolean permits(AlgorithmParameters parameters) {
    String paramAlg = parameters.getAlgorithm();
    if (!algorithm.equalsIgnoreCase(parameters.getAlgorithm())) {
        // Consider the impact of the algorithm aliases.
        Collection<String> aliases =
                AlgorithmDecomposer.getAliases(algorithm);
        if (!aliases.contains(paramAlg)) {
            return true;
        }
    }

    int keySize = KeyUtil.getKeySize(parameters);
    if (keySize == 0) {
        return false;
    } else if (keySize > 0) {
        return !((keySize < minSize) || (keySize > maxSize) ||
            (prohibitedSize == keySize));
    }   // Otherwise, the key size is not accessible or determined.
        // Conservatively, please don't disable such keys.

    return true;
}
 
開發者ID:AdoptOpenJDK,項目名稱:openjdk-jdk10,代碼行數:24,代碼來源:DisabledAlgorithmConstraints.java

示例13: runGCMWithSameArray

import java.security.AlgorithmParameters; //導入依賴的package包/類
private void runGCMWithSameArray(int mode, byte[] array, int txtOffset,
        int length, AlgorithmParameters params) throws Exception {
    // first, generate cipher text at an allocated buffer
    Cipher cipher = createCipher(mode, params);
    cipher.updateAAD(array, 0, AADLength);
    byte[] outputText = cipher.doFinal(array, txtOffset, length);

    // new cipher for encrypt operation
    Cipher anotherCipher = createCipher(mode, params);
    anotherCipher.updateAAD(array, 0, AADLength);

    // next, generate cipher text again at the same buffer of plain text
    int off = anotherCipher.update(array, txtOffset, length,
            array, txtOffset);
    anotherCipher.doFinal(array, txtOffset + off);

    // check if two results are equal or not
    if (!isEqual(array, txtOffset, outputText, 0,
            outputText.length)) {
        throw new RuntimeException(
                "Two results are not equal, mode:" + mode);
    }
}
 
開發者ID:lambdalab-mirror,項目名稱:jdk8u-jdk,代碼行數:24,代碼來源:SameBuffer.java

示例14: combination_12

import java.security.AlgorithmParameters; //導入依賴的package包/類
private void combination_12(List<byte[]> results, int mode, byte[] AAD,
        byte[] plainText, AlgorithmParameters params) throws Exception {

    // prepare ByteBuffer to test
    ByteBuffer buf = ByteBuffer.allocate(AAD.length);
    buf.put(AAD);
    buf.position(0);
    buf.limit(AAD.length);
    Cipher ci = createCipher(mode, params);
    ci.updateAAD(buf);

    // prepare an empty ByteBuffer
    ByteBuffer emptyBuf = ByteBuffer.allocate(0);
    emptyBuf.put(new byte[0]);
    ci.updateAAD(emptyBuf);
    byte[] part12_1 = new byte[ci.getOutputSize(plainText.length)];
    int offset = plainText.length > ARRAY_OFFSET ? ARRAY_OFFSET : 0;
    int len12 = ci.update(plainText, 0, plainText.length - offset,
            part12_1, 0);
    int rest12 = ci.doFinal(plainText, plainText.length - offset, offset,
            part12_1, len12);
    byte[] outputText12 = new byte[len12 + rest12];
    System.arraycopy(part12_1, 0, outputText12, 0, outputText12.length);
    results.add(outputText12);
}
 
開發者ID:AdoptOpenJDK,項目名稱:openjdk-jdk10,代碼行數:26,代碼來源:Encrypt.java

示例15: generateParameterSpec

import java.security.AlgorithmParameters; //導入依賴的package包/類
protected AlgorithmParameterSpec generateParameterSpec(ASN1ObjectIdentifier macOID, SecretKey encKey)
    throws CMSException
{
    try
    {
        if (macOID.equals(PKCSObjectIdentifiers.RC2_CBC))
        {
            byte[] iv = new byte[8];

            random.nextBytes(iv);

            return new RC2ParameterSpec(encKey.getEncoded().length * 8, iv);
        }

        AlgorithmParameterGenerator pGen = helper.createAlgorithmParameterGenerator(macOID);

        AlgorithmParameters p = pGen.generateParameters();

        return p.getParameterSpec(IvParameterSpec.class);
    }
    catch (GeneralSecurityException e)
    {
        return null;
    }
}
 
開發者ID:Appdome,項目名稱:ipack,代碼行數:26,代碼來源:JceCMSMacCalculatorBuilder.java


注:本文中的java.security.AlgorithmParameters類示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。