Java错误:使用填充密码解密时,输入长度必须是16的倍数
伙计们!我正在尝试制作一个使用AES加密的聊天程序,但遇到了一个问题。当我试着运行程序时,它给出了你可以在标题中看到的错误。我将为您提供这两个类的代码,因为这似乎是完全诊断问题的唯一方法。这方面有很多重复,但似乎没有一个能回答我的具体问题。谢谢你的帮助! 客户: 服务器:Java错误:使用填充密码解密时,输入长度必须是16的倍数,java,aes,Java,Aes,伙计们!我正在尝试制作一个使用AES加密的聊天程序,但遇到了一个问题。当我试着运行程序时,它给出了你可以在标题中看到的错误。我将为您提供这两个类的代码,因为这似乎是完全诊断问题的唯一方法。这方面有很多重复,但似乎没有一个能回答我的具体问题。谢谢你的帮助! 客户: 服务器: package Chat.Application; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamR
package Chat.Application;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.HashSet;
import java.security.GeneralSecurityException;
import java.security.MessageDigest;
import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;
import org.omg.PortableInterceptor.SYSTEM_EXCEPTION;
/**
* A multi-threaded chat room server. When a client connects the
* server requests a screen name by sending the client the
* text "SUBMITNAME", and keeps requesting a name until
* a unique one is received. After a client submits a unique
* name, the server acknowledges with "NAMEACCEPTED". Then
* all messages from that client will be broadcast to all other
* clients that have submitted a unique screen name. The
* broadcast messages are prefixed with "MESSAGE ".
*
* Because this is just a teaching example to illustrate a simple
* chat server, there are a few features that have been left out.
* Two are very useful and belong in production code:
*
* 1. The protocol should be enhanced so that the client can
* send clean disconnect messages to the server.
*
* 2. The server should do some logging.
*/
public class ChatServer {
/**
* The port that the server listens on.
*/
private static final int PORT = 9001;
/**
* The set of all names of clients in the chat room. Maintained
* so that we can check that new clients are not registering name
* already in use.
*/
private static HashSet<String> names = new HashSet<String>();
/**
* The set of all the print writers for all the clients. This
* set is kept so we can easily broadcast messages.
*/
private static HashSet<PrintWriter> writers = new HashSet<PrintWriter>();
/**
* The application main method, which just listens on a port and
* spawns handler threads.
*/
public static void main(String[] args) throws Exception {
System.out.println("Chat Server Activated");
ServerSocket listener = new ServerSocket(PORT);
try {
while (true) {
new Handler(listener.accept()).start();
}
} finally {
listener.close();
}
}
/**
* A handler thread class. Handlers are spawned from the listening
* loop and are responsible for a dealing with a single client
* and broadcasting its messages.
*/
private static class Handler extends Thread {
private String name;
private Socket socket;
private BufferedReader in;
private PrintWriter out;
private Integer length;
/**
* Constructs a handler thread, squirreling away the socket.
* All the interesting work is done in the run method.
*/
public Handler(Socket socket) {
this.socket = socket;
}
/**
* Services this thread's client by repeatedly requesting a
* screen name until a unique one has been submitted, then
* acknowledges the name and registers the output stream for
* the client in a global set, then repeatedly gets inputs and
* broadcasts them.
*/
public void run() {
try {
// Create character streams for the socket.
in = new BufferedReader(new InputStreamReader(
socket.getInputStream()));
out = new PrintWriter(socket.getOutputStream(), true);
// Request a name from this client. Keep requesting until
// a name is submitted that is not already used. Note that
// checking for the existence of a name and adding the name
// must be done while locking the set of names.
while (true) {
out.println("SUBMITNAME");
/*Supposed to change anybody who tries to login as Admin into different name... "Supposed" to....
if (name.equals("Admin")) {
out.println("SUBMITNAME");
return;
}*/
name = in.readLine();
length = name.length();
if (length == 0) {
out.println("SUBMITNAME");
return;
}
if (name == "null") {
out.println("SUBMITNAME");
return;
}
synchronized (names) {
if (!names.contains(name)) {
names.add(name);
break;
}
}
}
// Now that a successful name has been chosen, add the
// socket's print writer to the set of all writers so
// this client can receive broadcast messages.
out.println("NAMEACCEPTED");
//Announces that user is Online
out.println("MESSAGE " + name + " is now Online");
for (PrintWriter writer : writers) {
writer.println("MESSAGE " + name + " is now Online");
}
writers.add(out);
// Accept messages from this client and broadcast them.
// Ignore other clients that cannot be broadcasted to.
while (true) {
String input = in.readLine();
System.out.println(input);
if (input == null) {
return;
}
for (PrintWriter writer : writers) {
writer.println("MESSAGE " + name + ": " + input);
}
}
} catch (IOException e) {
System.out.println(e);
} finally {
// This client is going down! Remove its name and its print
// writer from the sets, and close its socket.
for (PrintWriter writer : writers) {
writer.println("MESSAGE " + name + " is now Offline");
} if (name != null) {
names.remove(name);
}
if (out != null) {
writers.remove(out);
}
try {
socket.close();
} catch (IOException e) {
}
}
}
}
}
套餐聊天应用;
导入java.io.BufferedReader;
导入java.io.IOException;
导入java.io.InputStreamReader;
导入java.io.PrintWriter;
导入java.net.ServerSocket;
导入java.net.Socket;
导入java.util.HashSet;
导入java.security.GeneralSecurityException;
导入java.security.MessageDigest;
导入javax.crypto.Cipher;
导入javax.crypto.spec.SecretKeySpec;
导入org.omg.PortableInterceptor.SYSTEM\u异常;
/**
*多线程聊天室服务器。当客户端连接
*服务器通过向客户端发送
*文本“SUBMITNAME”,并一直请求名称,直到
*收到一个唯一的。在客户端提交唯一的
*名称,服务器用“NAMEACCEPTED”确认。然后
*来自该客户端的所有消息都将广播到所有其他客户端
*已提交唯一屏幕名称的客户端。这个
*广播消息的前缀为“MESSAGE”。
*
*因为这只是一个简单的教学例子
*聊天服务器,有一些功能被遗漏了。
*其中两个非常有用,属于生产代码:
*
* 1. 应该增强协议,以便客户端可以
*向服务器发送干净的断开连接消息。
*
* 2. 服务器应该做一些日志记录。
*/
公共类聊天服务器{
/**
*服务器侦听的端口。
*/
专用静态最终int端口=9001;
/**
*聊天室中所有客户端名称的集合。已维护
*这样我们就可以检查新客户端是否未注册名称
*已经在使用中。
*/
私有静态HashSet name=新HashSet();
/**
*所有客户端的所有打印写入程序集。此
*这台电视机被保留了下来,这样我们就可以轻松地广播信息了。
*/
私有静态HashSet writers=newhashset();
/**
*应用程序main方法,它只侦听端口和
*生成处理程序线程。
*/
公共静态void main(字符串[]args)引发异常{
System.out.println(“聊天服务器激活”);
ServerSocket侦听器=新的ServerSocket(端口);
试一试{
while(true){
新处理程序(listener.accept()).start();
}
}最后{
listener.close();
}
}
/**
*处理程序线程类。处理程序是从侦听线程派生的
*循环并负责与单个客户机进行交易
*并广播其信息。
*/
私有静态类处理程序扩展线程{
私有字符串名称;
专用插座;
中的私有缓冲区读取器;
私人打印输出;
私有整数长度;
/**
*构造一个处理程序线程,存储套接字。
*所有有趣的工作都是在run方法中完成的。
*/
公共处理程序(套接字){
this.socket=socket;
}
/**
*通过重复请求
*屏幕名称,直到提交了唯一的屏幕名称,然后
*确认该名称并为其注册输出流
*客户端在一个全局集合中,然后重复获取输入和
*广播他们。
*/
公开募捐{
试一试{
//为套接字创建字符流。
in=新的BufferedReader(新的InputStreamReader(
getInputStream());
out=新的PrintWriter(socket.getOutputStream(),true);
//从该客户端请求名称。继续请求,直到
//提交的名称尚未使用。请注意
//检查名称是否存在并添加名称
//必须在锁定名称集时执行。
while(true){
out.println(“提交名称”);
/*应该将试图以管理员身份登录的任何人更改为其他名称…“应该”更改为。。。。
if(name.equals(“Admin”)){
out.println(“提交名称”);
返回;
}*/
name=in.readLine();
length=name.length();
如果(长度==0){
out.println(“提交名称”);
返回;
}
如果(名称=“空”){
out.println(“提交名称”);
返回;
}
已同步(名称){
如果(!names.contains(name)){
名称。添加(名称);
打破
}
}
}
//现在已经选择了一个成功的名称,添加
//socket的打印写入程序到所有写入程序的集合,因此
//此客户端可以接收广播消息。
out.println(“接受名称”);
//宣布用户已联机
out.println(“消息“+name+”现在在线”);
for(打印作者:作者){
println(“消息“+name+”现在在线”);
}
作者:加(出);
//接受来自此客户端的消息并广播它们。
//忽略其他无法访问b的客户端
package Chat.Application;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.HashSet;
import java.security.GeneralSecurityException;
import java.security.MessageDigest;
import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;
import org.omg.PortableInterceptor.SYSTEM_EXCEPTION;
/**
* A multi-threaded chat room server. When a client connects the
* server requests a screen name by sending the client the
* text "SUBMITNAME", and keeps requesting a name until
* a unique one is received. After a client submits a unique
* name, the server acknowledges with "NAMEACCEPTED". Then
* all messages from that client will be broadcast to all other
* clients that have submitted a unique screen name. The
* broadcast messages are prefixed with "MESSAGE ".
*
* Because this is just a teaching example to illustrate a simple
* chat server, there are a few features that have been left out.
* Two are very useful and belong in production code:
*
* 1. The protocol should be enhanced so that the client can
* send clean disconnect messages to the server.
*
* 2. The server should do some logging.
*/
public class ChatServer {
/**
* The port that the server listens on.
*/
private static final int PORT = 9001;
/**
* The set of all names of clients in the chat room. Maintained
* so that we can check that new clients are not registering name
* already in use.
*/
private static HashSet<String> names = new HashSet<String>();
/**
* The set of all the print writers for all the clients. This
* set is kept so we can easily broadcast messages.
*/
private static HashSet<PrintWriter> writers = new HashSet<PrintWriter>();
/**
* The application main method, which just listens on a port and
* spawns handler threads.
*/
public static void main(String[] args) throws Exception {
System.out.println("Chat Server Activated");
ServerSocket listener = new ServerSocket(PORT);
try {
while (true) {
new Handler(listener.accept()).start();
}
} finally {
listener.close();
}
}
/**
* A handler thread class. Handlers are spawned from the listening
* loop and are responsible for a dealing with a single client
* and broadcasting its messages.
*/
private static class Handler extends Thread {
private String name;
private Socket socket;
private BufferedReader in;
private PrintWriter out;
private Integer length;
/**
* Constructs a handler thread, squirreling away the socket.
* All the interesting work is done in the run method.
*/
public Handler(Socket socket) {
this.socket = socket;
}
/**
* Services this thread's client by repeatedly requesting a
* screen name until a unique one has been submitted, then
* acknowledges the name and registers the output stream for
* the client in a global set, then repeatedly gets inputs and
* broadcasts them.
*/
public void run() {
try {
// Create character streams for the socket.
in = new BufferedReader(new InputStreamReader(
socket.getInputStream()));
out = new PrintWriter(socket.getOutputStream(), true);
// Request a name from this client. Keep requesting until
// a name is submitted that is not already used. Note that
// checking for the existence of a name and adding the name
// must be done while locking the set of names.
while (true) {
out.println("SUBMITNAME");
/*Supposed to change anybody who tries to login as Admin into different name... "Supposed" to....
if (name.equals("Admin")) {
out.println("SUBMITNAME");
return;
}*/
name = in.readLine();
length = name.length();
if (length == 0) {
out.println("SUBMITNAME");
return;
}
if (name == "null") {
out.println("SUBMITNAME");
return;
}
synchronized (names) {
if (!names.contains(name)) {
names.add(name);
break;
}
}
}
// Now that a successful name has been chosen, add the
// socket's print writer to the set of all writers so
// this client can receive broadcast messages.
out.println("NAMEACCEPTED");
//Announces that user is Online
out.println("MESSAGE " + name + " is now Online");
for (PrintWriter writer : writers) {
writer.println("MESSAGE " + name + " is now Online");
}
writers.add(out);
// Accept messages from this client and broadcast them.
// Ignore other clients that cannot be broadcasted to.
while (true) {
String input = in.readLine();
System.out.println(input);
if (input == null) {
return;
}
for (PrintWriter writer : writers) {
writer.println("MESSAGE " + name + ": " + input);
}
}
} catch (IOException e) {
System.out.println(e);
} finally {
// This client is going down! Remove its name and its print
// writer from the sets, and close its socket.
for (PrintWriter writer : writers) {
writer.println("MESSAGE " + name + " is now Offline");
} if (name != null) {
names.remove(name);
}
if (out != null) {
writers.remove(out);
}
try {
socket.close();
} catch (IOException e) {
}
}
}
}
}