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


Java Wrapper.wrap方法代码示例

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


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

示例1: wrapAndUnwrap

import org.bouncycastle.crypto.Wrapper; //导入方法依赖的package包/类
private void wrapAndUnwrap(byte[] kek, byte[] key, byte[] expected)
    throws Exception
{
    Wrapper wrapper = new AESWrapPadEngine();

    wrapper.init(true, new KeyParameter(kek));

    byte[] cipherText = wrapper.wrap(key, 0, key.length);
    if (!areEqual(cipherText, expected))
    {
        fail("Wrapped value does not match expected.");
    }
    wrapper.init(false, new KeyParameter(kek));
    byte[] plainText = wrapper.unwrap(cipherText, 0, cipherText.length);

    if (!areEqual(key, plainText))
    {
        fail("Unwrapped value does not match original.");
    }
}
 
开发者ID:ttt43ttt,项目名称:gwt-crypto,代码行数:21,代码来源:AESWrapPadTest.java

示例2: generateEncryptedBytes

import org.bouncycastle.crypto.Wrapper; //导入方法依赖的package包/类
public byte[] generateEncryptedBytes(AlgorithmIdentifier keyEncryptionAlgorithm, byte[] derivedKey, GenericKey contentEncryptionKey)
    throws CMSException
{
    byte[] contentEncryptionKeySpec = ((KeyParameter)CMSUtils.getBcKey(contentEncryptionKey)).getKey();
    Wrapper keyEncryptionCipher = EnvelopedDataHelper.createRFC3211Wrapper(keyEncryptionAlgorithm.getAlgorithm());

    keyEncryptionCipher.init(true, new ParametersWithIV(new KeyParameter(derivedKey), ASN1OctetString.getInstance(keyEncryptionAlgorithm.getParameters()).getOctets()));

    return keyEncryptionCipher.wrap(contentEncryptionKeySpec, 0, contentEncryptionKeySpec.length);
}
 
开发者ID:Appdome,项目名称:ipack,代码行数:11,代码来源:BcPasswordRecipientInfoGenerator.java

示例3: wrapTest

import org.bouncycastle.crypto.Wrapper; //导入方法依赖的package包/类
private void wrapTest(
    int          id,
    BlockCipher  engine,
    byte[]       kek,
    byte[]       iv,
    SecureRandom rand,
    byte[]       in,
    byte[]       out)
    throws Exception
{
    Wrapper wrapper = new RFC3211WrapEngine(engine);

    wrapper.init(true, new ParametersWithRandom(new ParametersWithIV(new KeyParameter(kek), iv), rand));

    byte[]  cText = wrapper.wrap(in, 0, in.length);
    if (!Arrays.areEqual(cText, out))
    {
        fail("failed wrap test " + id  + " expected " + new String(Hex.encode(out)) + " got " + new String(Hex.encode(cText)));
    }

    wrapper.init(false, new ParametersWithIV(new KeyParameter(kek), iv));

    byte[]  pText = wrapper.unwrap(out, 0, out.length);
    if (!Arrays.areEqual(pText, in))
    {
        fail("rfailed unwrap test " + id  + " expected " + new String(Hex.encode(in)) + " got " + new String(Hex.encode(pText)));
    }
}
 
开发者ID:ttt43ttt,项目名称:gwt-crypto,代码行数:29,代码来源:RFC3211WrapTest.java

示例4: wrap

import org.bouncycastle.crypto.Wrapper; //导入方法依赖的package包/类
public byte[] wrap(byte[] in, int inOff, int inLen)
{
    if (!forWrapping)
    {
        throw new IllegalStateException("not set for wrapping");
    }
    byte[] iv = new byte[8];

    // MLI = size of key to be wrapped
    byte[] mli = Pack.intToBigEndian(inLen);
    // copy in the fixed portion of the AIV
    System.arraycopy(preIV, 0, iv, 0, preIV.length);
    // copy in the MLI after the AIV
    System.arraycopy(mli, 0, iv, preIV.length, mli.length);

    // get the relevant plaintext to be wrapped
    byte[] relevantPlaintext = new byte[inLen];
    System.arraycopy(in, inOff, relevantPlaintext, 0, inLen);
    byte[] paddedPlaintext = padPlaintext(relevantPlaintext);

    if (paddedPlaintext.length == 8)
    {
        // if the padded plaintext contains exactly 8 octets,
        // then prepend iv and encrypt using AES in ECB mode.

        // prepend the IV to the plaintext
        byte[] paddedPlainTextWithIV = new byte[paddedPlaintext.length + iv.length];
        System.arraycopy(iv, 0, paddedPlainTextWithIV, 0, iv.length);
        System.arraycopy(paddedPlaintext, 0, paddedPlainTextWithIV, iv.length, paddedPlaintext.length);

        engine.init(true, param);
        for (int i = 0; i < paddedPlainTextWithIV.length; i += engine.getBlockSize())
        {
            engine.processBlock(paddedPlainTextWithIV, i, paddedPlainTextWithIV, i);
        }

        return paddedPlainTextWithIV;
    }
    else
    {
        // otherwise, apply the RFC 3394 wrap to
        // the padded plaintext with the new IV
        Wrapper wrapper = new RFC3394WrapEngine(engine);
        ParametersWithIV paramsWithIV = new ParametersWithIV(param, iv);
        wrapper.init(true, paramsWithIV);
        return wrapper.wrap(paddedPlaintext, 0, paddedPlaintext.length);
    }

}
 
开发者ID:ttt43ttt,项目名称:gwt-crypto,代码行数:50,代码来源:RFC5649WrapEngine.java

示例5: performTest

import org.bouncycastle.crypto.Wrapper; //导入方法依赖的package包/类
public void performTest()
    throws Exception
{
    // test RFC 5649 test vectors
    byte[] kek = Hex.decode("5840df6e29b02af1ab493b705bf16ea1ae8338f4dcc176a8");
    byte[] key = Hex.decode("c37b7e6492584340bed12207808941155068f738");
    byte[] wrap = Hex.decode("138bdeaa9b8fa7fc61f97742e72248ee5ae6ae5360d1ae6a5f54f373fa543b6a");

    wrapAndUnwrap(kek, key, wrap);

    wrap = Hex.decode("afbeb0f07dfbf5419200f2ccb50bb24f");
    key = Hex.decode("466f7250617369");
    wrapAndUnwrap(kek, key, wrap);


    //
    // offset test
    //
    Wrapper wrapper = new AESWrapPadEngine();

    byte[] pText = new byte[5 + key.length];
    byte[]  cText;

    System.arraycopy(key, 0, pText, 5, key.length);

    wrapper.init(true, new KeyParameter(kek));

    cText = wrapper.wrap(pText, 5, key.length);
    if (!Arrays.areEqual(cText, wrap))
    {
        fail("failed offset wrap test expected " + new String(Hex.encode(wrap)) + " got " + new String(Hex.encode(cText)));
    }

    wrapper.init(false, new KeyParameter(kek));

    cText = new byte[6 + wrap.length];
    System.arraycopy(wrap, 0, cText, 6, wrap.length);

    pText = wrapper.unwrap(cText, 6, wrap.length);
    if (!Arrays.areEqual(pText, key))
    {
        fail("failed offset unwrap test expected " + new String(Hex.encode(key)) + " got " + new String(Hex.encode(pText)));
    }

    // test random values
    SecureRandom rnd = new SecureRandom();
    for (int i = 0; i < numOfRandomIterations; i++)
    {
        int kekLength = 128;
        boolean shouldIncrease = (rnd.nextInt() & 0x01) != 0;
        if (shouldIncrease)
        {
            kekLength = 256;
        }
        kek = new byte[kekLength / 8];
        rnd.nextBytes(kek);
        int keyToWrapSize = RNGUtils.nextInt(rnd, 256 / 8 - 8) + 8;
        byte[] keyToWrap = new byte[keyToWrapSize];
        rnd.nextBytes(keyToWrap);
        wrapAndUnwrap(kek, keyToWrap);
    }
}
 
开发者ID:ttt43ttt,项目名称:gwt-crypto,代码行数:63,代码来源:AESWrapPadTest.java

示例6: wrap

import org.bouncycastle.crypto.Wrapper; //导入方法依赖的package包/类
public byte[] wrap(byte[] in, int inOff, int inLen)
{
    if (!forWrapping)
    {
        throw new IllegalStateException("not set for wrapping");
    }
    byte[] iv = new byte[8];

    // MLI = size of key to be wrapped
    byte[] mli = Pack.intToBigEndian(inLen);
    // copy in the fixed portion of the AIV
    System.arraycopy(preIV, 0, iv, 0, preIV.length);
    // copy in the MLI after the AIV
    System.arraycopy(mli, 0, iv, preIV.length, mli.length);

    // get the relevant plaintext to be wrapped
    byte[] relevantPlaintext = new byte[inLen];
    System.arraycopy(in, inOff, relevantPlaintext, 0, inLen);
    byte[] paddedPlaintext = padPlaintext(relevantPlaintext);

    if (paddedPlaintext.length == 8)
    {
        // if the padded plaintext contains exactly 8 octets,
        // then prepend iv and encrypt using AES in ECB mode.

        // prepend the IV to the plaintext
        byte[] paddedPlainTextWithIV = new byte[paddedPlaintext.length + iv.length];
        System.arraycopy(iv, 0, paddedPlainTextWithIV, 0, iv.length);
        System.arraycopy(paddedPlaintext, 0, paddedPlainTextWithIV, iv.length, paddedPlaintext.length);

        engine.init(true, param);
        for (int i = 0; i < paddedPlainTextWithIV.length; i += engine.getBlockSize())
        {
            engine.processBlock(paddedPlainTextWithIV, i, paddedPlainTextWithIV, i);
        }

        return paddedPlainTextWithIV;
    }
    else
    {
        // otherwise, apply the RFC 3394 wrap to
        // the padded plaintext with the new IV
        Wrapper wrapper = new RFC3394WrapEngine(engine);
        ParametersWithIV paramsWithIV = new ParametersWithIV(param, iv);
        wrapper.init(true, paramsWithIV);
        return wrapper.wrap(paddedPlaintext, inOff, paddedPlaintext.length);
    }

}
 
开发者ID:redfish64,项目名称:TinyTravelTracker,代码行数:50,代码来源:RFC5649WrapEngine.java


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