- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我创建了一个小程序来使用 Bouncy CaSTLe 1.47 API 生成 DSA/El Gamal PGP key 环。 key 生成非常顺利,没有错误。我使用装甲输出将私钥和公钥导出到一个文件中,当我尝试使用 GPG(确切地说是 KGpg)导入生成的私钥时,出现以下错误:
[GNUPG:] NODATA 1
[GNUPG:] IMPORT_OK 17 1277C25B455C71D91EE42C8FF9A6087305C00DA6
[GNUPG:] IMPORTED F9A6087305C00DA6 test@gmail.com
[GNUPG:] IMPORT_OK 1 1277C25B455C71D91EE42C8FF9A6087305C00DA6
[GNUPG:] IMPORT_RES 1 0 1 0 0 0 0 0 0 1 1 0 0 0
单击“确定”后,它告诉我只处理了 1 个 key 。看起来它只使用了 DSA key ,因为在屏幕上它显示为 1024/0。
** 编辑 **我刚刚尝试在 Windows 上的 PGP 10.1.0 中打开 key ,它也给了我一个错误“ key 环包含一个坏的(损坏的)PGP 数据包。”
我正在使用的代码如下所示,第一个是 Utility 类,第二个是调用它来创建 Key 的程序。
package george.crypto.pgp;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.NoSuchAlgorithmException;
import java.security.NoSuchProviderException;
import java.security.Security;
import java.util.Date;
import org.bouncycastle.bcpg.ArmoredOutputStream;
import org.bouncycastle.bcpg.HashAlgorithmTags;
import org.bouncycastle.jce.provider.BouncyCastleProvider;
import org.bouncycastle.jce.spec.ElGamalParameterSpec;
import org.bouncycastle.openpgp.PGPEncryptedData;
import org.bouncycastle.openpgp.PGPKeyPair;
import org.bouncycastle.openpgp.PGPKeyRingGenerator;
import org.bouncycastle.openpgp.PGPPublicKey;
import org.bouncycastle.openpgp.PGPPublicKeyRing;
import org.bouncycastle.openpgp.PGPSecretKeyRing;
import org.bouncycastle.openpgp.PGPSignature;
import org.bouncycastle.openpgp.operator.PGPDigestCalculator;
import org.bouncycastle.openpgp.operator.jcajce.JcaPGPContentSignerBuilder;
import org.bouncycastle.openpgp.operator.jcajce.JcaPGPDigestCalculatorProviderBuilder;
import org.bouncycastle.openpgp.operator.jcajce.JcePBESecretKeyEncryptorBuilder;
public final class PGPTools {
static {
Security.addProvider(new BouncyCastleProvider());
}
private PGPTools() {
}
public static final void exportSecretKey(PGPKeyRingGenerator pgpKeyRingGen, File keyFile, boolean asciiArmor) throws IOException {
PGPSecretKeyRing pgpSecKeyRing = pgpKeyRingGen.generateSecretKeyRing();
if(asciiArmor) {
pgpSecKeyRing.encode(new ArmoredOutputStream(new FileOutputStream(keyFile)));
}
else {
pgpSecKeyRing.encode(new FileOutputStream(keyFile));
}
}
public static final void exportPublicKey(PGPKeyRingGenerator pgpKeyRingGen, File keyFile, boolean asciiArmor) throws IOException {
PGPPublicKeyRing pgpPubKeyRing = pgpKeyRingGen.generatePublicKeyRing();
if(asciiArmor) {
pgpPubKeyRing.encode(new ArmoredOutputStream(new FileOutputStream(keyFile)));
}
else {
pgpPubKeyRing.encode(new FileOutputStream(keyFile));
}
}
/**
*
* @param dsaKeyPair - the generated DSA key pair
* @param elGamalKeyPair - the generated El Gamal key pair
* @param identity - the given identity of the key pair ring
* @param passphrase - the secret pass phrase to protect the key pair
* @return a PGP Key Ring Generate with the El Gamal key pair added as sub key
* @throws Exception
*/
@SuppressWarnings("deprecation")
public static final PGPKeyRingGenerator createPGPKeyRingGenerator(KeyPair dsaKeyPair, KeyPair elGamalKeyPair, String identity, char[] passphrase) throws Exception
{
PGPKeyPair dsaPgpKeyPair = new PGPKeyPair(PGPPublicKey.DSA, dsaKeyPair, new Date());
PGPKeyPair elGamalPgpKeyPair = new PGPKeyPair(PGPPublicKey.ELGAMAL_ENCRYPT, elGamalKeyPair, new Date());
PGPDigestCalculator sha1Calc = new JcaPGPDigestCalculatorProviderBuilder().build().get(HashAlgorithmTags.SHA1);
PGPKeyRingGenerator keyRingGen = new PGPKeyRingGenerator(PGPSignature.POSITIVE_CERTIFICATION, dsaPgpKeyPair, identity, sha1Calc, null, null, new JcaPGPContentSignerBuilder(dsaPgpKeyPair.getPublicKey().getAlgorithm(), HashAlgorithmTags.SHA1), new JcePBESecretKeyEncryptorBuilder(PGPEncryptedData.AES_256, sha1Calc).setProvider("BC").build(passphrase));
keyRingGen.addSubKey(elGamalPgpKeyPair);
return keyRingGen;
}
/**
*
* @param keySize 512 - 1024 (multiple of 64)
* @return the DSA generated key pair
* @throws NoSuchProviderException
* @throws NoSuchAlgorithmException
*/
public static final KeyPair generateDsaKeyPair(int keySize) throws NoSuchAlgorithmException, NoSuchProviderException
{
KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("DSA", "BC");
keyPairGenerator.initialize(keySize);
KeyPair keyPair = keyPairGenerator.generateKeyPair();
return keyPair;
}
/**
*
* @param keySize - 1024, 2048, 4096
* @return the El Gamal generated key pair
* @throws Exception
*/
public static final KeyPair generateElGamalKeyPair(int keySize) throws Exception
{
KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("ELGAMAL", "BC");
keyPairGenerator.initialize(keySize);
KeyPair keyPair = keyPairGenerator.generateKeyPair();
return keyPair;
}
/**
*
* @param paramSpecs - the pre-defined parameter specs
* @return the El Gamal generated key pair
* @throws Exception
*/
public static final KeyPair generateElGamalKeyPair(ElGamalParameterSpec paramSpecs) throws Exception
{
KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("ELGAMAL", "BC");
keyPairGenerator.initialize(paramSpecs);
KeyPair keyPair = keyPairGenerator.generateKeyPair();
return keyPair;
}
}
package george.crypto.pgp;
import java.io.File;
import java.math.BigInteger;
import java.security.KeyPair;
import org.bouncycastle.jce.spec.ElGamalParameterSpec;
import org.bouncycastle.openpgp.PGPKeyRingGenerator;
public class PGPCryptoBC {
public PGPCryptoBC() {
try {
String keysDir = System.getProperty("user.dir")+File.separator+"src/george/crypto/pgp/keys";
//Generating a safe prime is a very long process so it's better to use
//a pre-generated safe prime, I took this from http://www.cryptopp.com/fom-serve/cache/71.html
BigInteger primeModulous = new BigInteger("36F0255DDE973DCB3B399D747F23E32ED6FDB1F77598338BFDF44159C4EC64DDAEB5F78671CBFB22106AE64C32C5BCE4CFD4F5920DA0EBC8B01ECA9292AE3DBA1B7A4A899DA181390BB3BD1659C81294F400A3490BF9481211C79404A576605A5160DBEE83B4E019B6D799AE131BA4C23DFF83475E9C40FA6725B7C9E3AA2C6596E9C05702DB30A07C9AA2DC235C5269E39D0CA9DF7AAD44612AD6F88F69699298F3CAB1B54367FB0E8B93F735E7DE83CD6FA1B9D1C931C41C6188D3E7F179FC64D87C5D13F85D704A3AA20F90B3AD3621D434096AA7E8E7C66AB683156A951AEA2DD9E76705FAEFEA8D71A5755355970000000000000001", 16);
BigInteger baseGenerator = new BigInteger("2", 16);
ElGamalParameterSpec paramSpecs = new ElGamalParameterSpec(primeModulous, baseGenerator);
KeyPair dsaKeyPair = PGPTools.generateDsaKeyPair(1024);
KeyPair elGamalKeyPair = PGPTools.generateElGamalKeyPair(paramSpecs);
PGPKeyRingGenerator pgpKeyRingGen = PGPTools.createPGPKeyRingGenerator(dsaKeyPair, elGamalKeyPair, "test@gmail.com", "TestPass12345!".toCharArray());
PGPTools.exportSecretKey(pgpKeyRingGen, new File(keysDir+File.separator+"secret.asc"), true);
PGPTools.exportPublicKey(pgpKeyRingGen, new File(keysDir+File.separator+"public.asc"), true);
}
catch(Exception ex) {
ex.printStackTrace();
}
}
public static void main(String ... args) {
new PGPCryptoBC();
}
}
生成的输出是 2 个文件“secret.asc”和“public.asc”如果我尝试在 GPG 中导入“secret.asc”我会收到错误消息并且 El Gamal 子 key 不会被导入。
有谁知道这有什么问题吗?
最佳答案
我设法解决了我自己的问题。我需要在最后明确关闭输出流。不这样做会导致数据损坏。
其次,作为额外的奖励,如果我使用 rfc3526 中列出的 Safe Prime(素数模数),它允许我生成不同位大小长度的 El Gamal key 。
关于java - 使用 bouncy caSTLe 生成 dsa/elgamal key 对,在 GPG 中导入时不会出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13733260/
我有一个案例需要使用 OpenPGP 加密一些文件。我正在使用 Bouncy CaSTLe 这样做。 据我了解,Bouncy CaSTLe 加密可以通过两种方式在 Java 中使用: 我将 Bounc
我想创建一个使用 Bouncy CaSTLe(版本 1.59)实现的 signedAndEnvelopedData (PKCS #7) 数据。 在 Bouncy CaSTLe 中,接口(interfa
我发现了一些使用 Bouncy CaSTLe 加密数据的代码,但我找不到任何文档来说明正在使用哪种算法来加密数据或 key 使用了多少位。我也找不到 Bouncy CaSTLe 的论坛。有谁知道它使用
这个问题已经有答案了: bouncycastle + JBoss AS7: JCE cannot authenticate the provider BC (5 个回答) 已关闭10 年前。 我使用的
我的系统中有一个进程将接收随机纯文本或密文输入。由于性能不是问题,我打算尝试解密所有传入的输入,使用如下伪代码: //get the input, either a plain text, or ci
我一直认为使用 ECFieldElement 对象而不是 BigIntegers 对指数执行算术运算更合适,但根据我的测试,这样做会产生不正确的结果。 测试例程(JUnit): class Arith
我正在尝试从 smime.p7s 文件中读取证书,证书链是: Baltimora Cyber Trust --> DigitPA --> Aruba PEC 因此,当我尝试提取时,我只检索到最后两
我正试图找到一种方法来消除移动设备上的 flex 滚动行为(例如,当下面没有内容可滚动时,您仍然可以滚动内容并将内容滚动到顶部,当释放时它会弹跳返回) 我的html结构是这样的
我正在使用来自Codyhouse的这个很棒的弹性过滤器但我一生都无法弄清楚如何让它自动运行,即自行翻转并仍然接受用户点击事件。 jsfiddle ...谢谢。 jQuery(document).rea
考虑正数的以下定义: 如果从左到右它的数字永远不会变小,则该数字是非递减的。例如,12345和 3388 是非递减的。 如果从左到右它的数字从不变大,则该数是非递增的。例如,987542 和881个没
我使用 WPF ScrollViewer 来托管一些控件。我真的很希望它能像在触摸设备上那样进行交互,当你将它拉得太远时,它会慢慢回到原位。 它没有滚动条 - 我使用此代码通过单击和拖动手动滚动鼠标:
我有一个使用 Bouncy CaSTLe 进行 PGP 解密的应用程序,它在过去 8 个月左右的时间里运行没有任何问题,而在过去的 2 天里突然出现了一个问题,其中 GetDataStream 方法抛
我有一个 PEM 或 DER 私钥,一个现有的 key 。如何加载此 key PrivateKeyFactory.createKey or into an AsymmetricCipherKeyPai
我正在查看 Bouncy CaSTLe,看看它的哈希算法的性能与 .NET Framework 的性能相比如何,它看起来不太好; MD5 实现比 .NET 慢 6 倍左右,SHA256 实现比 .NE
我正在寻找 Bouncy CaSTLe PGP“签名和加密”的实现。理想情况下,如果这有什么不同的话,只需一次操作即可。 我已经采取了 encrypt example和 signing example
我正在按照此处的示例:http://www.baeldung.com/java-bouncy-castle 我有几个问题: public static byte[] encryptData(byte[
我正在尝试用 C# 签署比特币交易。我有 2 位代码正在尝试完成。我可以使用 Bouncy caSTLe 创建一组私钥和公钥。我可以将其转换为钱包导入格式。 我还可以从 ECDSA 公钥生成比特币地址
我正在查看 Bouncy CaSTLe,看看它的哈希算法的性能与 .NET Framework 的性能相比如何,它看起来不太好; MD5 实现比 .NET 慢 6 倍左右,SHA256 实现比 .NE
我很确定没有,但我想确认 Bouncy CaSTLe for Java 中的 SCrypt 实现 SCrypt.generate() 是否在结果中包含参数(例如NodeJS 的实现确实如此)。 最佳答
我正在使用 bouncy caSTLe 1.48 通过 OCSP 验证证书验证。效果很好。但我使用 Ocsp Url 作为静态变量,我想从证书中读取它。证书中的 URL 写为Authority Inf
我是一名优秀的程序员,十分优秀!