bouncycastle java - 代码(original code)
public class EncryptAndDecrypt {
private static final String WORK_DIR = "..\\dir\\";
private static final File SOURCE_PDF = new File(WORK_DIR, "input.txt");
private static final File DESTINATION_FILE = new File(WORK_DIR, "out.txt");
private static final File DECRYPTED_FILE = new File(WORK_DIR, "decrypted.pdf");
public static void main(final String[] args) throws Exception {
if (!new File(WORK_DIR).exists()) {
throw new RuntimeException("Update WORK_DIR to point to the directory the project is cloned into.");
}
Files.deleteIfExists(DESTINATION_FILE.toPath());
Files.deleteIfExists(DECRYPTED_FILE.toPath());
Security.addProvider(new BouncyCastleProvider());
X509Certificate certificate = getX509Certificate(new File(WORK_DIR, "cert.cer"));
encrypt(certificate, SOURCE_PDF, DESTINATION_FILE);
}
private static void encrypt(X509Certificate cert, File source, File destination) throws CertificateEncodingException, CMSException, IOException {
CMSEnvelopedDataStreamGenerator gen = new CMSEnvelopedDataStreamGenerator();
gen.addRecipientInfoGenerator(new JceKeyTransRecipientInfoGenerator(cert));
OutputEncryptor encryptor = new JceCMSContentEncryptorBuilder(CMSAlgorithm.SHA256)
.setProvider(BouncyCastleProvider.PROVIDER_NAME)
.build();
try (FileOutputStream fileStream = new FileOutputStream(destination);
OutputStream encryptingStream = gen.open(fileStream, encryptor)) {
byte[] unencryptedContent = Files.readAllBytes(source.toPath());
encryptingStream.write(unencryptedContent);
}
System.out.println(String.format("Encrypted '%s' to '%s'", source.getAbsolutePath(), destination.getAbsolutePath()));
}
private static X509Certificate getX509Certificate(File certificate) throws IOException, CertificateException {
try (InputStream inStream = new FileInputStream(certificate)) {
CertificateFactory cf = CertificateFactory.getInstance("X.509");
return (X509Certificate) cf.generateCertificate(inStream);
}
}
}
此代码返回
上的异常OutputEncryptor encryptor = new JceCMSContentEncryptorBuilder(CMSAlgorithm.SHA256)
.setProvider(BouncyCastleProvider.PROVIDER_NAME)
.build();
- >
Exception in thread "main" org.bouncycastle.cms.CMSException: cannot create key generator: no such algorithm: 2.16.840.1.101.3.4.2.3 for provider BC
at org.bouncycastle.cms.jcajce.EnvelopedDataHelper.createKeyGenerator(Unknown Source)
at org.bouncycastle.cms.jcajce.JceCMSContentEncryptorBuilder$CMSOutputEncryptor.<init>(Unknown Source)
at org.bouncycastle.cms.jcajce.JceCMSContentEncryptorBuilder.build(Unknown Source)
at EncryptAndDecrypt.encrypt(EncryptAndDecrypt.java:66)
at EncryptAndDecrypt.main(EncryptAndDecrypt.java:42)
Caused by: java.security.NoSuchAlgorithmException: no such algorithm: 2.16.840.1.101.3.4.2.3 for provider BC
at sun.security.jca.GetInstance.getService(GetInstance.java:87)
at javax.crypto.JceSecurity.getInstance(JceSecurity.java:97)
at javax.crypto.KeyGenerator.getInstance(KeyGenerator.java:265)
at org.bouncycastle.jcajce.util.NamedJcaJceHelper.createKeyGenerator(Unknown Source)
... 5 more
和
码
public class Main {
public static void main(String[] args) throws Exception {
PublicKey publicKey = getCert().getPublicKey();
byte[] message = "Hello World".getBytes("UTF8");
Cipher cipher = Cipher.getInstance(JCP.GOST_EL_DEGREE_NAME);
cipher.init(Cipher.ENCRYPT_MODE, publicKey);
byte[] encrypted = cipher.doFinal(message);
}
public static Certificate getCert() {
KeyStore trustStore = null;
try {
trustStore = KeyStore.getInstance(KeyStore.getDefaultType());
trustStore.load(null);//Make an empty store
InputStream fis = new FileInputStream("cert.cer");
BufferedInputStream bis = new BufferedInputStream(fis);
CertificateFactory cf = CertificateFactory.getInstance("X.509");
while (bis.available() > 0) {
Certificate cert = cf.generateCertificate(bis);
trustStore.setCertificateEntry("fiddler" + bis.available(), cert);
}
return trustStore.getCertificate("fiddler" + bis.available());
//System.out.println(test);
//System.out.println(test.getPublicKey());
} catch (Exception ignored) {
}
throw new RuntimeException();
}
}
此代码返回异常:
线程中的异常“main”java.security.NoSuchAlgorithmException:找不到任何支持GOST3410EL的提供程序
和代码
Cipher encryptCipher = Cipher.getInstance("RSA");
encryptCipher.init(Cipher.ENCRYPT_MODE, Encrypt.getCert());
byte[] message = "Hello World".getBytes("UTF8");
byte[] encrypted = encryptCipher.doFinal(message);
System.out.println(Arrays.toString(encrypted));
此代码返回异常:
线程“main”中的异常java.security.InvalidKeyException:没有安装的提供程序支持此密钥:ru.CryptoPro.JCP.Key.GostPublicKey