我想为java制作安全的聊天程序。 我输入了一些消息,但它会产生错误,例如InvalidKeyException。 请解决它。谢谢!
是错误信息!
=== RSA加密=== java.security.InvalidKeyException:没有安装的提供程序支持此密钥:(null)at javax.crypto.Cipher.chooseProvider(Cipher.java:893)at javax.crypto.Cipher.init(Cipher.java:1249)at javax.crypto.Cipher.init(Cipher.java:1186)at Crypto.RSACryption.encryptMessage(RSACryption.java:28)at Chat.Client.sendEncryptMessage(Client.java:236)at Chat.ClientGUI.actionPerformed(ClientGUI.java:191)at javax.swing.JTextField.fireActionPerformed(未知来源)at javax.swing.JTextField.postActionEvent(未知来源)at javax.swing.JTextField $ NotifyAction.actionPerformed(未知来源) 在javax.swing.SwingUtilities.notifyAction(未知来源)at javax.swing.JComponent.processKeyBinding(未知来源)at javax.swing.JComponent.processKeyBindings(未知来源)at javax.swing.JComponent.processKeyEvent(未知来源)at java.awt.Component.processEvent(未知来源)at java.awt.Container.processEvent(未知来源)at java.awt.Component.dispatchEventImpl(未知来源)at java.awt.Container.dispatchEventImpl(未知来源)at java.awt.Component.dispatchEvent(未知来源)at java.awt.KeyboardFocusManager.redispatchEvent(未知来源)at java.awt.DefaultKeyboardFocusManager.dispatchKeyEvent(未知来源) 在java.awt.DefaultKeyboardFocusManager.preDispatchKeyEvent(未知 来源)at java.awt.DefaultKeyboardFocusManager.typeAheadAssertions(未知 来自java.awt.DefaultKeyboardFocusManager.dispatchEvent(未知 来自)java.awt.Component.dispatchEventImpl(未知来源)at java.awt.Container.dispatchEventImpl(未知来源)at java.awt.Window.dispatchEventImpl(未知来源)at java.awt.Component.dispatchEvent(未知来源)at java.awt.EventQueue.dispatchEventImpl(未知来源)at java.awt.EventQueue.access $ 500(未知来源)at java.awt.EventQueue $ 3.run(未知来源)at java.awt.EventQueue $ 3.run(未知来源)at java.security.AccessController.doPrivileged(Native Method)at java.security.ProtectionDomain $ JavaSecurityAccessImpl.doIntersectionPrivilege(未知 来源)at java.security.ProtectionDomain $ JavaSecurityAccessImpl.doIntersectionPrivilege(未知 来自)java.awt.EventQueue $ 4.run(未知来源)at java.awt.EventQueue $ 4.run(未知来源)at java.security.AccessController.doPrivileged(Native Method)at java.security.ProtectionDomain $ JavaSecurityAccessImpl.doIntersectionPrivilege(未知 来自)java.awt.EventQueue.dispatchEvent(未知来源)at java.awt.EventDispatchThread.pumpOneEventForFilters(未知来源) at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source) 在java.awt.EventDispatchThread.pumpEventsForHierarchy(未知 来自java.awt.EventDispatchThread.pumpEvents(未知来源) at java.awt.EventDispatchThread.pumpEvents(Unknown Source)at java.awt.EventDispatchThread.run(未知来源)
这是encryptMessage方法的源代码。
public byte[] encryptMessage(String plainText, PublicKey publicKey) throws NoSuchPaddingException, NoSuchAlgorithmException, InvalidKeyException, BadPaddingException, IllegalBlockSizeException {
System.out.print("\n Plaintext : " + plainText + "\n");
byte[] t0 = plainText.getBytes();
for (byte b : t0) System.out.printf("%02X ", b);
System.out.println("\n Plaintext Length : " + t0.length + " byte");
System.out.println("\n=== RSA Encryption ===");
Cipher cipher = Cipher.getInstance("RSA");
cipher.init(Cipher.ENCRYPT_MODE, publicKey);
byte[] b0 = cipher.doFinal(t0);
System.out.print("\n\n Ciphertext : ");
for (byte b : b0) System.out.printf("%02X ", b);
System.out.println("\n Ciphertext Length : " + b0.length + " byte");
return b0;
}
答案 0 :(得分:0)
Java中的密码学是一个非常复杂的问题。开箱即用,JVM安装了某些“providers”。 java中的Provider定义了加密操作的接口,并且在JVM中有一个list of them available。每个提供程序实现都有不同的支持算法和密钥。当您调用Cipher.getInstance
时,JVM会查看所有已安装的提供程序,并选择一个支持您请求的算法的提供程序。在您的情况下,异常告诉您没有向JVM注册的提供程序支持您正在执行的加密类型。这可能是由于多种原因造成的。
当我想要在java中加密时,我使用BouncyCastle作为提供者。您可以使用其他Cipher.getInstance方法指定充气城堡提供程序,或使用BouncyCastle的帮助程序API,这样您就不必直接使用Cipher
类。查看RSA encrypt/decrypt example here。
作为旁注,如果您打算使用AES 256或更高版本并且正在使用Oracle JDK,则必须安装Unlimited Strength JCE components。
http://docs.oracle.com/javase/7/docs/technotes/guides/security/SunProviders.html