我一直在寻找一些关于如何使用Bouncy Castle Framework加密标题中的加密的简单字符串的示例代码.
此代码将在Windows Universal项目上运行.我之前尝试使用内置API加密的尝试无法在服务器上解密.
我试过这个:这给了我一个字符串:
4pQUfomwVVsl68oQqWoWYNRmRM + CP + vNFXBNdkN6dZPQ34VZ35vsKn9Q7QGTDVOj + w5mqVYHnGuAOFOgdgl8kA ==
s = String.Format("{0}_{1}", s, DateTime.Now.ToString("ddMMyyyyHmmss"));
SymmetricKeyAlgorithmProvider algorithm = SymmetricKeyAlgorithmProvider.OpenAlgorithm(SymmetricAlgorithmNames.AesCbcPkcs7);
IBuffer keymaterial = CryptographicBuffer.ConvertStringToBinary("[Key]", BinaryStringEncoding.Utf8);
CryptographicKey KEY = algorithm.CreateSymmetricKey(keymaterial);
IBuffer IV = CryptographicBuffer.ConvertStringToBinary("[IV]", BinaryStringEncoding.Utf8);
IBuffer data = CryptographicBuffer.ConvertStringToBinary(s, BinaryStringEncoding.Utf8);
IBuffer output = CryptographicEngine.Encrypt(KEY, data, IV);
return CryptographicBuffer.EncodeToBase64String(output);
Run Code Online (Sandbox Code Playgroud)
服务器使用加密/解密
public static string Encrypt(string text, byte[] key, byte[] iv, int keysize = 128, int blocksize = 128, CipherMode cipher = CipherMode.CBC, PaddingMode padding = PaddingMode.PKCS7)
{
AesCryptoServiceProvider aes = …Run Code Online (Sandbox Code Playgroud) 我在我的应用程序中使用BouncyCastle进行加密.当我独立运行时,一切正常.但是,如果我把它放在webapp中并部署在JBoss服务器上,我会收到以下错误:
javax.servlet.ServletException: error constructing MAC: java.security.NoSuchProviderException: JCE cannot authenticate the provider BC
(...)
root cause
java.lang.Exception: error constructing MAC: java.security.NoSuchProviderException: JCE cannot authenticate the provider BC
(...)
root cause
java.io.IOException: error constructing MAC: java.security.NoSuchProviderException: JCE cannot authenticate the provider BC
org.bouncycastle.jce.provider.JDKPKCS12KeyStore.engineLoad(Unknown Source)
java.security.KeyStore.load(Unknown Source)
Run Code Online (Sandbox Code Playgroud)
以下是导致此错误的代码的一部分:
if (Security.getProvider(BouncyCastleProvider.PROVIDER_NAME) == null)
{
Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider());
}
// Read the Private Key
KeyStore ks = KeyStore.getInstance("PKCS12", BouncyCastleProvider.PROVIDER_NAME);
ks.load(new FileInputStream(certificatePath), privateKeyPassword.toCharArray());
Run Code Online (Sandbox Code Playgroud)
和maven依赖:
<dependency>
<groupId>bouncycastle</groupId>
<artifactId>bcmail-jdk16</artifactId>
<version>140</version>
</dependency>
Run Code Online (Sandbox Code Playgroud)
你知道我怎么能部署它?
更新: Git上提供的部分解决方案
编辑:这个的编译版本可以在https://github.com/makerofthings7/Bitcoin-MessageSignerVerifier上找到
请注意,要验证的邮件必须Bitcoin Signed Message:\n作为前缀. Source1 Source2
在C#实现中有一些错误,我可以从这个Python实现中纠正
实际上提出正确的Base 58地址似乎有问题.
我在下面有以下消息,签名和Base58地址.我打算从签名中提取密钥,散列该密钥,并比较Base58哈希值.
我的问题是:如何从签名中提取密钥?(编辑我在这篇文章的底部发现了c ++代码,需要它在Bouncy Castle /或C#中)
信息
StackOverflow test 123
Run Code Online (Sandbox Code Playgroud)
签名
IB7XjSi9TdBbB3dVUK4+Uzqf2Pqk71XkZ5PUsVUN+2gnb3TaZWJwWW2jt0OjhHc4B++yYYRy1Lg2kl+WaiF+Xsc=
Run Code Online (Sandbox Code Playgroud)
Base58比特币地址"哈希"
1Kb76YK9a4mhrif766m321AMocNvzeQxqV
Run Code Online (Sandbox Code Playgroud)
由于Base58比特币地址只是一个哈希,我不能用它来验证比特币消息.但是,可以从签名中提取公钥.
编辑:我强调我是从签名本身派生公钥,而不是从Base58公钥哈希派生.如果我想(并且我确实想要),我应该能够将这些公钥位转换为Base58哈希.这样做我不需要帮助,我只需要帮助提取公钥位并验证签名.
题
在上面的签名中,此签名的格式是什么?PKCS10?(答案:不,这是专有的,如此处所述)
如何在Bouncy Castle中提取公钥?
验证签名的正确方法是什么?(假设我已经知道如何将公钥位转换为等于上面的比特币哈希的哈希)
之前的研究
此链接描述了如何使用ECDSA曲线,以下代码将允许我将公钥转换为BC对象,但我不确定如何Q从签名中获取点.
在下面的示例中,Q是硬编码值
Org.BouncyCastle.Asn1.X9.X9ECParameters ecp = Org.BouncyCastle.Asn1.Sec.SecNamedCurves.GetByName("secp256k1");
ECDomainParameters params = new ECDomainParameters(ecp.Curve, ecp.G, ecp.N, ecp.H);
ECPublicKeySpec pubKeySpec = new ECPublicKeySpec(
ecp .curve.decodePoint(Hex.decode("045894609CCECF9A92533F630DE713A958E96C97CCB8F5ABB5A688A238DEED6DC2D9D0C94EBFB7D526BA6A61764175B99CB6011E2047F9F067293F57F5")), // Q
params);
PublicKey pubKey = f.generatePublic(pubKeySpec);
var signer …Run Code Online (Sandbox Code Playgroud) 以下代码:
//used Bouncy Castle provider for keyStore
keyStore.setKeyEntry(alias, (Key)keyPair.getPrivate(), pwd, certChain);
Run Code Online (Sandbox Code Playgroud)
certChain持有结束证书和颁发者证书(即两个证书)
的情况下,如果keyStore是一个实例,则不会将发行者证书保存为保存到文件系统密钥库文件中的链的一部分PKCS12.
如果密钥库类型是,它确实保存两个证书PKCS12-3DES-3DES.为什么是这样?PKCS12是否假设两个证书都是链的一部分?
编辑:这是一个SSCCE.这很好用"JKS",失败的原因是"PKCS12":只有链中的第一个证书可以访问getCertificateChain(String).可以打开保存的文件,同时openssl pkcs12显示两个证书.
public void testKeyStore() {
try {
KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("RSA");
keyPairGenerator.initialize(1024);
KeyPair keyPair = keyPairGenerator.generateKeyPair();
PublicKey publicKey = keyPair.getPublic();
PrivateKey privateKey = keyPair.getPrivate();
Certificate[] outChain = { createCertificate("CN=CA", publicKey, privateKey), createCertificate("CN=Client", publicKey, privateKey) };
KeyStore outStore = KeyStore.getInstance("PKCS12");
outStore.load(null, "secret".toCharArray());
outStore.setKeyEntry("mykey", privateKey, "secret".toCharArray(), outChain);
OutputStream outputStream = new FileOutputStream("c:/outstore.pkcs12");
outStore.store(outputStream, …Run Code Online (Sandbox Code Playgroud) 有人知道BouncyCastle的TLS示例吗?我对互联网上缺少它们感到惊讶.如果确实没有,那就让我们收集它们作为答案.
对于JAVA,是否有可靠的PBKDF2-HMAC-SHA256实现?
我以前用bouncycastle加密,但它没有提供PBKDF2WithHmacSHA256'.
我不想自己编写加密模块.
你能推荐任何替代的库或算法(如果我能坚持使用bouncycastle)
(这里是bouncycastle支持算法的算法) http://www.bouncycastle.org/specifications.html
我正在尝试使用iText Java.当您运行示例" 如何签名 "时,会发生以下错误:
Caused by: java.lang.ClassNotFoundException: org.bouncycastle.tsp.TimeStampTokenInfo
Run Code Online (Sandbox Code Playgroud)
根据"iText入门 - 如何使用iText签名PDF",我必须使用BouncyCastle.
我从BouncyCastle下载页面 下载了文件:bcprov-jdk15on-147.jar .
并添加到项目中:Java Build Path/Libraries/Add External JARs ...
我添加了以下行:
Security.addProvider(new BouncyCastleProvider());
Run Code Online (Sandbox Code Playgroud)
运行该示例时,会发生相同的错误.
所以我下载了另一个文件:bcpkix-jdk15on-147.jar,标题为"PKIX/CMS/EAC/PKCS/OCSP/TSP/OPENSSL"
并添加到项目中:Java Build Path/Libraries/Add External JARs ...
现在我有了两个罐子.
运行该示例时,会发生以下错误:
Caused by: java.lang.ClassNotFoundException: org.bouncycastle.asn1.DEREncodable
Run Code Online (Sandbox Code Playgroud)
我尝试下载文件"bcprov-ext-jdk15on-147.jar",但没有解决问题.
我在Windows 7 64位上使用iText 5.2.1和eclipse.
有像PEM证书一样
-----BEGIN RSA PRIVATE KEY-----
Proc-Type: 4,ENCRYPTED
DEK-Info: AES-256-CBC,B9846B5D1803E.....
Run Code Online (Sandbox Code Playgroud)
使用BC 1.46,我使用以下代码提取密钥对:
int myFunc(String pemString, char [] password) {
ByteArrayInputStream tube = new ByteArrayInputStream(pemString.getBytes());
Reader fRd = new BufferedReader(new InputStreamReader(tube));
PEMReader pr = new PEMReader(fRd, new Password (password), "BC");
try {
Object o = pr.readObject();
if (o instanceof KeyPair)
.....
Run Code Online (Sandbox Code Playgroud)
现在我刚刚安装了BC 1.48,他们告诉我PEMReader已被弃用,必须由PEMParser替换.
我的问题是,AFAIK,PEMParser中没有密码的地方.
有人能举例说明如何将我的代码迁移到PEMParser版本吗?
令人惊讶的是,网上关于使用Bouncy Castle的轻量级API的信息非常少.环顾四周后,我能够把一个基本的例子放在一起:
RSAKeyPairGenerator generator = new RSAKeyPairGenerator();
generator.init(new RSAKeyGenerationParameters
(
new BigInteger("10001", 16),//publicExponent
SecureRandom.getInstance("SHA1PRNG"),//prng
1024,//strength
80//certainty
));
AsymmetricCipherKeyPair keyPair = generator.generateKeyPair();
Run Code Online (Sandbox Code Playgroud)
我有RSA的一个基本的了解,并且会在幕后数学,让我明白了什么publicExponent和strength是.我认为只要使用适当的填充,我publicExponent指的是互质phi(pq)并且从我收集的它可以是小的(如3).但是,我不知道是certainty指什么(某些地方提到它可能指的是一个百分比,但我想确定).使用SecureRandom是不言自明的.RSAKeyGenerationParameters的文档完全没有价值(毫不奇怪).我唯一的猜测是它与生成的键的准确性有关,但我想再次确定.所以我的问题是什么是适当的价值certainty和publicExponent?
PS请不要回复"这取决于具体情况 - 您希望信息的安全性".假设最高程度的安全性(即4096位RSA密钥或更高)是非常安全的,除非另有说明......我还希望链接到能够提供使用Bouncy Castle轻量级API的良好示例的链接(我不是所有对JCA实施感兴趣或任何与之相关的例子).
我需要在C#中加密数据,以便将其传递给Java.Java代码属于第三方但我得到了相关的来源,所以我决定,当Java使用Bouncy Castle库时,我将使用C#端口.
解密工作正常.但是,只有当我使用私钥加密而不使用公钥时,解密才有效.使用公钥时,解密失败unknown block type.
显然加密时内部加密RsaEncryptWithPrivate使用公钥,所以我不明白为什么这两种加密方法在功能上不相同:
using Org.BouncyCastle.Crypto;
using Org.BouncyCastle.Crypto.Encodings;
using Org.BouncyCastle.Crypto.Engines;
using Org.BouncyCastle.OpenSsl;
public class EncryptionClass
{
public string RsaEncryptWithPublic(string clearText
, string publicKey)
{
var bytesToEncrypt = Encoding.UTF8.GetBytes(clearText);
var encryptEngine = new Pkcs1Encoding(new RsaEngine());
using (var txtreader = new StringReader(publicKey))
{
var keyParameter = (AsymmetricKeyParameter)new PemReader(txtreader).ReadObject();
encryptEngine.Init(true, keyParameter);
}
var encrypted = Convert.ToBase64String(encryptEngine.ProcessBlock(bytesToEncrypt, 0, bytesToEncrypt.Length));
return encrypted;
}
public string RsaEncryptWithPrivate(string clearText
, string privateKey)
{
var bytesToEncrypt = Encoding.UTF8.GetBytes(clearText);
var encryptEngine = new …Run Code Online (Sandbox Code Playgroud) bouncycastle ×10
java ×7
c# ×3
cryptography ×3
encryption ×3
security ×3
rsa ×2
ssl ×2
certificate ×1
exception ×1
itext ×1
jboss ×1
jce ×1
openssl ×1
pbkdf2 ×1
python ×1
windows-8.1 ×1
x509 ×1