openssl c++实现bouncycastle中AES加解密
0x01 為什么要用bouncycastle
先說說JCE(Java Cryptography Extension)是一組包,它們提供用于加密、密鑰生成和協(xié)商以及 Message Authentication Code(MAC)算法的框架和實(shí)現(xiàn)。
它提供對(duì)對(duì)稱、不對(duì)稱、塊和流密碼的加密支持,它還支持安全流和密封的對(duì)象。它不對(duì)外出口,用它開發(fā)完成封裝后將無法調(diào)用。由于美國(guó)出口控制規(guī)定,JCA是可出口的,但是JCE對(duì)部分國(guó)家是限制出口的。因此,要實(shí)現(xiàn)一個(gè)完整的安全結(jié)構(gòu),就需要一個(gè)活多個(gè)第三方廠商提供JCE產(chǎn)品,稱為安全供應(yīng)商。bouncycastle JCE就是其中一個(gè)安全供應(yīng)者。
舉個(gè)例子:
在向一個(gè)國(guó)際CA發(fā)送簽名請(qǐng)求后,CA會(huì)返回PKCS12格式的個(gè)人信息交換文件(.p12,.pfx)。此時(shí)使用sun自帶的提供者(provider)進(jìn)行讀取產(chǎn)生私鑰信息(keystore)時(shí),
KeyStore ks = KeyStore.getInstance("PKCS12", "SUN");ks.load(new FileInputStream("visa.p12"), "password".toCharArray());可能(只是可能)會(huì)拋出 java.io.IOException: unsupported PKCS12 bag type 1.2.840.113549.1.12.10.1.1at com.sun.net.ssl.internal.ssl.PKCS12KeyStore.a(Unknown Source)at com.sun.net.ssl.internal.ssl.PKCS12KeyStore.engineLoad(Unknown Source)at java.security.KeyStore.load(Unknown Source)
這種情況就需要選用bouncycastle第三方完成讀取產(chǎn)生私鑰信息。
0x02 如何在JAVA環(huán)境中使用
第一步,首先到https://bouncycastle.org/latest_releases.html下載對(duì)應(yīng)的jar包,我這里下載是bcprov-jdk14-143.jar
第二步,將bcprov-jdk14-143.jar拷貝到%JAVA_HOME%/jre1.8.0_60/lib/ext/目錄下,并修改java_security文件
第三步,在eclipse中創(chuàng)建java工程
按向?qū)?chuàng)建
第四步,引入provider類名并注冊(cè)使用
import org.bouncycastle.jce.provider.*; Security.insertProviderAt((Provider)new BouncyCastleProvider(), 1);is = new BufferedInputStream(new FileInputStream(crypt12FileEnc));int length = is.available();System.out.println("length:" + length); cipher = Cipher.getInstance("AES/GCM/NoPadding", "BC"); cipher.init(Cipher.DECRYPT_MODE, new SecretKeySpec(aesK, "AES"), new IvParameterSpec(aesIV)); isCipher = new CipherInputStream(is, cipher);os = new FileOutputStream(decryptedZlibFile);int templen = 0;while((read=isCipher.read(buffer))!=-1) { templen += read;System.out.println("read:" + read);os.write(buffer, 0, read); }System.out.println("allread:" + templen);os.close();is.close();這個(gè)時(shí)候,可能會(huì)報(bào)java異常 exception:Illegal key size java.security.InvalidKeyException: Illegal key sizeat javax.crypto.Cipher.checkCryptoPerm(Cipher.java:1039)at javax.crypto.Cipher.init(Cipher.java:1393)at javax.crypto.Cipher.init(Cipher.java:1327)at crypt12.main(crypt12.java:81)
- jdk對(duì)應(yīng)jar包的路徑:D:\Java\jdk1.8.0_60\jre\lib\security
- jre對(duì)應(yīng)jar包的路徑:D:\Java\jre1.8.0_60\lib\security
我這里是jdk8,所以對(duì)應(yīng)的下載
地址:
http://www.oracle.com/technetwork/java/javase/downloads/jce8-download-2133166.html
當(dāng)然還有其他版本的:
jdk 5: http://www.oracle.com/technetwork/java/javasebusiness/downloads/java-archive-downloads-java-plat-419418.html#jce_policy-1.5.0-oth-JPR
jdk6: http://www.oracle.com/technetwork/java/javase/downloads/jce-6-download-429243.html
0x03 如何用openssl實(shí)現(xiàn)AES加解密
在openssl中EVP_CIPHER的使用跟java里面Cipher的使用及其相似,真懷疑是不是相互有借鑒。但要注意EVP_CIPHER_CTX_ctrl的使用是不可或缺的,在加解密中若忽略此函數(shù)的使用,會(huì)直接導(dǎo)致加解密失敗,使用方法如下:
#define BUFFERSIZE 8192 EVP_CIPHER_CTX ctx;const EVP_CIPHER *cipher = EVP_aes_256_gcm();EVP_CIPHER_CTX_init(&ctx);EVP_CIPHER_CTX_set_padding(&ctx, 0);EVP_DecryptInit_ex(&ctx, cipher, NULL, NULL, NULL);EVP_CIPHER_CTX_ctrl(&ctx, EVP_CTRL_GCM_SET_IVLEN, 16, NULL);EVP_DecryptInit_ex(&ctx, NULL, NULL, (unsigned char*)aesK, (unsigned char*)aesIV);int index = 0;std::ofstream zlibOfs(zlibFile.c_str(), std::ios::binary);while (needCryLen > 0){memset(buffer, 0, BUFFERSIZE);memcpy_s(buffer, BUFFERSIZE, cryBuffer + BUFFERSIZE * index, needCryLen <= BUFFERSIZE ? needCryLen - 20 : BUFFERSIZE);int outLen = 0;int decryOutLen = 0;EVP_DecryptUpdate(&ctx, (unsigned char*)outBuffer, &outLen, (unsigned char*)buffer, needCryLen <= BUFFERSIZE ? needCryLen - 20 : BUFFERSIZE);decryOutLen += outLen;EVP_DecryptFinal_ex(&ctx, (unsigned char*)outBuffer, &outLen);decryOutLen += outLen;zlibOfs.write(outBuffer, decryOutLen);index++;needCryLen = needCryLen - BUFFERSIZE;}zlibOfs.close();EVP_CIPHER_CTX_cleanup(&ctx);
總結(jié)
以上是生活随笔為你收集整理的openssl c++实现bouncycastle中AES加解密的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: PID控制器改进笔记之三:改进PID控制
- 下一篇: Java反射基础(一)--Class对象