Java 类org.bouncycastle.asn1.cms.GCMParameters 实例源码

项目:Aki-SSL    文件:AES.java   
protected AlgorithmParameters engineGenerateParameters()
{
    byte[]  nonce = new byte[12];

    if (random == null)
    {
        random = new SecureRandom();
    }

    random.nextBytes(nonce);

    AlgorithmParameters params;

    try
    {
        params = createParametersInstance("GCM");
        params.init(new GCMParameters(nonce, 12).getEncoded());
    }
    catch (Exception e)
    {
        throw new RuntimeException(e.getMessage());
    }

    return params;
}
项目:CryptMeme    文件:AES.java   
protected void engineInit(AlgorithmParameterSpec paramSpec)
    throws InvalidParameterSpecException
{
    if (gcmSpecClass != null)
    {
        try
        {
            Method tLen = gcmSpecClass.getDeclaredMethod("getTLen", new Class[0]);
            Method iv= gcmSpecClass.getDeclaredMethod("getIV", new Class[0]);


            gcmParams = new GCMParameters((byte[])iv.invoke(paramSpec, new Object[0]), ((Integer)tLen.invoke(paramSpec, new Object[0])).intValue());
        }
        catch (Exception e)
        {
            throw new InvalidParameterSpecException("Cannot process GCMParameterSpec.");
        }
    }
}
项目:Aki-SSL    文件:AES.java   
protected void engineInit(byte[] params, String format)
    throws IOException
{
    if (!isASN1FormatString(format))
    {
        throw new IOException("unknown format specified");
    }

    gcmParams = GCMParameters.getInstance(params);
}
项目:Aki-SSL    文件:GcmSpecUtil.java   
static GCMParameters extractGcmParameters(AlgorithmParameterSpec paramSpec)
    throws InvalidParameterSpecException
{
    try
    {
        Method tLen = gcmSpecClass.getDeclaredMethod("getTLen", new Class[0]);
        Method iv= gcmSpecClass.getDeclaredMethod("getIV", new Class[0]);

        return new GCMParameters((byte[])iv.invoke(paramSpec, new Object[0]), ((Integer)tLen.invoke(paramSpec, new Object[0])).intValue() / 8);
    }
    catch (Exception e)
    {
        throw new InvalidParameterSpecException("Cannot process GCMParameterSpec");
    }
}
项目:CryptMeme    文件:AES.java   
protected void engineInit(byte[] params, String format)
    throws IOException
{
    if (!isASN1FormatString(format))
    {
        throw new IOException("unknown format specified");
    }

    gcmParams = GCMParameters.getInstance(params);
}
项目:xitk    文件:AESGmacContentSigner.java   
@Override
public AlgorithmIdentifier getAlgorithmIdentifier() {
    GCMParameters params = new GCMParameters(nonce, tagByteLen);
    return new AlgorithmIdentifier(oid, params);
}
项目:Aki-SSL    文件:AES.java   
protected void engineInit(byte[] params)
    throws IOException
{
    gcmParams = GCMParameters.getInstance(params);
}
项目:CryptMeme    文件:AEADTest.java   
private void testGCMParameterSpec(byte[] K,
                                  byte[] N,
                                  byte[] A,
                                  byte[] P,
                                  byte[] C)
    throws InvalidKeyException,
    NoSuchAlgorithmException, NoSuchPaddingException,
    IllegalBlockSizeException, BadPaddingException,
    InvalidAlgorithmParameterException, NoSuchProviderException, IOException
{
    Cipher eax = Cipher.getInstance("AES/EAX/NoPadding", "BC");
    SecretKeySpec key = new SecretKeySpec(K, "AES");

    // GCMParameterSpec mapped to AEADParameters and overrides default MAC
    // size
    GCMParameterSpec spec = new GCMParameterSpec(128, N);
    eax.init(Cipher.ENCRYPT_MODE, key, spec);

    eax.updateAAD(A);
    byte[] c = eax.doFinal(P);

    if (!areEqual(C, c))
    {
        fail("JCE encrypt with additional data and GCMParameterSpec failed.");
    }

    eax.init(Cipher.DECRYPT_MODE, key, spec);
    eax.updateAAD(A);
    byte[] p = eax.doFinal(C);

    if (!areEqual(P, p))
    {
        fail("JCE decrypt with additional data and GCMParameterSpec failed.");
    }

    AlgorithmParameters algParams = eax.getParameters();

    byte[] encParams = algParams.getEncoded();

    GCMParameters gcmParameters = GCMParameters.getInstance(encParams);

    if (!Arrays.areEqual(spec.getIV(), gcmParameters.getNonce()) || spec.getTLen() != gcmParameters.getIcvLen())
    {
        fail("parameters mismatch");
    }
}
项目:CryptMeme    文件:AEADTest.java   
private void testGCMParameterSpecWithRepeatKey(byte[] K,
                                               byte[] N,
                                               byte[] A,
                                               byte[] P,
                                               byte[] C)
    throws InvalidKeyException, NoSuchAlgorithmException,
    NoSuchPaddingException, IllegalBlockSizeException,
    BadPaddingException, InvalidAlgorithmParameterException, NoSuchProviderException, IOException
{
    Cipher eax = Cipher.getInstance("AES/EAX/NoPadding", "BC");
    SecretKeySpec key = new SecretKeySpec(K, "AES");
    GCMParameterSpec spec = new GCMParameterSpec(128, N);
    eax.init(Cipher.ENCRYPT_MODE, key, spec);

    eax.updateAAD(A);
    byte[] c = eax.doFinal(P);

    if (!areEqual(C, c))
    {
        fail("JCE encrypt with additional data and RepeatedSecretKeySpec failed.");
    }

    // Check GCMParameterSpec handling knows about RepeatedSecretKeySpec
    eax.init(Cipher.DECRYPT_MODE, new RepeatedSecretKeySpec("AES"), spec);
    eax.updateAAD(A);
    byte[] p = eax.doFinal(C);

    if (!areEqual(P, p))
    {
        fail("JCE decrypt with additional data and RepeatedSecretKeySpec failed.");
    }

    AlgorithmParameters algParams = eax.getParameters();

    byte[] encParams = algParams.getEncoded();

    GCMParameters gcmParameters = GCMParameters.getInstance(encParams);

    if (!Arrays.areEqual(spec.getIV(), gcmParameters.getNonce()) || spec.getTLen() != gcmParameters.getIcvLen())
    {
        fail("parameters mismatch");
    }
}
项目:CryptMeme    文件:AEADTest.java   
private void testGCMGeneric(byte[] K,
                                  byte[] N,
                                  byte[] A,
                                  byte[] P,
                                  byte[] C)
    throws InvalidKeyException,
    NoSuchAlgorithmException, NoSuchPaddingException,
    IllegalBlockSizeException, BadPaddingException,
    InvalidAlgorithmParameterException, NoSuchProviderException, IOException
{
    Cipher eax = Cipher.getInstance("AES/GCM/NoPadding", "BC");
    SecretKeySpec key = new SecretKeySpec(K, "AES");

    // GCMParameterSpec mapped to AEADParameters and overrides default MAC
    // size
    GCMParameterSpec spec = new GCMParameterSpec(128, N);
    eax.init(Cipher.ENCRYPT_MODE, key, spec);

    eax.updateAAD(A);
    byte[] c = eax.doFinal(P);

    if (!areEqual(C, c))
    {
        fail("JCE encrypt with additional data and GCMParameterSpec failed.");
    }

    eax = Cipher.getInstance("GCM", "BC");
    eax.init(Cipher.DECRYPT_MODE, key, spec);
    eax.updateAAD(A);
    byte[] p = eax.doFinal(C);

    if (!areEqual(P, p))
    {
        fail("JCE decrypt with additional data and GCMParameterSpec failed.");
    }

    AlgorithmParameters algParams = eax.getParameters();

    byte[] encParams = algParams.getEncoded();

    GCMParameters gcmParameters = GCMParameters.getInstance(encParams);

    if (!Arrays.areEqual(spec.getIV(), gcmParameters.getNonce()) || spec.getTLen() != gcmParameters.getIcvLen())
    {
        fail("parameters mismatch");
    }
}
项目:CryptMeme    文件:AES.java   
protected void engineInit(byte[] params)
    throws IOException
{
    gcmParams = GCMParameters.getInstance(params);
}