Yug*_*hou 5 java encryption bouncycastle pgp openpgp
我对BouncyCastle和pgp很新.我在互联网上看过很多文章和样本.几乎每个加密样本都包含下面剪切的代码
if (armor)
out = new ArmoredOutputStream(out);
Run Code Online (Sandbox Code Playgroud)
似乎我的本地测试通过了护甲和无护甲.我用google搜索但发现很少有用,而ArmoredOutputStream的javadoc只显示这是基本的输出流.
那么有什么区别以及何时使用它?
完整的代码示例:
public static void encryptFile(String decryptedFilePath,
String encryptedFilePath,
String encKeyPath,
boolean armor,
boolean withIntegrityCheck)
throws Exception{
OutputStream out = new FileOutputStream(encryptedFilePath);
FileInputStream pubKey = new FileInputStream(encKeyPath);
PGPPublicKey encKey = readPublicKeyFromCollection2(pubKey);
Security.addProvider(new BouncyCastleProvider());
if (armor)
out = new ArmoredOutputStream(out);
// Init encrypted data generator
PGPEncryptedDataGenerator encryptedDataGenerator =
new PGPEncryptedDataGenerator(PGPEncryptedData.CAST5, withIntegrityCheck, new SecureRandom(),"BC");
encryptedDataGenerator.addMethod(encKey);
OutputStream encryptedOut = encryptedDataGenerator.open(out, new byte[BUFFER_SIZE]);
// Init compression
PGPCompressedDataGenerator compressedDataGenerator = new PGPCompressedDataGenerator(PGPCompressedData.ZIP);
OutputStream compressedOut = compressedDataGenerator.open(encryptedOut);
PGPLiteralDataGenerator literalDataGenerator = new PGPLiteralDataGenerator();
OutputStream literalOut = literalDataGenerator.open(compressedOut, PGPLiteralData.BINARY, decryptedFilePath, new Date(), new byte[BUFFER_SIZE]);
FileInputStream inputFileStream = new FileInputStream(decryptedFilePath);
byte[] buf = new byte[BUFFER_SIZE];
int len;
while((len = inputFileStream.read(buf))>0){
literalOut.write(buf,0,len);
}
literalOut.close();
literalDataGenerator.close();
compressedOut.close();
compressedDataGenerator.close();
encryptedOut.close();
encryptedDataGenerator.close();
inputFileStream.close();
out.close();
}
}
Run Code Online (Sandbox Code Playgroud)
ASCII Armor 是一个通用术语,表示二进制数据表示为纯 ASCII 文本。从技术上讲,有很多方法可以对二进制数据进行ascii-armor,但是在与密码学相关的领域中,PEM 格式很普遍(也可以在 serverfault 中查看此问题和相关问题)。
所述PEM是基本上包裹在Base64编码的二进制数据-----BEGIN SOMETHING-----和-----END SOMETHING-----分隔符和一组可以包含关于二进制内容一些元信息的附加报头。
| 归档时间: |
|
| 查看次数: |
3952 次 |
| 最近记录: |