1

i am doing RC4 encryption in Java. However, i am not sure if my secret key output is correct. My codes are shown as below. Do advise, thanks! Refer to my output below.

    BigInteger newformula =  gwithoutspaces.modPow(newx, pwithoutspaces);
    //System.out.println("Formula: " + newformula); 
    String strformula = newformula.toString();
    System.out.println("Formula = " + strformula);

    /* Encryption */
    KeyGenerator keygenerator = KeyGenerator.getInstance(ENCRYPTION_ALGORITHM);
    keygenerator.init(128);     
    SecretKey secretkey = keygenerator.generateKey();
    byte[] plainTextByteArray = strformula.getBytes();

    Cipher cipher = Cipher.getInstance(ENCRYPTION_ALGORITHM);
    cipher.init(Cipher.ENCRYPT_MODE,secretkey);
    byte[] encrypted = cipher.doFinal(plainTextByteArray);
    //out.writeUTF("Ciphertext from Host: " + new String(encrypted)); // send to Client

    System.out.print("Encrypted data: ");
    for (int i =0; i < encrypted.length; i++)
    {
        System.out.print(encrypted[i] + " ");
    }

    System.out.print("\n");

    cipher.init(Cipher.DECRYPT_MODE,secretkey);
    byte[] decrypted = cipher.doFinal(encrypted);
    out.writeUTF("Secret Key from Host: " + secretkey); // send to Client

    System.out.println("Secret Key: " + secretkey);
    System.out.println("Decrypted: " + new String(decrypted));

For the Secret key output, i got something like
Secret Key: javax.crypto.spec.SecretKeySpec@d36805da

May i know if it is correct? Please advise thanks!

Stella
  • 39
  • 1
  • 3

0 Answers0