我有以下需要转换为 c# 的 Java 代码:
public static byte[] encryptAES(byte[] toEncrypt, byte[] key,
boolean encrypte) throws Exception {
Security.addProvider(new BouncyCastleProvider());
byte[] iv = { (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00,
(byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00,
(byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00,
(byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00 };
IvParameterSpec salt = new IvParameterSpec(iv);
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding", "BC");
if (encrypte == false)
cipher.init(Cipher.DECRYPT_MODE, new SecretKeySpec(key, "AES"), salt);
else
cipher.init(Cipher.ENCRYPT_MODE, new SecretKeySpec(key, "AES"), …Run Code Online (Sandbox Code Playgroud) 我正在编写一个 java 程序来从文件系统中的文件中导入私钥并使用 java 创建一个私钥对象...我可以对.pem格式的文件执行此操作,但是使用 .der 格式,我不知道该做什么做,因为我无法首先检测用于生成密钥的算法。在.pem文件中,我可以从头文件中确定算法,PKCS#1其具有类似
-----BEGIN RSA PRIVATE KEY----
格式的头文件,并使用 bouncycastlepem阅读器用于 PKCS#8 中的那些有头文件
-----BEGIN PRIVATE KEY-----
但 .der 格式的文件不知道 :(
如果有人知道.key格式告诉我
thanx
看来,虽然PGPPublicKey该类提供了一种isEncryptionKey()方法来确定公钥的算法是否可以用于加密目的(RSA_GENERAL, RSA_ENCRYPT, ELGAMAL_GENERAL, ELGAMAL_ENCRYPT),但仅凭这一点还不足以选择有效的加密子密钥。
有关于存储在数据包中的公钥的预期用途的信息,如 GnuPG packet.h 中所示:
41 /* Usage flags */
42 #define PUBKEY_USAGE_SIG GCRY_PK_USAGE_SIGN /* Good for signatures. */
43 #define PUBKEY_USAGE_ENC GCRY_PK_USAGE_ENCR /* Good for encryption. */
44 #define PUBKEY_USAGE_CERT GCRY_PK_USAGE_CERT /* Also good to certify keys.*/
45 #define PUBKEY_USAGE_AUTH GCRY_PK_USAGE_AUTH /* Good for authentication. */
46 #define PUBKEY_USAGE_UNKNOWN GCRY_PK_USAGE_UNKN /* Unknown usage flag. */
47 #define PUBKEY_USAGE_NONE 256 /* No usage given. */
Run Code Online (Sandbox Code Playgroud)
我的问题是,鉴于 Bouncy Castle …
我目前正在玩 Java 和散列。
我在网上环顾四周时遇到了bouncycastle,我将它安装在我的 IDE 和所有东西中。
但是,如何使用此处找到的算法使用它来散列文本?(查找第 5.2 节算法)
我只想使用本节中定义的摘要算法:
GOST3411-MD2-MD4-MD5-RipeMD128-RipeMD160-RipeMD256-RipeMD320-SHA1-SHA-224-SHA-256-SHA-384-SHA-512-SHA3-224-SHA3-2586-SHA3-SHA2 Skein-256-* - Skein-512-* - Skein-1024-* - Tiger - Whirlpool
我正在使用Bouncycastle来管理我的项目的加密功能。我设法使用CMS进行加密和解密,其中两个密钥都存储在我的文件系统(a.cert和 a .p12)中。
这是我实际使用的两个函数:
private static byte[] CmsEncrypt(byte[] message)
{
var envelopGenerator = new CmsEnvelopedDataGenerator();
var certificateStream = new FileStream("Test.cer", FileMode.Open, FileAccess.Read);
var cert = new X509CertificateParser().ReadCertificate(certificateStream);
envelopGenerator.AddKeyTransRecipient(cert);
return
envelopGenerator.Generate(new CmsProcessableByteArray(message), CmsEnvelopedGenerator.DesEde3Cbc)
.GetEncoded();
}
private static byte[] CmsDecrypt(byte[] encrypted, AsymmetricKeyParameter key, X509Certificate cert)
{
return new CmsEnvelopedData(encrypted).GetRecipientInfos().GetFirstRecipient(new RecipientID()
{
SerialNumber = cert.SerialNumber,
Issuer = cert.IssuerDN
}).GetContent(key);
}
Run Code Online (Sandbox Code Playgroud)
现在我必须向前迈出一步,私钥必须在智能卡上,但我真的不知道在这种情况下使用CMS。
我可以初始化卡并解密一条简单的消息(使用标准pkcs11,我找到了一个很好的 c# 包装器),但我找不到任何关于如何使用智能卡进行CMS解密的线索。
我正在尝试使用曲线 25519 的 /java bouncy castle 1.52 实现生成密钥对,这给了我什么
java.lang.IllegalArgumentException: 字符串 curve25519 不是 OID
这是我的代码:
public KeyPair generateKeys() throws NoSuchAlgorithmException,
NoSuchProviderException, InvalidAlgorithmParameterException {
ECParameterSpec ecSpec = ECNamedCurveTable.getParameterSpec("curve25519");
KeyPairGenerator g = KeyPairGenerator.getInstance("ECDSA", "BC");
g.initialize(ecSpec, new SecureRandom());
return g.generateKeyPair();
}
Run Code Online (Sandbox Code Playgroud)
此代码的结果是下面的堆栈跟踪:
java.lang.IllegalArgumentException:字符串curve25519 不是org.bouncycastle.asn1.ASN1ObjectIdentifier.(Unknown Source) at org.bouncycastle.jcajce.provider.asymmetric.ec.BCECPublicKey.getEncoded(Unknown Source) at org.bouncycastle的OID .provider.asymmetric.ec.BCECPrivateKey.getPublicKeyDetails(Unknown Source) at org.bouncycastle.jcajce.provider.asymmetric.ec.BCECPrivateKey.(Unknown Source) at org.bouncycastle.jcajce.provider.asymmetric.ec.KeyPairGeneratorSpi$EC. generateKeyPair(Unknown Source) at com.poc.databank.encryption.BouncyCastleEncryption.generateKeys(BouncyCastleEncryption.java:22) at com.poc.databank.encryption.BouncyCastleTest.testApp(BouncyCastleTest.java:16) at sun.reflect.NativeMethodAccessorImpl. invoke0(Native Method) 在 sun.reflect.NativeMethodAccessorImpl。invoke(NativeMethodAccessorImpl.java:57) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:606) at org.junit.runners.model.FrameworkMethod $1.runReflectiveCall(FrameworkMethod.java:47) at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12) at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:44)在 org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17) 在 org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:271) 在 …
安全对象有很多格式。有时您需要 X509Certificate,有时您需要它作为 PEM 编码的字符串。您如何从 X509Certificate 格式转换为 PEM?
制作这个(X509Certificate):
[0] Version: 3
SerialNumber: 95573
IssuerDN: C=US,ST=California,OU=PDX,O=Example Inc.,CN=Example Cust Issuing CA 1
Start Date: Wed Jan 13 14:21:12 PST 2016
Final Date: Sat Jan 14 14:21:12 PST 2017
SubjectDN: C=US,ST=California,OU=TEST,O=Example,CN=vm1452810069963
Public Key: RSA Public Key
modulus: 9c2b98b154cbd2bdaed82271e2324e73589356cab9a762b8ba7248fab236347eb44d19322696109e
[...]
c0868c88e5e7bc09baadb48cf85c631d
public exponent: 10001
Signature Algorithm: SHA256WITHRSA
Signature: 2197491b50f69c317c7b930634d487744f4502cc
[...]
dfcb0a75ba67f94b958d2edc2c6cea9a
Extensions:
critical(false) 2.5.29.35 value = Sequence
Tagged [0] IMPLICIT
DER Octet String[32]
Tagged [1]
Tagged [4]
DER Sequence
DER Set
DER Sequence
ObjectIdentifier(2.5.4.6) …Run Code Online (Sandbox Code Playgroud) 参考How to use TLS 1.2 in Java 6 中提供的解决方案,是否可以将 TSLSocketConnectionFactory 与 Apache HttpClient4.4 一起使用。
问候, j
我的问题很简单:我需要在Fp使用Java时添加两点.一旦java api缺少一些ecc utils我就会使用bouncycastle.
以下是使用的公式:
P + Q = -R
? = (yq - yp)/(xq-xp)
?r = -yp + ?(xp - xr)
xr = ?^2 - xp - xq
Run Code Online (Sandbox Code Playgroud)
并在java中快速实现上面的公式:
String newline = System.lineSeparator();
BigInteger yp = new BigInteger("4018974056539037503335449422937059775635739389905545080690979365213431566280");
BigInteger yq = new BigInteger("17614944419213781543809391949654080031942662045363639260709847859438286763994");
BigInteger xp = new BigInteger("2");
BigInteger xq = new BigInteger("57520216126176808443631405023338071176630104906313632182896741342206604859403");
BigInteger p = new BigInteger("57896044618658097711785492504343953926634992332820282019728792003956564821041");
BigInteger a_ = new BigInteger("7");
BigInteger b_ = new BigInteger("43308876546767276905765904595650931995942111794451039583252968842033849580414");
ECFieldElement x1 = new ECFieldElement.Fp(p, xp);
ECFieldElement y1 = new …Run Code Online (Sandbox Code Playgroud) 我走了很长一段路让java签署CSR,最后我能够做到这一点,但openssl告诉它无效。使用 openssl 签名的相同 CSR 通过验证步骤。
都是一样的 x509 版本 (1),没有扩展,Subject、Issuer 都是一样的。
我怀疑问题出在主题 DN(尤其是电子邮件)或日期上。
确认:
openssl verify -verbose -CAfile src/test/resources/ca.cer.pem o.cer.pem
o.cer.pem: OK
openssl verify -verbose -CAfile src/test/resources/ca.cer.pem client.cer.pem
client.cer.pem: C = RU, ST = Moscow, L = Moscow, O = Hoofs, OU = IT, CN = Danee Yaitskov
error 20 at 0 depth lookup:unable to get local issuer certificate
Run Code Online (Sandbox Code Playgroud)
文件大小相似:
1229 Jul 28 12:45 client.cer.pem 1233 Jul 28 13:00 o.cer.pem
它抱怨链中缺少证书,但我没有看到此类信息。
如何检查下一个父证书是什么?
好证书的相关资料:
openssl x509 -in o.cer.pem -text -noout
Certificate:
Data: …Run Code Online (Sandbox Code Playgroud)