几天前,我问了一个关于 Jasypt 问题的问题。我引用了一个抛出 EncryptionOperationNotPossibleException 的更大程序。好吧,我仍然无法弄清楚问题所在。这是正在发生的事情:(这提供了对其工作原理的洞察:)
Step 1: Connection
Client Connects
Server Callback: connection(Selection key)
Server Sends Cipher
Client Receives Cipher
Sends encrypted "connection" message "YYPgDOGffgxu6aahZyNSgw=="
Client Receives Encrypted msg "YYPgDOGffgxu6aahZyNSgw=="
Client Throws EncryptionOperationNotPossibleExcepton
at line 45
这真的是紫星。字符编码可能存在问题,但我不确定。服务器和客户端目前在同一台计算机上运行,我很确定我一直在使用 US-ASCII。以下是相关代码:
这是客户端:
public static String CHAR_ENC_B = "US-ASCII";
public static String cipher = null;
public static void main(String[] argv) throws UnknownHostException {
final BasicTextEncryptor textEncryptor = new BasicTextEncryptor();
AbstractBlockingClient client = new AbstractBlockingClient(InetAddress.getByName("127.0.0.1"),4444) {
@Override
protected void messageReceived(ByteBuffer message) {
if (cipher==null) {
cipher=bb2str(message);
textEncryptor.setPassword(cipher);
System.out.println("Cipher(20):"+cipher);}
else {
System.out.println("Raw Message(22):"+bb2str(message));
System.out.println("Decrypted(23):"+textEncryptor.decrypt(bb2str(message)));
String tosend = textEncryptor.encrypt("Test Reply");
try {
this.write(textEncryptor.encrypt("Test Reply").getBytes(CHAR_ENC_B));
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
}
}
@Override
protected void disconnected() {
}
@Override
protected void connected(boolean alreadyConnected) {
}
};
client.run();
}
这是服务器:
public static String CHAR_ENC_B = "US-ASCII";
public static void main(String[] argv) {
//Create the server:
AbstractServer server = new AbstractServer(4444) {
@Override
protected void messageReceived(ByteBuffer message, SelectionKey key) {
System.out.println("Recieved Raw Message(18):"+bb2str(message));
System.out.println("Recieved Decrypted Message(19):"+decrypt_string(getCS(key).ekey,bb2str(message)));
ClientSelector replacement = process_message(decrypt_string(getCS(key).ekey,bb2str(message)),getCS(key));
key.attach(replacement);
}
@Override
protected void connection(SelectionKey key) {
ClientSelector newone = new ClientSelector(key,"","","");
newone.ip = ((SocketChannel)key.channel()).socket().getRemoteSocketAddress().toString();
key.attach(newone);
this.write(key,newone.ekey.getBytes());
String tosend = encrypt_string(newone.ekey,"a");
this.write(key,tosend.getBytes());
System.out.println("Cipher:"+newone.ekey);
System.out.println("Encrypted String Sent(34):"+tosend);
}
@Override
protected void disconnected(SelectionKey key) {
}
@Override
protected void started(boolean alreadyStarted) {
System.out.println("SERVER STARTED");
}
@Override
protected void stopped() {
}
};
server.run();
}
public static String decrypt_string(String key, String msg) {
BasicTextEncryptor textEncryptor = new BasicTextEncryptor();
textEncryptor.setPassword(key);
return textEncryptor.decrypt(msg);
}
public static String encrypt_string(String key, String msg) {
BasicTextEncryptor textEncryptor = new BasicTextEncryptor();
textEncryptor.setPassword(key);
return textEncryptor.encrypt(msg);
}
这些实际上并不是要发送的消息,但它是一个很好的起点。
一些信息:ClientSelector
是一个类,它允许服务器识别它正在与谁交谈(使用用户名、密码、ip 等)并bb2str
转换ByteBuffer
为String
.
任何帮助将不胜感激。我希望这不是像上一个那样愚蠢的错误!谢谢你。
编辑:我添加了 bb2str 的代码:
public static String bb2str(ByteBuffer bytebuff) {
byte[] bytearr = new byte[bytebuff.remaining()];
bytebuff.get(bytearr);
String s = null;
try {s = new String(bytearr,"US-ASCII");}
catch (UnsupportedEncodingException e) {e.printStackTrace();}
return s;
}