Java 使用BouncyCastle加密字符串时出现的问题
我在尝试使用BouncyCastle加密和解密字符串时遇到问题 下面是一个示例,代码如下所示:Java 使用BouncyCastle加密字符串时出现的问题,java,cryptography,bouncycastle,Java,Cryptography,Bouncycastle,我在尝试使用BouncyCastle加密和解密字符串时遇到问题 下面是一个示例,代码如下所示: public class Cryptotests { public static final String ALGORITHM = "RSA"; /** * @param args the command line arguments */ public static void main(String[] args) { try {
public class Cryptotests {
public static final String ALGORITHM = "RSA";
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
try {
init();
KeyPair kp = generateKey();
byte[] enc = encrypt("The Fat Cat Jumped Over the Bat".getBytes("UTF8"), kp.getPublic());
byte[] dec = decrypt(enc, kp.getPrivate());
} catch (Exception ex) {
Logger.getLogger(Cryptotests.class.getName()).log(Level.SEVERE, null, ex);
}
}
public static void init() {
Security.addProvider(new BouncyCastleProvider());
}
public static KeyPair generateKey() throws NoSuchAlgorithmException {
KeyPairGenerator keyGen = KeyPairGenerator.getInstance(ALGORITHM);
keyGen.initialize(1024);
KeyPair key = keyGen.generateKeyPair();
return key;
}
/**
* Encrypt a text using public key.
*
* @param text The original unencrypted text
* @param key The public key
* @return Encrypted text
* @throws java.lang.Exception
*/
public static byte[] encrypt(byte[] text, PublicKey key) throws Exception {
byte[] cipherText = null;
// get an RSA cipher object and print the provider
Cipher cipher = Cipher.getInstance(
"RSA / ECB / PKCS1Padding");
System.out.println(
"nProvider is:" + cipher.getProvider().getInfo());
// encrypt the plaintext using the public key
cipher.init(Cipher.ENCRYPT_MODE, key);
cipherText = cipher.doFinal(text);
return cipherText;
}
/**
* Decrypt text using private key
*
* @param text The encrypted text
* @param key The private key
* @return The unencrypted text
* @throws java.lang.Exception
*/
public static byte[] decrypt(byte[] text, PrivateKey key) throws Exception {
byte[] dectyptedText = null;
// decrypt the text using the private key
Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding");
cipher.init(Cipher.DECRYPT_MODE, key);
dectyptedText = cipher.doFinal(text);
return dectyptedText;
}
}
当我运行此代码时,最终会出现一个错误:
May 21, 2013 10:20:31 AM cryptotests.Cryptotests main
SEVERE: null
java.security.InvalidKeyException: Illegal key size or default parameters
at javax.crypto.Cipher.checkCryptoPerm(Cipher.java:1011)
at javax.crypto.Cipher.init(Cipher.java:1209)
at javax.crypto.Cipher.init(Cipher.java:1153)
at cryptotests.Cryptotests.encrypt(Cryptotests.java:70)
at cryptotests.Cryptotests.main(Cryptotests.java:34)
我真的是个新手,老实说,当涉及到密码学时,我感到有点迷茫。我的目标是解决这个问题,这样我就可以使用SHA512和4k长度创建和使用RSA密钥对。我很难找到如何实现这一点的清晰示例。需要安装Unlimited Java Cryptography Extension(JCE)Unlimited Strength
很明显:
非法的密钥大小或默认参数
找出什么是合法的。您正在运行什么版本的bouncycastle jar和什么jre。出于兴趣,我毫无例外地使用JRE 1.6和bcprov-jdk16-1.46.jar运行了您的示例-您使用BouncyCastle有什么特殊原因吗?我本以为你可以通过标准的Java提供者来实现这一切。邓肯,老实说,这可能是因为我对加密技术没有经验。我想要实现的是:我得到了一个PKCS8 PEM文本文件中的私钥和一个可以使用该密钥解码的文件,我不知道如何解码该文件,因为我对密码学不够熟悉。老实说,我觉得我越来越迷路了,我读得越多。它还需要是Java