如何检查密钥是否正确

时间:2019-04-17 11:19:59

标签: java encryption secret-key rc4-cipher

我正在用Java进行RC4加密。但是,我不确定我的密钥输出是否正确。我的代码如下所示。请指教,谢谢!请参阅下面的我的输出。

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

对于密钥输出,我得到了类似的内容
密钥:javax.crypto.spec.SecretKeySpec@d36805da

我可以知道它是否正确吗?请告知谢谢!

0 个答案:

没有答案