- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我开始寻找一个可以帮助我使用 OpenPGP 加密/解密 MIME 消息的 JAVA 库,并找到了 BouncyCaSTLe。在尝试了解该库的工作原理时,我成功解密了 PGP/Inline 类型的 OpenPGP 加密 MIME 消息。现在我尝试找出是否也可以使用 PGP/MIME 类型的 BouncyCaSTLe MIME 消息进行解密。我在网上搜索了很多,但找不到任何提示是否可能,如果可以的话该怎么做。所以我决定把问题放在这里。任何人都可以提供有关此主题的提示吗?
提前谢谢您!
最佳答案
使用文件中的 key (通常是 .asc 文件)或流中的 key 进行解密(具体取决于您的公司管理 key 的方式,使用 Bouncy CaSTLe:
using Org.BouncyCastle.Bcpg.OpenPgp;
using Org.BouncyCastle.Utilities.IO;
using System;
using System.IO;
using System.Linq;
namespace Cryptor.Agents
{
/// <summary>
/// Decrypts a specified PGP Encrypted File with the specified Keys and Password
/// </summary>
public class PgpDecryptionAgent
{
private readonly string _encryptedFilePath;
private readonly string _outputDirectory;
private readonly PgpEncryptionKeys _pgpKeys;
/// <summary>
/// Constructs the PgpDecryptionAgent with PGP Keys in File
/// </summary>
/// <param name="encryptedFilePath">The path to the encrypted file</param>
/// <param name="outputDirectory">The directory to which the file will be decrypted</param>
/// <param name="publicKeyPath">The path to the public key</param>
/// <param name="privateKeyPath">The path to the private key</param>
/// <param name="password">The password to access the Private key</param>
public PgpDecryptionAgent(string encryptedFilePath, string outputDirectory, string publicKeyPath, string privateKeyPath, string password)
{
_encryptedFilePath = encryptedFilePath;
_outputDirectory = outputDirectory;
_pgpKeys = new PgpEncryptionKeys(publicKeyPath, privateKeyPath, password);
}
/// <summary>
/// Constructs the PgpDecryptionAgent with PGP Keys in Stream
/// </summary>
/// <param name="encryptedFilePath">The path to the encrypted file</param>
/// <param name="outputDirectory">The directory to which the file will be decrypted</param>
/// <param name="publicKey">Stream of the Public key</param>
/// <param name="privateKey">Stream of the Private key</param>
/// <param name="password">The password to access the Private key</param>
public PgpDecryptionAgent(string encryptedFilePath, string outputDirectory, Stream publicKey, Stream privateKey, string password)
{
_encryptedFilePath = encryptedFilePath;
_outputDirectory = outputDirectory;
_pgpKeys = new PgpEncryptionKeys(publicKey, privateKey, password);
}
/// <summary>
/// Performs the Decryption operation
/// </summary>
public void DecryptPgpEncryptedFile()
{
FileStream encryptedFile = new FileStream(_encryptedFilePath, FileMode.Open, FileAccess.Read);
DecryptFile(encryptedFile, _outputDirectory);
}
/// <summary>
/// Performs the Decryption operation
/// </summary>
/// <param name="input">The encrypted file input</param>
/// <param name="outputpath">The decrypted file output</param>
private void DecryptFile(Stream input, string outputpath)
{
input = PgpUtilities.GetDecoderStream(input);
try
{
PgpObjectFactory pgpObjF = new PgpObjectFactory(input);
PgpEncryptedDataList enc;
PgpObject obj = pgpObjF.NextPgpObject();
var list = obj as PgpEncryptedDataList;
if (list != null)
{
enc = list;
}
else
{
enc = (PgpEncryptedDataList) pgpObjF.NextPgpObject();
}
PgpPrivateKey privKey = _pgpKeys.PrivateKey;
PgpPublicKeyEncryptedData pbe = enc.GetEncryptedDataObjects().Cast<PgpPublicKeyEncryptedData>().FirstOrDefault(pked => privKey != null);
if (pbe != null)
{
Stream clear = pbe.GetDataStream(privKey);
PgpObjectFactory plainFact = new PgpObjectFactory(clear);
PgpObject message = plainFact.NextPgpObject();
var pgpCompressedData = message as PgpCompressedData;
if (pgpCompressedData == null) return;
PgpCompressedData cData = pgpCompressedData;
Stream compDataIn = cData.GetDataStream();
PgpObjectFactory o = new PgpObjectFactory(compDataIn);
message = o.NextPgpObject();
PgpLiteralData literalData;
if (message is PgpOnePassSignatureList)
{
message = o.NextPgpObject();
literalData = (PgpLiteralData) message;
Stream output = File.Create(outputpath + "\\" + literalData.FileName);
Stream unc = literalData.GetInputStream();
Streams.PipeAll(unc, output);
}
else
{
literalData = (PgpLiteralData) message;
Stream output = File.Create(outputpath + "\\" + literalData.FileName);
Stream unc = literalData.GetInputStream();
Streams.PipeAll(unc, output);
}
}
}
catch (Exception e)
{
throw new Exception(e.Message);
}
}
/// <summary>
/// The PGP Encryption Keys
/// </summary>
public class PgpEncryptionKeys
{
public PgpPublicKey PublicKey { get; private set; }
public PgpPrivateKey PrivateKey { get; private set; }
public PgpSecretKey SecretKey { get; private set; }
/// <summary>
/// Configure Decryption from Key Streams
/// </summary>
/// <param name="publicKey">Stream of the Public Key</param>
/// <param name="privateKey">Stream of the Private Key</param>
/// <param name="password">Password to access the Private Key</param>
public PgpEncryptionKeys(Stream publicKey, Stream privateKey, string password)
{
PublicKey = ReadPublicKeyFromStream(publicKey);
SecretKey = ReadSecretKeyFromStream(privateKey);
PrivateKey = ReadPrivateKey(password);
}
/// <summary>
/// Configure Decryption from Key Files
/// </summary>
/// <param name="publicKeyPath">String path to the Public Key file</param>
/// <param name="privateKeyPath">String path to the Private key file</param>
/// <param name="passPhrase">Password to access the Private Key</param>
public PgpEncryptionKeys(string publicKeyPath, string privateKeyPath, string passPhrase)
{
if (!File.Exists(publicKeyPath))
throw new ArgumentException("Public key file not found", "publicKeyPath");
if (!File.Exists(privateKeyPath))
throw new ArgumentException("Private key file not found", "privateKeyPath");
if (String.IsNullOrEmpty(passPhrase))
throw new ArgumentException("passPhrase is null or empty.", "passPhrase");
PublicKey = ReadPublicKeyFromFile(publicKeyPath);
SecretKey = ReadSecretKeyFromFile(privateKeyPath);
PrivateKey = ReadPrivateKey(passPhrase);
}
/// <summary>
/// Gets the Secret Key from a File (generally a .asc file)
/// </summary>
/// <param name="privateKeyPath">The path to the Private Key file</param>
private PgpSecretKey ReadSecretKeyFromFile(string privateKeyPath)
{
using (Stream keyIn = File.OpenRead(privateKeyPath))
{
return ReadSecretKeyFromStream(keyIn);
}
}
/// <summary>
/// Gets the Secret Key from a Stream
/// </summary>
/// <param name="keyIn">The Stream of the PGP</param>
private PgpSecretKey ReadSecretKeyFromStream(Stream keyIn)
{
using (Stream inputStream = PgpUtilities.GetDecoderStream(keyIn))
{
PgpSecretKeyRingBundle secretKeyRingBundle = new PgpSecretKeyRingBundle(inputStream);
PgpSecretKey foundKey = GetFirstSecretKey(secretKeyRingBundle);
if (foundKey != null)
return foundKey;
}
throw new ArgumentException("Can't find signing key in key ring.");
}
/// <summary>
/// Obtains the first secret key from the bundle
/// </summary>
/// <param name="secretKeyRingBundle"></param>
/// <returns></returns>
private PgpSecretKey GetFirstSecretKey(PgpSecretKeyRingBundle secretKeyRingBundle)
{
foreach (PgpSecretKeyRing kRing in secretKeyRingBundle.GetKeyRings())
{
PgpSecretKey key = kRing.GetSecretKeys().Cast<PgpSecretKey>().FirstOrDefault(k => k.IsSigningKey);
if (key != null)
return key;
}
return null;
}
/// <summary>
/// Gets the Public Key from a file (generally a .asc file)
/// </summary>
/// <param name="publicKeyPath">The path to the Public Key file</param>
private PgpPublicKey ReadPublicKeyFromFile(string publicKeyPath)
{
using (Stream keyIn = File.OpenRead(publicKeyPath))
{
return ReadPublicKeyFromStream(keyIn);
}
}
/// <summary>
/// Gets the Public Key from a Stream
/// </summary>
/// <param name="keyIn">The Stream of the Public Key</param>
private PgpPublicKey ReadPublicKeyFromStream(Stream keyIn)
{
using (Stream inputStream = PgpUtilities.GetDecoderStream(keyIn))
{
PgpPublicKeyRingBundle publicKeyRingBundle = new PgpPublicKeyRingBundle(inputStream);
PgpPublicKey foundKey = GetFirstPublicKey(publicKeyRingBundle);
if (foundKey != null)
return foundKey;
}
throw new ArgumentException("No encryption key found in public key ring.");
}
/// <summary>
/// Gets the first Public Key from the bundle
/// </summary>
/// <param name="publicKeyRingBundle"></param>
/// <returns></returns>
private PgpPublicKey GetFirstPublicKey(PgpPublicKeyRingBundle publicKeyRingBundle)
{
foreach (PgpPublicKeyRing kRing in publicKeyRingBundle.GetKeyRings())
{
PgpPublicKey key = kRing.GetPublicKeys()
.Cast<PgpPublicKey>()
.Where(k => k.IsEncryptionKey)
.FirstOrDefault();
if (key != null)
return key;
}
return null;
}
/// <summary>
/// Reads the Private Key with the provided password
/// </summary>
/// <param name="passPhrase">The password to access the Private Key</param>
private PgpPrivateKey ReadPrivateKey(string passPhrase)
{
PgpPrivateKey privateKey = SecretKey.ExtractPrivateKey(passPhrase.ToCharArray());
if (privateKey != null)
return privateKey;
throw new ArgumentException("No private key found in secret key.");
}
}
}
}
关于java - 使用 BouncyCaSTLe 解密 PGP/MIME 消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31404248/
我有一个 C# 应用程序调用 Java 网络服务来验证用户密码。我想让 C# 应用程序加密密码,然后让 Java Web 服务解密密码。我已经完成了 Java 端的代码(解密代码),但我无法找出 C#
我正在使用以下代码在使用 openssl 的 Windows 中使用 C 加密和解密二进制数据。如您所见,在这两个函数中,我都知道纯文本的大小。有什么方法可以在不知道纯文本大小的情况下解密消息? #i
已关闭。此问题需要 debugging details 。目前不接受答案。 编辑问题以包含 desired behavior, a specific problem or error, and the
我有一个非常恼人的问题,Java中使用RSA算法对字符串进行不可靠的加密和解密。它似乎只能在大约 35% 的时间内工作,而且我不明白为什么它有时能工作,有时却不能。这是我写的一些测试代码,试图验证加密
我已经设法编写了用于文件加密/解密的函数。但它非常慢,尤其是随着文件大小的增加。例如几MB长的音频/视频文件 我几乎浏览了所有帖子来改进它,并尝试更改算法。如果有任何更改可以帮助我提高性能,请帮助我。
我正在尝试让我的转置密码发挥作用。 每当我将加密方法得到的密文输入解密方法时,我应该得到原始的明文......但事实并非如此...... 我做错了什么? 感谢您的帮助! public String E
我正在使用密码来加密和解密消息: public String encrypt(String string) throws InvalidKeyException, IllegalBlockSizeEx
我有一个在 MySQL 中存储数据的 spring-mvc 堆栈。其中一些数据需要保护,所以我想我应该加密它。由于我以后可能需要使用这些数据(信用卡、SSN 等),所以我需要对其进行解密。我认为这排除
作为一名SEOER,都想了解百度算法,通过算法原理来找到捷径的优化方案,那么今天我把研究多年的百度算法原理解密给大家,可能不是最好的,但是我可以给大家保证,这些都是非常实际的。希望给SEOER带来一
我试图找到一种技术来加密和解密程序中的文件,而无需将密码硬编码到程序中,也无需向用户询问密码。 如果我也可以从我正在编写的另一个程序中解密文件,那就太好了。 到目前为止,我还没有多少运气找到一种看起来
有没有一种方法可以使用作为字符串参数传递给程序的私钥而不是使用存储在机器上的证书来解密 PowerShell 中的 RSA?欢迎任何帮助,我的代码如下。 Function Decrypt-Asymme
通过问题Is it possible to use the Grails Jasypt plugin outside the GORM layer for simple String encrypti
我需要解密/加密我的域类中的几列,并且正在寻找有关如何做的信息。我已经找到了jasypt加密插件,但不幸的是它似乎与Grails 2.4不兼容。 我可能可以将一些东西拼凑在一起,但是想要确保Im遵循最
我需要有关声音文件加密/解密的帮助。我想在存储这个声音文件时加密一个声音文件,并在播放这个文件时解密它。我阅读了有关 java 中的加密/解密以及 java 中可用于此的大量示例代码。但这些程序不适用
我很感兴趣是否可以使用 Excel Visual Basic 和某些加密服务提供程序进行字符串加密/解密。 我找到了一个演练 Encrypting and Decrypting Strings in
我们正在使用加密/解密和UIIMAGE。如果我们在不保存到iphone画廊的情况下进行加密和解密以及UIIMAge,则可以正常工作,但是,如果我们进行加密,保存到画廊,将(加密的图像)加载到应用程序中
我正在做一个像这样的简单程序: package rsaexample; import java.io.*; import java.math.BigInteger; import java.secur
我发现这段代码返回给定字符串的校验和。 public static String getChecksum(String md5) { int counter = 0; while (c
我在 Java SE 和 Android 项目上使用相同的代码。在 Java 和 Android 中运行的应用程序连接到相同的 MQTT 代理并交换消息。消息使用 AES 进行加密/解密。我对 Jav
我想在 openssl/libcrypto 中使用 RSA 加密/解密一个长文件(我知道 AES 更好,但这只是为了比较)。我将输入文件分成大小为 numBlocks = inputFileLengt
我是一名优秀的程序员,十分优秀!