gpt4 book ai didi

java - Jasypt EncryptionOperationNotPossibleException

转载 作者:太空宇宙 更新时间:2023-11-04 07:35:09 24 4
gpt4 key购买 nike

几天前,我提出了一个有关 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 等),并且 bb2strByteBuffer 转换为 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;
}

最佳答案

我明白了:

事情是这样的... bb2str() 的实现我这里写的好像清空了bytebuffer。所以我只能调用bb2str一次。如果我多次调用它,那么我最终会得到一个空字符串,这会搞砸 jayspt,因为在代码中:

if (this.saltGenerator.includePlainSaltInEncryptionResults()) {
// Check that the received message is bigger than the salt
if (encryptedMessage.length <= this.saltSizeBytes) {
throw new EncryptionOperationNotPossibleException();
}
}

当然还有encryptedMessage.length将是 <= this.saltSizeBytes如果encryptedMessage.length为零且 this.saltSizeBytes是 8。

已修复。 war 结束了。

关于java - Jasypt EncryptionOperationNotPossibleException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16989041/

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