本文整理汇总了Java中org.bouncycastle.crypto.Wrapper.unwrap方法的典型用法代码示例。如果您正苦于以下问题:Java Wrapper.unwrap方法的具体用法?Java Wrapper.unwrap怎么用?Java Wrapper.unwrap使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类org.bouncycastle.crypto.Wrapper
的用法示例。
在下文中一共展示了Wrapper.unwrap方法的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: extractSecretKey
import org.bouncycastle.crypto.Wrapper; //导入方法依赖的package包/类
protected KeyParameter extractSecretKey(AlgorithmIdentifier keyEncryptionAlgorithm, AlgorithmIdentifier contentEncryptionAlgorithm, byte[] derivedKey, byte[] encryptedContentEncryptionKey)
throws CMSException
{
Wrapper keyEncryptionCipher = EnvelopedDataHelper.createRFC3211Wrapper(keyEncryptionAlgorithm.getAlgorithm());
keyEncryptionCipher.init(false, new ParametersWithIV(new KeyParameter(derivedKey), ASN1OctetString.getInstance(keyEncryptionAlgorithm.getParameters()).getOctets()));
try
{
return new KeyParameter(keyEncryptionCipher.unwrap(encryptedContentEncryptionKey, 0, encryptedContentEncryptionKey.length));
}
catch (InvalidCipherTextException e)
{
throw new CMSException("unable to unwrap key: " + e.getMessage(), e);
}
}
示例2: 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.");
}
}
示例3: heapIssueTest
import org.bouncycastle.crypto.Wrapper; //导入方法依赖的package包/类
private void heapIssueTest()
{
byte[] key = Hex.decode("d305ef52a6b9e72c810b821261d2d678");
byte[] ciphertext = Hex.decode("d2b2906d209a46261d8f6794eca3179d");
Wrapper aes = new AESWrapPadEngine();
aes.init(false, new KeyParameter(key));
try
{
byte[] result = aes.unwrap(ciphertext, 0, ciphertext.length);
fail("incorrect pad not detected");
}
catch (InvalidCipherTextException e)
{
// ignore
}
}
示例4: testChecksum
import org.bouncycastle.crypto.Wrapper; //导入方法依赖的package包/类
private void testChecksum(byte[] kek, byte[] iv, byte[] block, Wrapper wrapper)
{
encryptBlock(kek, iv, block);
try
{
wrapper.unwrap(block, 0, block.length);
fail("bad checksum not detected");
}
catch (InvalidCipherTextException e)
{
if (!e.getMessage().equals("wrapped key fails checksum"))
{
fail("wrong exception");
}
}
}
示例5: 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)));
}
}
示例6: testCorruption
import org.bouncycastle.crypto.Wrapper; //导入方法依赖的package包/类
private void testCorruption()
throws InvalidCipherTextException
{
byte[] kek = Hex.decode("D1DAA78615F287E6");
byte[] iv = Hex.decode("EFE598EF21B33D6D");
Wrapper wrapper = new RFC3211WrapEngine(new DESEngine());
wrapper.init(false, new ParametersWithIV(new KeyParameter(kek), iv));
byte[] block = Hex.decode("ff739D838C627C897323A2F8C436F541");
encryptBlock(kek, iv, block);
try
{
wrapper.unwrap(block, 0, block.length);
fail("bad length not detected");
}
catch (InvalidCipherTextException e)
{
if (!e.getMessage().equals("wrapped key corrupted"))
{
fail("wrong exception on length");
}
}
block = Hex.decode("08639D838C627C897323A2F8C436F541");
testChecksum(kek, iv, block, wrapper);
block = Hex.decode("08736D838C627C897323A2F8C436F541");
testChecksum(kek, iv, block, wrapper);
block = Hex.decode("08739D638C627C897323A2F8C436F541");
testChecksum(kek, iv, block, wrapper);
}
示例7: 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);
}
}