我又调试了一下程序,的确不行。于是我仔细看了一下文档,才发现原来JCE根据不支持RSA加密算法,当然……你试一下DES吧,这个它支持:),在JDK的security包里也有RSA,但只支持用它来签名。没有加密。或是换一个加密包,比如Boucy Castle就行,它支持RSA加密。下面是我的用DES的代码片断:public class hello { public hello() { } public void echo(){ try{ Security.addProvider(new com.sun.crypto.provider.SunJCE());
RSAKey
RSAPublicKey
RSAPrivateKey他们的方法很简单,看看java 文档好不好?
:我的RSA密钥已经产生,我的问题是如何加密的问题。代码如下:
//用key产生Cipher
Cipher cipher=null;
try
{cipher=Cipher.getInstance("RSA");
cipher.init(Cipher.ENCRYPT_MODE,gypublickey);
}catch(NoSuchAlgorithmException yhbe2)
{
System.out.println("no such algorithm");
System.exit(0);
}
catch(InvalidKeyException yhbe)
{
System.out.println("Key is invalid");
System.exit(0);
}
catch(Exception ey3)
{
System.out.println("Error when create the cipher");
System.exit(0);
}返回的错误是:no such algorithm。也就是说,我用RSA的公钥做密钥来对文件加密前,产生CIPHER就出错。
我想知道的是:非对称算法怎样用key产生Cipher?
是真的高手。
请问到那里找JCE的资料?
}
public void echo(){
try{
Security.addProvider(new com.sun.crypto.provider.SunJCE());
KeyGenerator keygen = KeyGenerator.getInstance("DES");
SecretKey desKey = keygen.generateKey(); Cipher desCipher;
desCipher = Cipher.getInstance("DES/ECB/PKCS5Padding");
desCipher.init(Cipher.ENCRYPT_MODE, desKey); byte[] cleartext = "This is just an example".getBytes(); byte[] ciphertext = desCipher.doFinal(cleartext); desCipher.init(Cipher.DECRYPT_MODE, desKey); byte[] cleartext1 = desCipher.doFinal(ciphertext); System.out.println("===="+new String(cleartext1));
}
catch(Exception e){
e.printStackTrace();
} }
public static void main(String[] args) {
hello hello1 = new hello();
hello1.echo();
}
}
我刚查到,JCE中无RSA加密。
我在学校里面,刚才用代理,好象上不去。
我必须用非对称的加密,所以准备用该包了。
要是你有,请给我传一个。
我的邮件[email protected]
另请sharetop到http://www.csdn.net/expert/topic/805/805933.xml?temp=.6998102领分。