PKCS#7加密 [英] PKCS#7 Encryption

查看:238
本文介绍了PKCS#7加密的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用java加密,签名,解密和验证签名需要遵循哪些步骤。
使用PKCS#7算法,
是什么是使用java密钥存储?关于PKCS#7。

What are the steps need to follow to encrypt, sign, decrypt and verify signature using java. using PKCS#7 algorithm, what is the use of java key store ? with respect to PKCS#7.

推荐答案

第1步使用keytool实用程序生成密钥。 =http://i-proving.com/wordpress/2006/05/30/configuring-jboss-ssl =nofollow>这里你会发现很好的教程

Step 1 Generate key using keytool utility.here your will find good tutorial

步骤2 加载密钥库

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.security.GeneralSecurityException;
import java.security.KeyStore;

import org.apache.commons.io.IOUtils;
import org.apache.commons.lang.SystemUtils;

public class MyKeystoreProvider {
  public KeyStore getKeystore(char[] password) throws GeneralSecurityException, IOException {
    KeyStore keystore = KeyStore.getInstance("jks");
    InputStream input = new FileInputStream(SystemUtils.USER_HOME + File.separator + ".keystore");
    try {
      keystore.load(input, password);
    } catch (IOException e) {
    } finally {
      IOUtils.closeQuietly(input);
    }
    return keystore;
  }
}

步骤3接下来,假设你想要一些代码标志一些内容。假设您的内容是一串ASCII文本,您可以表示为字节数组。所以你将使用一些Bouncy城​​堡类来生成CMS签名数据:

Step 3 Next, suppose you want to have some code that signs some content. Let’s say that your content is a bunch of ASCII text that you can represent as an array of bytes. so you will use some Bouncy Castle classes to generate "CMS Signed Data":

  public byte[] sign(byte[] data) throws 
           GeneralSecurityException, CMSException, IOException {
      Security.addProvider(new BouncyCastleProvider());
      CMSSignedDataGenerator generator = new CMSSignedDataGenerator();
      generator.addSigner(getPrivateKey(), (X509Certificate) getCertificate(),
          CMSSignedDataGenerator.DIGEST_SHA1);
      generator.addCertificatesAndCRLs(getCertStore());
      CMSProcessable content = new CMSProcessableByteArray(data);

      CMSSignedData signedData = generator.generate(content, true, "BC");
      return signedData.getEncoded();
    }

private CertStore getCertStore() throws GeneralSecurityException {
  ArrayList<Certificate> list = new ArrayList<Certificate>();
  Certificate[] certificates = getKeystore().getCertificateChain(this.alias);
  for (int i = 0, length = certificates == null ? 0 : certificates.length; i < length; i++) {
    list.add(certificates[i]);
  }
  return CertStore.getInstance("Collection", new CollectionCertStoreParameters(list), "BC");
}

private PrivateKey getPrivateKey() throws GeneralSecurityException {
  if (this.privateKey == null) {
     this.privateKey = initalizePrivateKey();
  }
  return this.privateKey;
}

private PrivateKey initalizePrivateKey() throws GeneralSecurityException {
   KeyStore keystore = new MyKeystoreProvider().getKeystore();
   return (PrivateKey) keystore.getKey(this.alias, getPasswordAsCharArray());
}

现在终于得到了信仰。

CMSSignedData s = new CMSSignedData(signedBytes);
CertStore certs = s.getCertificatesAndCRLs("Collection", "BC");
SignerInformationStore signers = s.getSignerInfos();
boolean verified = false;

    for (Iterator i = signers.getSigners().iterator(); i.hasNext(); ) {
      SignerInformation signer = (SignerInformation) i.next();
      Collection<? extends Certificate> certCollection = certs.getCertificates(signer.getSID());
      if (!certCollection.isEmpty()) {
        X509Certificate cert = (X509Certificate) certCollection.iterator().next();
        if (signer.verify(cert.getPublicKey(), "BC")) {
          verified = true;
        }
      }
    }
    CMSProcessable signedContent = s.getSignedContent() ;
    byte[] originalContent  = (byte[]) signedContent.getContent();

这篇关于PKCS#7加密的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆