gpt4 book ai didi

java - 在客户端-服务器应用程序中使用 RSA 时为 "UnsupportedEncodingException: SHA"

转载 作者:行者123 更新时间:2023-12-01 09:14:13 25 4
gpt4 key购买 nike

我正在开发一个 Java 即时通讯应用程序。我尝试实现 Demonstrate the use of RSA Public-key system to exchange messages that achieve confidentiality and integrity/authentication 中讨论的 RSA 算法。提供了解决方案,但收到的消息未正确解密。

enter image description here

堆栈跟踪:

java.io.UnsupportedEncodingException: SHA
at java.lang.StringCoding.decode(StringCoding.java:190)
at java.lang.String.<init>(String.java:426)
at java.lang.String.<init>(String.java:491)
at mychatappp.networking.MessageListener.decryptMessage(MessageListener.java:87)
at mychatappp.networking.MessageListener.run(MessageListener.java:115)

服务器端运行方法:

ServerSocket server;
int listenPort = 8877;
WritableGUI gui;
LoginScreen sc = new LoginScreen();

public MessageListener(WritableGUI gui, int port){
this.listenPort = port;
this.gui = gui;
try {
server = new ServerSocket(port);
} catch (IOException ex) {
Logger.getLogger(MessageListener.class.getName()).log(Level.SEVERE, null, ex);
}
}
public void decryptMessage(InputStream inStream) throws IOException, NoSuchAlgorithmException
{
try {

//Create the Data input stream from the socket
DataInputStream dis = new DataInputStream(inStream);

//Get the key
ObjectInputStream in = new ObjectInputStream(new FileInputStream("KeyFile.xx"));

PrivateKey privatekey = (PrivateKey) in.readObject();
System.out.println("Key Used: " + in.toString());
in.close();

//Initiate the cipher
Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding");
cipher.init(Cipher.DECRYPT_MODE,privatekey);

int len = dis.readInt();
byte[] encryptedMsg = new byte[len];
dis.readFully(encryptedMsg);

System.out.println("Server - Msg Length: " + len);
System.out.println("Server - Encrypted: " + asHex(encryptedMsg));

// -Print out the decrypt String to see if it matches the original message.
byte[] plainText = cipher.doFinal(encryptedMsg);
System.out.println("Decrypted Message: " + new String(plainText, "SHA"));


} catch (Exception e) {
e.printStackTrace();
}
}

//Function to make the bytes printable (hex format)
public static String asHex(byte buf[]) {
StringBuilder strbuf = new StringBuilder(buf.length * 2);
int i;
for (i = 0; i < buf.length; i++) {
if (((int) buf[i] & 0xff) < 0x10) {
strbuf.append("0");
}
strbuf.append(Long.toString((int) buf[i] & 0xff, 16));
}
return strbuf.toString();
}

@Override
public void run() {
Socket clientSocket;

try {
while((clientSocket = server.accept()) != null){
InputStream is = clientSocket.getInputStream();
decryptMessage(is);
BufferedReader br = new BufferedReader(new InputStreamReader(is));
String line = br.readLine();
if(line != null){
gui.write(line);
}
}

我哪里出错了?

最佳答案

问题出在这个代码段new String(plainText, "SHA")中。 SHA 不是字符编码。我不知道您的字符串是如何编码的(即字符如何转换为字节,反之亦然),但您可能需要这里的 "UTF-8"

关于java - 在客户端-服务器应用程序中使用 RSA 时为 "UnsupportedEncodingException: SHA",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40693831/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com