2012-06-14 74 views
2

使用java加密,簽名,解密和驗證簽名需要執行哪些步驟。 使用PKCS#7算法, java密鑰庫有什麼用?關於PKCS#7。PKCS#7加密

回答

4

步驟1使用keytool實用程序生成密鑰。 here你會發現很好的教程

步驟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; 
    } 
} 

步驟三接下來,假設你想有一些代碼,簽署了一些內容。假設你的內容是一串ASCII文本,你可以用一個字節數組表示。因此您將使用一些Bouncy Castle類來生成「CMS簽名數據」:

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(); 
+0

感謝VIPUL在步驟3'generator.addSigner(getPrivateKey(),(x509證書)getCertificate(), CMSSignedDataGenerator.DIGEST_SHA1);'是什麼getCertificate(),它是如何工作的? – Abhijeet

+0

如果你可以反悔bouncycastle PKCS.They有非常好的文檔。 :) –

+0

請給我你所談論的鏈接....非常感謝 – Abhijeet