0

I have a

String X = 0110100001100101011011000110110001101111530940929e959001f70dd4da5f5cc3b373165781

i first make String X into a byte [] by X.getBytes(); and i go through a RC4 encryption using this..

public static byte[] RC4(byte[] x,byte[] keyBytes)   
{   
    byte[] e = null; 

    try   
    {   
        SecureRandom sr = new SecureRandom(keyBytes);
        KeyGenerator kg = KeyGenerator.getInstance(algorithm);
        kg.init(sr);
        SecretKey sk = kg.generateKey();  
        Cipher enCipher = Cipher.getInstance("RC4");   
        enCipher.init(Cipher.ENCRYPT_MODE,sk);   
        e = enCipher.doFinal(plaintext);              
    }   
    catch(Exception ex)   
    {   
        ex.printStackTrace();   
    }   
    return e;   
} 

EDIT** After the encryption i used cipher = encrpyted.toString(); This returns me a value of cipher = [B@a1c582

after which i try to run a decrypt using RC4, and a toString function to try and get back String X's original value which is listed above, but to no avail..

what i have done.. edit**

//the reason i send using string is due to me having a client server architecture restriction on buffered writer, and this is the receiving side

            message = stdIn.readLine();
            System.out.println("Message received : " + message);

            byte [] kc = key.getBytes();
            byte [] decrypt = message.getBytes();
            byte [] decryptC = EncryptionScheme.decrypt(decrypt, kc);
            X = new String(decryptC);
            System.out.println("String X = " + X);

Message received : [B@a1c582

String X = ����,��

Is there any way to resolve this? i wish to get back the string of String X = 0110100001100101011011000110110001101111530940929e959001f70dd4da5f5cc3b373165781

decryption algorithm

    public static byte[] decrypt(byte[] ciphertext,byte[] keyBytes)   
    {   
        byte de[] = null;   
        try   
        {   
            SecureRandom sr = new SecureRandom(keyBytes);
            KeyGenerator kg = KeyGenerator.getInstance(algorithm);
            kg.init(sr);
            SecretKey sk = kg.generateKey();    
            Cipher deCipher = Cipher.getInstance("RC4");   
            deCipher.init(Cipher.DECRYPT_MODE,sk);   
            de = deCipher.doFinal(ciphertext);   
        }   
        catch(Exception e)   
        {   
            e.printStackTrace();   
        }    
        return de;   

    }  
darren lim
  • 29
  • 4
  • Would changing it to `Arrays.toString(test)` work? – Makoto Aug 02 '15 at 04:22
  • 1
    What about `System.out.println("test = " + new String(test)); ` – David Ehrmann Aug 02 '15 at 04:23
  • 'To no avail' is not a problem description. You're asking about the behaviour of code you haven't posted. `cipher = [B@a1c582` and `test = [B@dcb6b4` are the result of calling `byte[].toString()`, not the contents of the array. – user207421 Aug 02 '15 at 04:30
  • @DavidEhrmann no it does not. same as abv pls read my edits. thanks ! – darren lim Aug 02 '15 at 05:16
  • I am referring to `EncryptionScheme.decrypt()`. Without that source code it is impossible to answer this question. However your problem may be due to sending the ciphertext as a string. `String` is not a container for binary data. You need to base64-encode it, or hex-encode it, etc. It may also be due to sending `[B@a1c582` instead of the array contents. There's just far too much code referenced and implied here that hasn't been posted. You wouldn't accept a problem report from a customer in such a state. – user207421 Aug 02 '15 at 05:16
  • @EJP added! so sorry!! – darren lim Aug 02 '15 at 05:18

2 Answers2

0

In case you cannot find a better solution, you can do following

    StringBuilder s = new StringBuilder();
    for(byte b : arr) {
        s.append(Integer.toBinaryString(0x100 | 0xFF & b).substring(1, 9));
    }
Evgeniy Dorofeev
  • 133,369
  • 30
  • 199
  • 275
0

After the encryption I used cipher = encrpyted.toString()

This is not valid. All this gives you is the result of calling Object.toString(), which is a composite of the object class (byte[], encoded as [B), and its System.identityHashCode(). If that's what you sent over the wire, which is the only conclusion I can draw from your post, you can't possibly decrypt it correctly, as it doesn't contain the ciphertext in the first place.

As String is not a container for binary data, you will have to send a base-64 or base-16 or base-36 encoding of the ciphertext, decode it at the receiver, and then decrypt it.

user207421
  • 305,947
  • 44
  • 307
  • 483
  • can u give me an example? on encoding at sender, and decoding at receiver?.. i dun quite get that part..thanks alot – darren lim Aug 02 '15 at 05:26
  • I suggest you just find yourself a base64-codec class. There's one in JDK 7 I think, certainly 8, and in Apache Commons as well. – user207421 Aug 02 '15 at 05:27
  • thanks i got it to work. but i used the following due to my java being 6/7 byte[] message = "hello world".getBytes("UTF-8"); String encoded = DatatypeConverter.printBase64Binary(message); byte[] decoded = DatatypeConverter.parseBase64Binary(encoded); System.out.println(encoded); System.out.println(new String(decoded, "UTF-8")); – darren lim Aug 02 '15 at 05:53
  • ah. i upvoted. but it doesnt show publicly cuz my rep is low. :D thanks again! – darren lim Aug 02 '15 at 06:31