我想爲java創建安全的聊天程序。 我輸入了一些消息,但是卻出現InvalidKeyException等錯誤。 請解決。謝謝!針對java的RSA加密聊天
這是錯誤信息!
=== RSA加密=== java.security.InvalidKeyException:沒有安裝提供支持此項:(空)在 javax.crypto.Cipher.chooseProvider(Cipher.java:893)在 的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(Unknown Source)at javax.swing.JTextField.postActionEvent(Unknown Source)at javax.swing.JTextField中的$ NotifyAction.actionPerformed(來源不明) 在javax.swing.SwingUtilities.notifyAction(來源不明)在 javax.swing.JComponent.processKeyBinding(來源不明)在 javax.swing.JComponent.processKeyBindings( (Unknown Source)at javax.swing.JComponent.processKeyEvent(Unknown Source)at java.awt.Component.processEvent(Unknown Source)at java.awt.Container.processEvent(Unknown Source)at java.awt.Component。 dispatchEventImpl(未知來源)處 java.awt.Component.dispatchEvent(未知來源) java.awt.Container.dispatchEventImpl(未知來源)處 java.awt.KeyboardFocusManager.redispatchEvent(未知來源) 0 java.awt.DefaultKeyboardFocusManager.dispatchKeyEvent(未知來源) 在java.awt.DefaultKeyboardFocusManager.preDispatchKeyEvent(未知 源)在 java.awt.DefaultKeyboardFocusManager.typeAheadAssertions(未知 源)在java.awt.DefaultKeyboardFocusManager.dispatchEvent(未知 源)在java.awt.Component.dispatchEventImpl(未知來源)處 java.awt.Window.dispatchEventImpl(未知來源) java.awt.Container.dispatchEventImpl(未知來源)在 java.awt.Component.dispatchEvent (Unknown Source) java.awt.EventQueue.dispatchEventImpl(Unknown Source)at java.awt.EventQueue.access $ 500(Unknown Source)at java.awt.EventQueue $ 3.r聯合國在 java.security.ProtectionDomain $ JavaSecurityAccessImpl.doIntersectionPrivilege(未知 來源)在 (來源不明)在 java.awt.EventQueue中的$ 3.run(來源不明)在 java.security.AccessController.doPrivileged(本機方法) java.security.ProtectionDomain $ JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)at java.awt.EventQueue $ 4.run(Unknown Source)at java.awt.EventQueue $ 4.run(Unknown Source)at java.security.AccessController.doPrivileged (Native Method)at java.awt.ProtectionDomain $ JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)at java.awt.EventQueue.dispatchEvent(Unknown Source)at java.awt.EventDispat chThread.pumpOneEventForFilters(未知來源)在java.awt.EventDispatchThread.pumpEvents 在java.awt.EventDispatchThread.pumpEventsForFilter(未知來源) 在java.awt.EventDispatchThread.pumpEventsForHierarchy(未知 源)(未知源) 在java的。 awt.EventDispatchThread.pumpEvents(Unknown Source) java.awt.EventDispatchThread。運行(Unknown Source)
這裏是關於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;
}
你應該學習如何調試你的代碼。該錯誤清楚地表明'publicKey'爲空? –