我遇到了一些使用 Bouncy Castle 加密数据的代码,但我找不到任何文档来表明使用哪种算法来加密数据或密钥使用了多少位。我也找不到 Bouncy Castle 的讨论论坛。有谁知道这使用什么算法以及密钥有多少位?
BlowfishEngine blowfishEngine = new BlowfishEngine();
CBCBlockCipher cbcBlockCipher = new CBCBlockCipher(blowfishEngine);
KeyParameter key = new KeyParameter(key);
BufferedBlockCipher cipher = new PaddedBlockCipher(cbcBlockCipher);
cipher.init(true, key);
int size = cipher.getOutputSize(data.length);
byte[] result = new byte[size];
int olen = cipher.processBytes(data, 0, data.length, result, 0);
olen += cipher.doFinal(result, olen);
if (olen < size)
{
byte[] tmp = new byte[olen];
System.arraycopy(result, 0, tmp, 0, olen);
result = tmp;
}
Run Code Online (Sandbox Code Playgroud) 我有一个项目使用两个版本的 bouncyCastle jars bcprov-jdk15 和 bcprov-jdk16。jvm 加载旧版本,但我编写的一个功能需要更新版本才能运行。我尝试使用自定义类加载器来解决这个类路径地狱。经过一番谷歌搜索并在以前的一些 Stackoverflow 答案[1] [2]和本博客的帮助下,我编写了以下Parent Last Class loader ,在委托给父类加载器之前从较新的 jar 中加载类。
public class ParentLastClassLoader extends ClassLoader {
private String jarFile; //Path to the jar file
private Hashtable classes = new Hashtable(); //used to cache already defined classes
public ParentLastClassLoader(ClassLoader parent, String path)
{
super(parent);
this.jarFile = path;
}
@Override
public Class<?> findClass(String name) throws ClassNotFoundException
{
System.out.println("Trying to find");
throw new ClassNotFoundException();
}
@Override
protected synchronized Class<?> loadClass(String className, boolean …Run Code Online (Sandbox Code Playgroud) 我已经在java卡中生成了密钥对并返回公钥。我在 android 应用程序中再次生成公钥,然后在 android 中生成 CSR
private byte[] CertReqGeneration() throws Exception
{
if(publickeyobj==null)
return null;
String info = "CN=cn, OU=ou, O=o, C=cn, ST=city";
X500Principal x500 = new X500Principal(info);
X500Name x500name;
x500name= X500Name.getInstance(x500.getEncoded());
CertificationRequestInfo csrInfo = new CertificationRequestInfo(x500name, publickeyobj, new DERSet());
ASN1EncodableVector v = new ASN1EncodableVector();
v.add(csrInfo);
ASN1ObjectIdentifier oid = new ASN1ObjectIdentifier("0.0");
v.add(new DERSequence(oid));
v.add(new DERBitString(new byte[] {}));
byte[] encoded = new DERSequence(v).getEncoded();
byte[] PKCS10= DataSignGeneration(encoded);
byte[] encoded = Base64.encode(PKCS10);
String base64=new String(encoded);
return base64;
}
Run Code Online (Sandbox Code Playgroud)
但是当我想在 ca 中发布它时,我收到错误 asn1 bad …
我有一种使用 bouncycastle 库从给定 PEM 格式文件中提取 X.509 证书的方法。
进口:
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
import org.bouncycastle.cert.X509CertificateHolder;
import org.bouncycastle.cert.jcajce.JcaX509CertificateConverter;
import org.bouncycastle.openssl.PEMParser;
Run Code Online (Sandbox Code Playgroud)
方法:
/**
* Reads an X509 certificate from a PEM file.
*
* @param certificateFile The PEM file.
* @return the X509 certificate, or null.
* @throws IOException if reading the file fails
* @throws CertificateException if parsing the certificate fails
*/
public static X509Certificate readCertificatePEMFile(File certificateFile) throws IOException, CertificateException {
if …Run Code Online (Sandbox Code Playgroud) 以下代码使用JcaPEMWriterBouncyCastle 中的类以 PKCS#1 格式 ( -----BEGIN RSA PRIVATE KEY-----)输出随机生成的 RSA 私钥:
public static void main(String[] args) throws Exception {
final KeyPairGenerator kpg = KeyPairGenerator.getInstance("RSA");
kpg.initialize(2048, null);
final KeyPair kp = kpg.generateKeyPair();
final PrivateKey privateKey = kp.getPrivate();
final StringWriter s = new StringWriter();
try (JcaPEMWriter w = new JcaPEMWriter(s)) {
w.writeObject(privateKey);
}
System.out.println(s);
}
Run Code Online (Sandbox Code Playgroud)
有没有办法让JcaPEMWriter输出 PKCS#8 格式 ( -----BEGIN PRIVATE KEY-----) 代替?
我是 EC 加密的新手,并且遇到了一些困难。我正在使用 Java 8 和 BouncyCatle 提供程序。我现在的问题是:当我使用以下代码生成 EC-KeyPair 时:
ECGenParameterSpec spec = new ECGenParameterSpec("secp521r1");
KeyPairGenerator kpg = KeyPairGenerator.getInstance("ECDH", BouncyCastleProvider.PROVIDER_NAME);
kpg.initialize(spec, new SecureRandom());
return kpg.generateKeyPair();
Run Code Online (Sandbox Code Playgroud)
并尝试获取公钥的字节数组并将其发送给另一个人,编码后的密钥长度为 158 字节,采用 X.509 格式。但我期望 X9.62 格式和 65 到 66 字节之间的密钥大小。为什么公钥这么大以及如何使用预期的密钥大小对其进行编码?(我预计密钥大小是因为我预计密钥长度为 521 位)
在我的场景中,Alice 和 Bob 就使用哪条曲线达成了一致。
我的问题是 Alice 的公钥实际上是一个点,因此它具有 xy 格式。
我需要将 x,y 坐标字节转换为 ECPublicKey。
这是我正在使用的源代码
// outerPublicKey is the raw bytes from x,y coordinates in hex format
KeyFactory kf = KeyFactory.getInstance("EC");
PublicKey remoteAlicePub = kf.generatePublic(new X509EncodedKeySpec(outerPublicKey));
KeyPairGenerator bobKeyGen = KeyPairGenerator.getInstance("ECDH", "BC");
bobKeyGen.initialize(new ECGenParameterSpec(properties.getCurveName()), new SecureRandom());
KeyPair bobPair = bobKeyGen.generateKeyPair();
ECPublicKey bobPub = (ECPublicKey)bobPair.getPublic();
ECPrivateKey bobPvt = (ECPrivateKey)bobPair.getPrivate();
byte[] bobPubEncoded = bobPub.getEncoded();
byte[] bobPvtEncoded = bobPvt.getEncoded();
KeyAgreement bobKeyAgree = KeyAgreement.getInstance("ECDH");
bobKeyAgree.init(bobPvt);
bobKeyAgree.doPhase(remoteAlicePub, true); …Run Code Online (Sandbox Code Playgroud) 我有同样的情况Decrypt Rijndael 256 Block Size with BouncyCastle
所以我从那篇文章中修复了代码,并替换了我的旧代码
public static string Decrypt(string cipherText, string superSecretPassPhrase)
{
if (cipherText == null)
{
throw new ArgumentNullException(nameof(cipherText));
}
// Get the complete stream of bytes that represent:
// [32 bytes of Salt] + [32 bytes of IV] + [n bytes of CipherText]
var cipherTextBytesWithSaltAndIv = Convert.FromBase64String(cipherText);
// Get the saltbytes by extracting the first 32 bytes from the supplied cipherText bytes.
var saltStringBytes = cipherTextBytesWithSaltAndIv.Take(Keysize / 8).ToArray();
// Get the IV bytes …Run Code Online (Sandbox Code Playgroud) 我正在尝试使用 PGP 实现加密,并且我的加密方法成功地加密了输入字符串,但是当我尝试对其进行解密以验证加密是否正确完成时,该字符串没有被解密。
我尝试了两种方法:
1st approach uses FileOutputStream to write encrypted string & 2nd approach uses ByteArrayOutputStream. FileOutputStream creates a file and I am able to decrypt it using Kleopatra. However my requirement is to just get an encrypted string (not written in a file). So when I try to decrypt the encrypted string (received after using ByteArrayOutputStream) its not working. I tried copying the string and decrypting it through tools>>clipboard in Kleopatra, but the decrypt/verify option is …
我想在 android 中使用 BouncyCastle 进行 GnuPG 加密(想要获取 .gpg 文件)。但我收到此错误。(不支持的类文件主要版本 59。
无法转换 bcprov-jdk15on-1.67.jar (org.bouncycastle:bcprov-jdk15on:1.67))
我的gradle版本是:gradle-6.7.1-bin.zip
JAVA_VERSION="1.8.0_242"
请帮忙。
或者,如果有人可以建议我使用其他方式在 android 中进行 GnuPG 加密,那也将非常有帮助。
bouncycastle ×10
java ×6
encryption ×4
android ×2
c# ×1
certificate ×1
classloader ×1
classpath ×1
cryptography ×1
csr ×1
ecdh ×1
ecdsa ×1
gnupg ×1
javacard ×1
kotlin ×1
openpgp ×1
pem ×1
pkcs#1 ×1
pkcs#8 ×1
public-key ×1
rijndael ×1
secret-key ×1
ssl ×1
x509 ×1