Pra*_*vin 11 java encryption bouncycastle ruby-on-rails openpgp
我试图使用gpg加密加密文件,然后在我的jruby项目中发送它.但是我找不到足够的资源.我尝试使用ruby-gpgme,但jruby不支持C库.我试过阅读Bouncy Castle,但我被课程文档所震撼,并没有找到加密文件的简单文章.
Vivek在这个问题上的答案接近我的解决方案,但只有解密文件的解决方案.我目前正在关注这篇文章并试图在jruby中连接java代码无济于事.我认为encryptFile功能是我需要的,如下所示:
public static void encryptFile(
OutputStream out,
String fileName,
PGPPublicKey encKey,
boolean armor,
boolean withIntegrityCheck)
throws IOException, NoSuchProviderException, PGPException
{
Security.addProvider(new BouncyCastleProvider());
if (armor) {
out = new ArmoredOutputStream(out);
}
ByteArrayOutputStream bOut = new ByteArrayOutputStream();
PGPCompressedDataGenerator comData = new PGPCompressedDataGenerator(PGPCompressedData.ZIP);
PGPUtil.writeFileToLiteralData(
comData.open(bOut),
PGPLiteralData.BINARY,
new File(fileName) );
comData.close();
BcPGPDataEncryptorBuilder dataEncryptor = new BcPGPDataEncryptorBuilder(PGPEncryptedData.TRIPLE_DES);
dataEncryptor.setWithIntegrityPacket(withIntegrityCheck);
dataEncryptor.setSecureRandom(new SecureRandom());
PGPEncryptedDataGenerator encryptedDataGenerator = new PGPEncryptedDataGenerator(dataEncryptor);
encryptedDataGenerator.addMethod(new BcPublicKeyKeyEncryptionMethodGenerator(encKey));
byte[] bytes = bOut.toByteArray();
OutputStream cOut = encryptedDataGenerator.open(out, bytes.length);
cOut.write(bytes);
cOut.close();
out.close();
}
)
Run Code Online (Sandbox Code Playgroud)
我收到以下错误:
NoMethodError: undefined method `ZIP' for Java::OrgBouncycastleOpenpgp::PGPCompressedData:Class
Run Code Online (Sandbox Code Playgroud)
在
PGPCompressedDataGenerator comData = new PGPCompressedDataGenerator(PGPCompressedData.ZIP);
Run Code Online (Sandbox Code Playgroud)
如果你可以在整个jruby中使用gpg帮助我使用代码或加密文件,那将是一个很大的帮助.
更新1 ZIP值结果为整数值的常量,并在此页面中列出.
更新2 我直到功能:
PGPEncryptedDataGenerator encryptedDataGenerator = new PGPEncryptedDataGenerator(dataEncryptor);
encryptedDataGenerator.addMethod(new BcPublicKeyKeyEncryptionMethodGenerator(encKey)); // encKey is class PGPPublicKey's instance
Run Code Online (Sandbox Code Playgroud)
我有从OS生成的公钥.如何encKey从我拥有的公钥字符串创建PGPPublic Key实例?
我找不到足够的答案或 gem 来执行此操作,包括项目文件夹中的 pgp 库。因此,我将此存储库分叉到此存储库以连接 Rails 和系统的 gpg 库。它可以在 ubuntu 上运行。我没有在其他机器上测试过。
在安装了公钥的机器上
encryptObj = Gpgr::Encrypt::GpgFileForEncryption.new
encryptObj.email_address = <email_of_gpg_owner>
encryptObj.file = <path_to_file_to_encrypt>
encryptObj.file_output = <path_to_output_file>
encryptObj.encrypt
Run Code Online (Sandbox Code Playgroud)
在拥有私钥的机器中
decryptObj = Gpgr::Decrypt::GpgFileForDecryption.new
decryptObj.file = <path_to_file_to_decrypt>
decryptObj.file_output = <path_to_output_file>
decryptObj.decrypt
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
575 次 |
| 最近记录: |