使用充气城堡在 Java 中加密 xml 文件的示例
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2052213/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
An example of encrypting an xml file in Java using bouncy castle
提问by Lee Warner
Can anyone show me (or provide a link to) an example of how to encrypt a file in Java using bouncy castle? I've looked over bouncycastle.org but cannot find any documentation of their API. Even just knowing which classes to use would be a big help for me to get started!
任何人都可以向我展示(或提供指向)如何使用充气城堡在 Java 中加密文件的示例?我查看了 bouncycastle.org,但找不到他们 API 的任何文档。即使只是知道要使用哪些类对我开始使用也会有很大帮助!
采纳答案by Kevin
What type of encryption do you want to perform? Password-based (PBE), symmetric, asymmetric? Its all in how you configure the Cipher.
您要执行哪种类型的加密?基于密码 (PBE)、对称、非对称?这完全取决于您如何配置Cipher。
You shouldn't have to use any BouncyCastle specific APIs, just the algorithms it provides. Here is an example that uses the BouncyCastle PBE cipher to encrypt a String:
您不必使用任何 BouncyCastle 特定的 API,只需使用它提供的算法即可。这是一个使用 BouncyCastle PBE 密码加密字符串的示例:
import java.security.SecureRandom;
import java.security.Security;
import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.PBEKeySpec;
import org.bouncycastle.jce.provider.BouncyCastleProvider;
public class PBE {
private static final String salt = "A long, but constant phrase that will be used each time as the salt.";
private static final int iterations = 2000;
private static final int keyLength = 256;
private static final SecureRandom random = new SecureRandom();
public static void main(String [] args) throws Exception {
Security.insertProviderAt(new BouncyCastleProvider(), 1);
String passphrase = "The quick brown fox jumped over the lazy brown dog";
String plaintext = "hello world";
byte [] ciphertext = encrypt(passphrase, plaintext);
String recoveredPlaintext = decrypt(passphrase, ciphertext);
System.out.println(recoveredPlaintext);
}
private static byte [] encrypt(String passphrase, String plaintext) throws Exception {
SecretKey key = generateKey(passphrase);
Cipher cipher = Cipher.getInstance("AES/CTR/NOPADDING");
cipher.init(Cipher.ENCRYPT_MODE, key, generateIV(cipher), random);
return cipher.doFinal(plaintext.getBytes());
}
private static String decrypt(String passphrase, byte [] ciphertext) throws Exception {
SecretKey key = generateKey(passphrase);
Cipher cipher = Cipher.getInstance("AES/CTR/NOPADDING");
cipher.init(Cipher.DECRYPT_MODE, key, generateIV(cipher), random);
return new String(cipher.doFinal(ciphertext));
}
private static SecretKey generateKey(String passphrase) throws Exception {
PBEKeySpec keySpec = new PBEKeySpec(passphrase.toCharArray(), salt.getBytes(), iterations, keyLength);
SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("PBEWITHSHA256AND256BITAES-CBC-BC");
return keyFactory.generateSecret(keySpec);
}
private static IvParameterSpec generateIV(Cipher cipher) throws Exception {
byte [] ivBytes = new byte[cipher.getBlockSize()];
random.nextBytes(ivBytes);
return new IvParameterSpec(ivBytes);
}
}
回答by Edward Q. Bridges
While it's an indirect answer to your question, perhaps you'll find it useful to use jasypt to handle the encryption.
虽然这是对您问题的间接回答,但也许您会发现使用 jasypt 处理加密很有用。
here's an example of how to encrypt a file using jasypt: http://www.jasypt.org/encrypting-configuration.html
以下是如何使用 jasypt 加密文件的示例:http: //www.jasypt.org/encrypting-configuration.html
And, here's how to configure bouncy castle as a provider for jasypt: http://www.jasypt.org/bouncy-castle.html
而且,这里是如何将充气城堡配置为 jasypt 的提供者:http: //www.jasypt.org/bouncy-castle.html
回答by Robert Christie
You can view the java doc at http://bouncycastle.org/docs/docs1.6/index.html
您可以在http://bouncycastle.org/docs/docs1.6/index.html查看 java 文档
You can download examples from this page: http://eu.wiley.com/WileyCDA/WileyTitle/productCd-0764596330,descCd-DOWNLOAD.html
您可以从此页面下载示例:http: //eu.wiley.com/WileyCDA/WileyTitle/productCd-0764596330,descCd-DOWNLOAD.html
回答by jarnbjo
回答by ChatC
The best place to find Bouncy Castle java code examples is to go through the test cases in the test suite of bouncy castle Bouncy Castle latest release java
找到 Bouncy Castle java 代码示例的最佳位置是通过 bouncy Castle Bouncy Castle 最新版本 java的测试套件中的测试用例
These test suites contain non-deprecated code which can be used readily
这些测试套件包含可以轻松使用的非弃用代码