java RC4解密后出现额外字符

hfyxw5xn  于 2023-02-07  发布在  Java
关注(0)|答案(1)|浏览(124)

I am currently creating a UDP program in Java to simulate a secure communication channel. Between the Client and Server, I want to encrypt data before it is sent over to the other side. The problem I am facing now is that there are a lot of weird characters appearing after the decrypted message(example below). Why is this happening and how can I get rid of these characters?
Server-side code:

String testMessage = "this is a test message";

        // initialise the key for RC4 encryption
        Key key = new SecretKeySpec("password".getBytes(StandardCharsets.UTF_8), "RC4");
        System.out.println("key: " + key);

        // initialise cipher for encryption
        Cipher encryptCipher = Cipher.getInstance("RC4");
        encryptCipher.init(Cipher.ENCRYPT_MODE, key);

        // encrypt the message
        byte[] encryptedMessage = encryptCipher.doFinal(testMessage.getBytes(StandardCharsets.UTF_8));

        // array of bytes to store encrypted data to be sent
        byte[] sendData = encryptedMessage;

        // send message
        // byte[] testMessageBytes = testMessage.getBytes(StandardCharsets.UTF_8);
        DatagramPacket sendPacket = new DatagramPacket(sendData, sendData.length, inet, senderPort);
        socket.send(sendPacket);

Client-side code:

// initialise the key for RC4 encryption
    Key key = new SecretKeySpec("password".getBytes(StandardCharsets.UTF_8), "RC4");
    System.out.println("key: " + key);
    
    // Array of bytes to store data received in a packet
    byte[] receiveData = new byte[1024];

    // DatagramPacket to receive data
    DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length);
    // receive data and print it to the console
    socket.receive(receivePacket);
    String receiveMessage = new String(receivePacket.getData(), StandardCharsets.UTF_8);
    System.out.println("Message before decryption: " + receiveMessage);

    // initialise cipher for decryption
    Cipher decryptCipher = Cipher.getInstance("RC4");
    decryptCipher.init(Cipher.DECRYPT_MODE, key);

    // decrypt the received message
    byte[] decryptedMessage = decryptCipher.doFinal(receivePacket.getData());
    receiveMessage = new String(decryptedMessage, StandardCharsets.UTF_8);
    System.out.println("Message after decryption: " + receiveMessage);

This is the result i am getting:
Message before decryption: ??Q}lW?q?↓A??♂?r?(?
Message after decryption: this is a test message&☺rL;?kj?W?♥?q???;g5z$'D??RC?e?gj??P?n[J?|J#K}@?y&??S??R♦?9??X?/m▬????♣J?M??]??|♫???AN ??P?(?♂{^M???q?K?▬s?T♥?Ø????l?R☼☻z ?♂V??}?49?P?????ZTG????/?/???J"@9z???♀??I??9??{??????K?z?♠?.♣ ?????mH →???↓☻???g?5??{?A1\-f*?♥n%??1 V?q=C►?§9♦U???☺??*?"R%?@♦??☺?g? 4???x ??Pht$◄?F►??◄g◄l?P9???▲?>???IG?t???H??t↓??V?bC?:?▲?y@U)g↑g,t?f?U#?r?♠??[7%?e??XR↑??a??Vv??►_?u???#B?☻??FU?¶???????y▲?$??x=44?}??/?cn?↨??+{???↕? 6?:?¶?B?]?)=+8d↨?p\?}3?q∟?▬?k-??o????}?W?M?Z◄¦¿?h??L?9?N?\♥V??☻^~?e?W3J?↑?^↔▲m%?>??P?H♫tCm☼*v☺?}??U']T?a 2?↑?↕???CN???fL?#C???dUCZ_↓?)]???▬DZc?⌂x?⌂??????@? ??wHt????b ?@??(→?KMZ[wx?6? ??b?Y."c??☻?Q?☻é☻??c?-.??☼V9???☺6???ep~?q?↨&???§?? />??c???♥??C36???►K???↨?.??Lr
I am very new to socket programming and cryptography, so pardon any inefficient code. I do know that RC4 is deprecated and should not be used anymore, but this is just for educational purposes. Please let me know if there is any information that I am lacking for this post. I appreciate any and all help.

nbnkbykc

nbnkbykc1#

您需要确保只处理包含 message 的数据包部分,而不是整个缓冲区,因此:

byte[] decryptedMessage = decryptCipher.doFinal(receivePacket.getData(), 0, receivePacket.getLength());

相关问题