0
I need A Sample Code in Java to 
Encrypt The String in AES 256 (Rijndael) with padding in CBC mode : MCRYPT_RIJNDAEL_256, MCRYPT_MODE_CBC 
Then Encode the result  in base 64.
using Encryption Key:-"f53c50e4ab798944a4debe137ec8ba677dbf44ebd37e373f785bf59a8c048c54".

*i had created this example and it is giving error key invalid but i want to use the above key only

public class aes {
    public static void main(String[] args) throws Exception {
        System.out.println("started");
        System.out.println(encrypt("hello"));
        System.out.println("end");

    }
    public static String encrypt(String value) throws Exception{

byte[] keyBytes="f53c50e4ab798944a4debe137ec8ba677dbf44ebd37e373f785bf59a8c048c54".getBytes();
        SecretKeySpec key = new SecretKeySpec(keyBytes, "AES");
        Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
        cipher.init(Cipher.ENCRYPT_MODE, key);
        byte[] res=cipher.doFinal(value.getBytes());
        String result=Base64.getEncoder().encodeToString(res);
        return result;

    }
}
Ashish
  • 31
  • 2
  • Please post the code you have tried. – Cosmin Staicu Oct 29 '19 at 06:32
  • The Rijndael-variant specified with `MCRYPT_RIJNDAEL_256` and AES-256 are incompatible! Rijndael has a _variable_ blocksize, where `MCRYPT_RIJNDAEL_256` (actually from PHP) denotes the variant with a blocksize of 256 bit, [e.g. here, section II](https://paragonie.com/blog/2015/05/if-you-re-typing-word-mcrypt-into-your-code-you-re-doing-it-wrong). AES is a subset of Rijndael and has a _fixed_ blocksize of 128 bit (AES-256 denotes AES with a _keysize_ of 256 bit), [here](https://stackoverflow.com/a/748645/9014097). So both algorithms have different blocksizes and are therefore incompatible. – Topaco Oct 29 '19 at 08:44
  • Thank you for the Answer. it helped me a lot. bt is it possible to do this ( AES 256 (Rijndael) with padding in CBC mode : MCRYPT_RIJNDAEL_256, MCRYPT_MODE_CBC ) in java? – Ashish Oct 29 '19 at 09:37
  • As I already said, no! Again, AES-256 (blocksize 128 bit) and `MCRYPT_RIJNDAEL_256` (blocksize 256 bit) are _different_, i.e. only one of them, either AES-256 or `MCRYPT_RIJNDAEL_256`, can be used. The counterpart to AES-256 is `MCRYPT_RIJNDAEL_128`. For `MCRYPT_RIJNDAEL_256` there is no counterpart in any of the Sun JCE providers, so 3. party libraries like Bouncy Castle have to be used, [here](https://stackoverflow.com/a/8087178/9014097). – Topaco Oct 29 '19 at 10:02
  • yeah! i tried with Bouncy castle but getting same error that invalid 64 byte key., and when providing 32 byte key it is working.. actually i got a requirement the web service is written in php and in have to create client for that in java, for that i got a key ="f53c50e4ab798944a4debe137ec8ba677dbf44ebd37e373f785bf59a8c048c54". to encrypt... and the requirement in doc is in AES 256 (Rijndael) with padding in CBC mode : MCRYPT_RIJNDAEL_256, MCRYPT_MODE_CBC .. so it will be very helpful for me if you can provide a sample code in java to implement this requirement as i am unable to find it – Ashish Oct 29 '19 at 10:43
  • My recommendation: First, communicate with the PHP-side regarding the algorithm, AES (blocksize 128 bit) or Rijndael (block size 256 bit). Only when this is clear it make sense to start the implementation. Your current problem with the key may be probably caused by your key having a hexadecimal format, but a byte array is required, try this [conversion](https://stackoverflow.com/a/140861/9014097). By the way, if you make changes in the code ( modified algorithm, new provider etc.), you should post this as well. – Topaco Oct 29 '19 at 11:30
  • can you share any link which can give explanation related to Bouncy Castle – Ashish Oct 29 '19 at 12:37
  • The reason you get an error is because the key you generate `keyBytes` is 64 bytes long - that is not a valid key size for AES – David Soroko Oct 29 '19 at 15:56
  • I had already given you [this link](https://stackoverflow.com/a/8087178/9014097). Here you can find a concrete [example](https://stackoverflow.com/q/15804895/9014097). You'll find more on the net. – Topaco Oct 29 '19 at 18:14

0 Answers0