几天前,我问了一个有关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()的实现似乎清空了字节缓冲区。所以我只能打一次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为8,则encryptedMessage.length将为this.saltSizeBytes

它是固定的。战争已结束。

10-04 12:51