Jasypt EncryptionOperationNotPossibleException

时间:2013-06-07 16:38:55

标签: java encryption client-server nio jasypt

几天前,我问了一个与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转换为{{ 1}}。

真的很感激任何帮助。我希望这不是像上一个那样的愚蠢错误! 谢谢。

编辑:我添加了bb2str的代码:

String

1 个答案:

答案 0 :(得分:1)

我明白了:

这就是......我在这里写的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为8,那么encryptedMessage.length当然是this.saltSizeBytes

这是固定的。战争结束了。