Question

A few days ago, I asked a question regarding a problem I had with Jasypt. I referenced a larger program throwing the EncryptionOperationNotPossibleException. Well, I still can't figure out the problem. Here's what's happening: (and this provides insight in to how it works:)

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

This is really purplexing. It's likely that there's a problem with character encodings, but I'm not sure. The server and client are running on the same computer at the moment and I'm pretty sure I'm using US-ASCII throughout. Here is the relevant code:

Here's the client:

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();
}

Here is the server:

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);
}

These aren't actually the messages that are going to be sent, but it is a good place to start.

Some information: ClientSelector is a class that allows the server to identify who it's talking to (with username, password, ip, etc.) and bb2str converts ByteBuffer to String.

Any help would really be appreciated. I hope it's not a stupid mistake like the last one! Thank you.

EDIT: I've added the code for 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;
}
Was it helpful?

Solution

I figured it out:

Here's the thing... the implementation of bb2str() I wrote here seems to empty the bytebuffer. So I can only call bb2str once. If I call it more than once then I'll end up with an empty string and that screws up jayspt because in the code:

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

And of course encryptedMessage.length is going to be <= this.saltSizeBytes if encryptedMessage.length is zero and this.saltSizeBytes is 8.

It's fixed. The war is over.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top