317991923
发贴: 1
积分: 0
|
于 2004-10-25 22:01
刚装完Bouncy castle 的JCE 运行一个程序,就出现问题,而且问题好像都一样,我估计是不是已经安装了SUN 提供的JCE 地缘故呀,望各位兄台多多指教。 import java.security.*; import java.security.interfaces.*; import javax.crypto.*; import java.io.*;
public class RSAEnAndDecrypt { public RSAEnAndDecrypt() { }
public static byte[] crypt(Cipher cipher, byte[] bytes) throws Exception { CipherInputStream ciIn = new CipherInputStream(new ByteArrayInputStream(bytes), cipher); ByteArrayOutputStream baOut = new ByteArrayOutputStream(); CipherOutputStream ciOut = new CipherOutputStream(baOut, cipher); int c = 0; while ((c = ciIn.read()) >= 0) { ciOut.write; } ciOut.flush(); ciOut.close(); return baOut.toByteArray(); }
public static void main(String[] args) { try { KeyPairGenerator keyPairGen = KeyPairGenerator.getInstance("RSA",
new org.bouncycastle.jce.provider. BouncyCastleProvider()); keyPairGen.initialize(1024); KeyPair keyPair = keyPairGen.genKeyPair(); RSAPublicKey pubKey = (RSAPublicKey) keyPair.getPublic(); RSAPrivateKey priKey = (RSAPrivateKey) keyPair.getPrivate();
Cipher cipher = Cipher.getInstance("RSA", new org.bouncycastle.jce. provider. BouncyCastleProvider()); String s = "1234567890abcdefghijklmnopqrstuvwxyz"; cipher.init(Cipher.ENCRYPT_MODE, priKey); byte[] bytes = crypt(cipher, s.getBytes());
cipher.init(cipher.DECRYPT_MODE, pubKey); bytes = crypt(cipher, bytes); System.out.println(new String(bytes)); } catch (Exception ex) { ex.printStackTrace(); } } } 出现以下异常: Exception in thread "main" java.lang.ExceptionInInitializerError at javax.crypto.Cipher.a(DashoA6275) at javax.crypto.Cipher.getInstance(DashoA6275) at security.RSAEnAndDecrypt.main(RSAEnAndDecrypt.java:41) Caused by: java.lang.SecurityException: Cannot set up certs for trusted CAs at javax.crypto.SunJCE_b.<clinit>(DashoA6275) ... 3 more Caused by: java.security.PrivilegedActionException: java.security.NoSuchAlgorith mException: MD5WITHRSA Signature not available at java.security.AccessController.doPrivileged(Native Method) ... 4 more Caused by: java.security.NoSuchAlgorithmException: MD5WITHRSA Signature not avai lable at java.security.Security.getEngineClassName(Security.java:584) at java.security.Security.getEngineClassName(Security.java:595) at java.security.Security.getImpl(Security.java:1044) at java.security.Signature.getInstance(Signature.java:169) at sun.security.x509.X509CertImpl.verify(X509CertImpl.java:425) at sun.security.x509.X509CertImpl.verify(X509CertImpl.java:383) at javax.crypto.SunJCE_b.c(DashoA6275) at javax.crypto.SunJCE_b.b(DashoA6275) at javax.crypto.SunJCE_s.run(DashoA6275) ... 5 more Press any key to continue...
317991923 edited on 2004-10-25 22:19
|