locked
Encryption using Rijndael AES using 128 bit keys, 16 byte blocks and cipher block chaining RRS feed

  • General discussion

  • Hi,

    I tried a lot of algorithms about Rijndael AES 128 bit keys (16 byte blocks and cipher block chaining),

    I try with this example:

    Text to Encrypt:2~2~000003~0910~20130618220201~T~00000100~USD~F~375019001012120~0~0~00000000000~
    The expected text Encrypted:11 9C 7B 84 AE 3B 36 EC EB FB 27 D7 A6 72 A2 B8 23 E7 80 36 53 95 A5 18 B3 BD 9C F2 63 35 43 0E 72 68 32 03 92 1B 45 1B AA C4 CC 8C 8D B0 D0 D0 30 A8 F1 10 9C BA 50 A7 CC 5E 7C ED 69 AC AC 13 EC 88 15 F2 DD EC E6 A7 5B 8E B3 22 98 B5 15 58

    And, my result is: 5746BC0C68A393285C8C1B9D5123FDAF7D6CFA270273C6F1FA4577F50F8437529C8A900C801AD3EC623150F4D185F10294E8FE7A2E86C454A533464888951C4C8FFFB7B0955626760361A18A21E10ACD

    I use:

    MCRYPT_RIJNDAEL_128
    MCRYPT_MODE_CBC
    ENCRYPTION_KEY = 'abcdef0123456789abcdef0123456789'
    IV = "00000000000000000000000000000000"

    I use PHP encryption,

    I tested during five days and all my results are different,

    I need your help in order to advance my project,

    Thanks

    • Changed type Min Zhu Monday, July 29, 2013 7:20 AM not related to BCL
    Friday, July 19, 2013 1:36 PM

All replies

  • "I use PHP encryption,"

    Hmm, so why are you asking in a .NET forum? Are you trying to match the results in .NET? If that's the case then post the .NET code you are using.

    Saturday, July 20, 2013 5:28 AM
  • Hi Mike,

    Thanks for your replay,

    I know that here is a site for .Net questions but I got a link that show an algorithm in C#, so I think here should have people that can help to me, it is the link:

    http://social.msdn.microsoft.com/Forums/en-US/a8076cd5-ad93-4916-8087-45c605c56835/encryption-using-rijndael-aes-using-128-bit-keys-16-byte-blocks-and-cipher-block-chaining


    I need to encrypt a string using Rijndael AES 128 bit keys, 16 byte blocks and CRC.

    I can encrypt the string BUT my results is not the same, I compare my response with the response of the other company.

    For example:

    * I am trying to encrypt this string,
    2~2~000003~0910~20130618220201~T~00000100~USD~F~375019001012120~0~0~00000000000~

    in order to get this encrypted result:
    11 9C 7B 84 AE 3B 36 EC EB FB 27 D7 A6 72 A2 B8 23 E7 80 36 53 95 A5 18 B3 BD 9C F2 63 35 43 0E 72 68 32 03 92 1B 45 1B AA C4 CC 8C 8D B0 D0 D0 30 A8 F1 10 9C BA 50 A7 CC 5E 7C ED 69 AC AC 13 EC 88 15 F2 DD EC E6 A7 5B 8E B3 22 98 B5 15 58

    But I got:

    5746BC0C68A393285C8C1B9D5123FDAF7D6CFA270273C6F1FA4577F50F8437529C8A900C801AD3EC623150F4D185F10294E8FE7A2E86C454A533464888951C4C8FFFB7B0955626760361A18A21E10ACD

    I used:

    MCRYPT_RIJNDAEL_128

    MCRYPT_MODE_CBC

    ENCRYPTION_KEY = "abcdef0123456789abcdef0123456789"

    iv = "0000000000000000"

    Thanks in advance for your help

    Monday, July 22, 2013 9:09 AM
  • "I know that here is a site for .Net questions but I got a link that show an algorithm in C#, so I think here should have people that can help to me, it is the link:"

    I can help you if you're using C# and the RijndaelManaged class. I have no idea about PHP and its encryption facilities.

    However, I'll do note that you keep saying 128 bit keys but the key you show has 256 bits, maybe that's your problem.

    Monday, July 22, 2013 10:44 AM
  • Hi Make,

    Yes, I change the key to "abcdef0123456789"

    I use also this link:

    http://zenu.wordpress.com/2011/09/21/aes-128bit-cross-platform-java-and-c-encryption-compatibility/


    In php I got:

    AB5CB53DB48E04EA8F2B4736F8FD012C28E25C56EC17FB6D3A8E5784DAA68398294D75071D5B48ABCB7641996A387EB1BB52066EA7B9DD135FD0776218DC7EB6501C28B6B68C7700D335723DD024D17F

    In Java I got:

    AB5CB53DB48E04EA8F2B4736F8FD012C28E25C56EC17FB6D3A8E5784DAA68398294D75071D5B48ABCB7641996A387EB1BB52066EA7B9DD135FD0776218DC7EB6501C28B6B68C7700D335723DD024D17F6C7A3A1A86B8C6E9908FD34056DCF2F0

    They are a little similar except for the last part.

    In both cases, they are different to the excepted response:

    11 9C 7B 84 AE 3B 36 EC EB FB 27 D7 A6 72 A2 B8 23 E7 80 36 53 95 A5 18 B3 BD 9C F2 63 35 43 0E 72 68 32 03 92 1B 45 1B AA C4 CC 8C 8D B0 D0 D0 30 A8 F1 10 9C BA 50 A7 CC 5E 7C ED 69 AC AC 13 EC 88 15 F2 DD EC E6 A7 5B 8E B3 22 98 B5 15 58

    I want to know if you can get the excepted response using .Net code and

    data_to_encrypt = "2~2~000003~0910~20130618220201~T~00000100~USD~F~375019001012120~0~0~00000000000~"
    key128 = "abcdef0123456789"
    iv = "0000000000000000"

    My PHP code is:

    <?php

        $data_to_encrypt = "2~2~000003~0910~20130618220201~T~00000100~USD~F~375019001012120~0~0~00000000000~";
        $key128 = "abcdef0123456789";
        $iv = "0000000000000000";

        echo "data_to_encrypt: ".$data_to_encrypt."<br/>";

        if (($res = mcrypt_generic_init($cipher, $key128, $iv)) != -1)
        {
            // PHP pads with NULL bytes if $cleartext is not a multiple of the block size.
            $cipherText = mcrypt_generic($cipher,$data_to_encrypt);
            mcrypt_generic_deinit($cipher);
            $data_encrypted = strtoupper(bin2hex($cipherText));
            echo $data_encrypted."<br/>";
            echo "len: ".strlen($data_encrypted)."<br/>";
        }

    ?>

    My JAVA code is:

    import java.security.InvalidAlgorithmParameterException;
    import java.security.InvalidKeyException;
    import java.security.NoSuchAlgorithmException;

    import javax.crypto.BadPaddingException;
    import javax.crypto.Cipher;
    import javax.crypto.IllegalBlockSizeException;
    import javax.crypto.NoSuchPaddingException;
    import javax.crypto.spec.IvParameterSpec;
    import javax.crypto.spec.SecretKeySpec;

    public class AES1 {

        private final String cipherTransformation = "AES/CBC/PKCS5Padding";
        private final String aesEncryptionAlgorithm = "AES";

        public byte[] encrypt(byte[] plainText, byte[] key, byte[] initialVector)
                throws NoSuchAlgorithmException, NoSuchPaddingException,
                InvalidKeyException, InvalidAlgorithmParameterException,
                IllegalBlockSizeException, BadPaddingException {
            Cipher cipher = Cipher.getInstance(cipherTransformation);
            SecretKeySpec secretKeySpec = new SecretKeySpec(key,
                    aesEncryptionAlgorithm);
            IvParameterSpec ivParameterSpec = new IvParameterSpec(initialVector);
            cipher.init(Cipher.ENCRYPT_MODE, secretKeySpec, ivParameterSpec);
            plainText = cipher.doFinal(plainText);
            return plainText;
        }

        public static void main(String[] args) throws InvalidKeyException, NoSuchAlgorithmException, NoSuchPaddingException, InvalidAlgorithmParameterException, IllegalBlockSizeException, BadPaddingException {

            byte[] plainText = "2~2~000003~0910~20130618220201~T~00000100~USD~F~375019001012120~0~0~00000000000~".getBytes();
            byte[] key = "abcdef0123456789".getBytes();
            byte[] initialVector="0000000000000000".getBytes();

            AES1 oAES1 = new AES1();

            byte[] cipher = oAES1.encrypt(plainText,key,initialVector);

            String R = "";
            for (int i = 0; i < cipher.length; i++) {
                R += String.format("%02X", cipher[i]) + "";
            }

            System.out.println(R);
        }

    }

    Thanks for your time,

    Monday, July 22, 2013 12:09 PM
  • I changed in Java

    from

    private final String cipherTransformation = "AES/CBC/PKCS5Padding";

    to

    private final String cipherTransformation = "AES/CBC/NoPadding";

    and I got the same response that PHP,

    AB5CB53DB48E04EA8F2B4736F8FD012C28E25C56EC17FB6D3A8E5784DAA68398294D75071D5B48ABCB7641996A387EB1BB52066EA7B9DD135FD0776218DC7EB6501C28B6B68C7700D335723DD024D17F

    Using:

    plainText = "2~2~000003~0910~20130618220201~T~00000100~USD~F~375019001012120~0~0~00000000000~"
    key = "abcdef0123456789"
    initialVector="0000000000000000"


    Monday, July 22, 2013 12:36 PM
  • Well, the default padding in .NET's Rijndael/AES is PKCS7. If I use no padding then I get your PHP response - AB5C ... D17F.

    It's not clear if this is the result you want because you changed the plain text. In any case, if you need more help try asking in a PHP or Java forum.

    Monday, July 22, 2013 1:23 PM
  • Thanks Mike,

    I will continue with my research.

    Thanks again for your time.

    Monday, July 22, 2013 1:31 PM
  • Hi Mike,

    Could you help to me in order to validate the result?

    I must use the Algorithm AES 128 CBC

    KEY = "abcdef0123456789abcdef0123456789"

    IV = "00000000000000000000000000000000"

    The text to encryt is

    2~1~000024~0910~20130723092446~T~00002000~USD~F~375019001012120~0~0~00000000000~

    The result expected is

    0D 58 35 AF EB EE 04 C6 DC 24 21 53 8D B7 C3 8A 12 83 97 0E B3 1F 21 A4 7D 2E 3C C6 23 D2 9E F0 46 12 79 C7 AC F9 3B 03 1B E2 B6 9C E4 5C 93 39 55 49 57 F2 9E F6 09 F0 19 EE C9 75 98 3A 03 B5 37 62 2D 7E 0F 19 6B E1 48 F1 C7 CB B8 8E 60 2A

    The result text's length is 160 characters.

    I tried using many algorithms but all my results are different.

    I want you encrypt the text with your own method and you validate with the expected result.

    Would you paste your results please?

    Thanks in advance for your help and time.

    Tuesday, July 30, 2013 8:18 AM
  • Firstly, I want to thank to all people that send to me one advice,

    Now, I share with you the solution to my question,

    I hope it also helps to anyone,

    <?php function hex2bin($hex_string) { return pack('H*', $hex_string); } $data_to_encrypt = '2~1~000024~0910~20130723092446~T~00002000~USD~F~375019001012120~0~0~00000000000~'; $key = 'abcdef0123456789abcdef0123456789'; $iv = '0000000000000000'; $key = hex2bin($key); $iv = hex2bin($iv); $data_encrypted = bin2hex(mcrypt_encrypt(MCRYPT_RIJNDAEL_128, $key, $data_to_encrypt, MCRYPT_MODE_CBC, $iv)); echo "Data encrypted: ".strtoupper($data_encrypted)."<br/>"; echo "Length: ".strlen($data_encrypted)."<br/>"; ?>

    Data encrypted: 0D5835AFEBEE04C6DC2421538DB7C38A1283970EB31F21A47D2E3CC623D29EF0461279C7ACF93B031BE2B69CE45C9339554957F29EF609F019EEC975983A03B537622D7E0F196BE148F1C7CBB88E602A

    Length: 160

    God bless you

    Wednesday, July 31, 2013 12:00 PM