// From http://code.calum.org/ public static byte[] decryptSymmetric(byte[] cryptWhole, PrivateKey privKey){ // System.out.println("decryptSymmetric: cryptWhole is " + cryptWhole.length + " bytes"); // get the first 256 bytes byte[] cryptKey = new byte[256]; System.arraycopy(cryptWhole, 0, cryptKey, 0, 256); //System.out.println("Copied into cryptKey"); // decrypt byte[] skeyBytes = decrypt(cryptKey, privKey); // If we're here, we've successfully decrypted the secret key. //System.out.println("Decrypted the secret key"); // get the rest of the packet int cryptDataSize = cryptWhole.length - 256; // System.out.println("decryptSymmetric: cryptDataSize is " + cryptDataSize + " bytes"); byte[] cryptData = new byte[cryptDataSize]; System.arraycopy(cryptWhole, 256, cryptData, 0, cryptDataSize); //System.out.println("Got cryptData as " + cryptData); byte[] decrypted = null; try { // decrypt data with random key KeyGenerator kgen = KeyGenerator.getInstance(Main.SYMMETRIC); kgen.init(Main.SYMMETRICBITS); SecretKeySpec skeySpec = new SecretKeySpec(skeyBytes, Main.SYMMETRIC); Cipher cipher = Cipher.getInstance(Main.SYMMETRIC); cipher.init(Cipher.DECRYPT_MODE, skeySpec); byte[] decryptedZipped = cipher.doFinal(cryptData); // System.out.println("decryptSymmetric: decrypted size is " + decrypted.length + " bytes"); decrypted = Functions.decompressByteArray(decryptedZipped); return decrypted; } catch (InvalidKeyException ex) { System.err.println(ex.toString()); System.err.println("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n" + "! If you see this, you've got some problems with your crypto stuff\n" + "! You probably need to install the JCE extensions for strong crypto.\n" + "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"); } catch (NoSuchPaddingException ex) { System.err.println(ex.toString()); } catch (NoSuchAlgorithmException ex) { System.err.println(ex.toString()); } catch (IllegalBlockSizeException ex) { System.err.println(ex.toString()); } catch (BadPaddingException ex) { System.err.println(ex.toString()); } return null; } }