⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 kekenvelopeddataexample.java

📁 sip CMSEnvelopedData
💻 JAVA
字号:
package chapter9;

import java.util.Arrays;

import javax.crypto.*;

import org.bouncycastle.cms.*;

/**
 * Demonstrate creation and processing a key-encrypted key enveloped-message.
 */
public class KEKEnvelopedDataExample
{
    public static void main(String[] args)
        throws Exception
    {
        KeyGenerator    keyGen = KeyGenerator.getInstance("DESEDE", "BC");
        SecretKey       key  = keyGen.generateKey();
        
        // set up the generator
        CMSEnvelopedDataGenerator edGen = new CMSEnvelopedDataGenerator();

        byte[]  kekID = new byte[] { 1, 2, 3, 4, 5 };

        edGen.addKEKRecipient(key, kekID);
        
        // create the enveloped-data object
        CMSProcessable  data = new CMSProcessableByteArray("Hello World!".getBytes());

        CMSEnvelopedData enveloped = edGen.generate(
                                data,
                                CMSEnvelopedDataGenerator.AES128_CBC, "BC");
        // recreate
        enveloped = new CMSEnvelopedData(enveloped.getEncoded());

        // look for our recipient
        RecipientId     recId = new RecipientId();

        recId.setKeyIdentifier(kekID);

        RecipientInformationStore   recipients = enveloped.getRecipientInfos();
        RecipientInformation        recipient = recipients.get(recId);
        
        if (recipient != null)
        {
            // decrypt the data
            byte[] recData = recipient.getContent(key, "BC");

            // compare recovered data to the original data
            if (Arrays.equals((byte[])data.getContent(), recData))
            {
                System.out.println("data recovery succeeded");
            }
            else
            {
                System.out.println("data recovery failed");
            }
        }
        else
        {
            System.out.println("could not find a matching recipient");
        }
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -