0

I have this WinRT code:

public static string Encrypt(string Login)
    {
        var input = CryptographicBuffer.ConvertStringToBinary(Login, BinaryStringEncoding.Utf8);
        var BinKey = CryptographicBuffer.ConvertStringToBinary(Key, BinaryStringEncoding.Utf8);
        var BinIV = CryptographicBuffer.ConvertStringToBinary(IV, BinaryStringEncoding.Utf8);

        var Encryptor = SymmetricKeyAlgorithmProvider.OpenAlgorithm(SymmetricAlgorithmNames.AesCbcPkcs7);

        var SymmKey = Encryptor.CreateSymmetricKey(BinKey);

        var encrypted = CryptographicEngine.Encrypt(SymmKey, input, BinIV);

        return CryptographicBuffer.EncodeToBase64String(encrypted);
    }

    public static string Decrypt(string LoginToDecode)
    {
        var input = CryptographicBuffer.DecodeFromBase64String(LoginToDecode);
        var BinKey = CryptographicBuffer.ConvertStringToBinary(Key, BinaryStringEncoding.Utf8);
        var BinIV = CryptographicBuffer.ConvertStringToBinary(IV, BinaryStringEncoding.Utf8);

        var Decryptor = SymmetricKeyAlgorithmProvider.OpenAlgorithm(SymmetricAlgorithmNames.AesCbcPkcs7);

        var SymmKey = Decryptor.CreateSymmetricKey(BinKey);

        var Decrypted = CryptographicEngine.Decrypt(SymmKey, input, BinIV);

        return CryptographicBuffer.ConvertBinaryToString(BinaryStringEncoding.Utf8, Decrypted);
    }

and I need this in PHP. I'm trying do this in this code but this isn't the same:

function decryptRJ256($key,$iv,$string_to_decrypt){
$string_to_decrypt = base64_decode($string_to_decrypt);
$rtn = mcrypt_decrypt(MCRYPT_RIJNDAEL_128, $key, $string_to_decrypt, MCRYPT_MODE_CBC, $iv);
$rtn = rtrim($rtn, "\0\4");
return($rtn);}

function encryptRJ256($key,$iv,$string_to_encrypt){ $rtn = mcrypt_encrypt(MCRYPT_RIJNDAEL_128, $key, $string_to_encrypt, MCRYPT_MODE_CBC, $iv); $rtn = base64_encode($rtn); return($rtn);}

For example for this input in c#:

  • Key = "lkirwf897+22#bbt"
  • IV = "741952hheeyy66#c"
  • password = "Asd"

I have "eSy8m8ygN7rtC80DMdGOUQ==". I need this in PHP.

Quiet
  • 115
  • 2
  • 14
  • I don't see how that IV would be accepted by the C# code considering you're using AES which has a block size of 16 bytes, but you're providing an IV of 32 bytes, but it should be 16 bytes long. Also, the IV has to be unpredictable (random) in order to provide semantic security, but it doesn't have to be secret. You can simply prepend it to the ciphertext and slice it off before decryption. – Artjom B. Dec 24 '15 at 10:36
  • 1
    Possible duplicate of [How to add/remove PKCS7 padding from an AES encrypted string?](http://stackoverflow.com/questions/7314901/how-to-add-remove-pkcs7-padding-from-an-aes-encrypted-string) – Artjom B. Dec 24 '15 at 10:37
  • @ArtjomB. I was wrong. I updated question. – Quiet Dec 24 '15 at 10:44
  • @ArtjomB.I wrote php encryption/decryption function from your link. Works fine but it's not still my c# code. I have: $block = mcrypt_get_block_size('rijndael_128', 'cbc'); $pad = $block - (strlen($str) % $block); $str .= str_repeat(chr($pad), $pad); return mcrypt_encrypt(MCRYPT_RIJNDAEL_128, $key, $str, MCRYPT_MODE_CBC, $iv); – Quiet Dec 24 '15 at 11:10
  • I get your expected value when I include the pkcs7pad function and run it. Note that the blocksize is 16. Code: `var_dump(encryptRJ256("lkirwf897+22#bbt", "741952hheeyy66#c", pkcs7pad("Asd", 16)));` – Artjom B. Dec 24 '15 at 12:23
  • @ArtjomB. Yes, it's really works fine :) In decode I have: $str = base64_decode($str); $str = mcrypt_decrypt(MCRYPT_RIJNDAEL_128, $key, $str, MCRYPT_MODE_CBC, $iv); $block = mcrypt_get_block_size('rijndael_128', 'cbc'); $len = strlen($str); $pad = ord($str[$len-1]); return substr($str, 0, strlen($str) - $pad); but it's not working – Quiet Dec 24 '15 at 13:40
  • If I change `rtrim($rtn, "\0\4")` to `pkcs7unpad($rtn, 16)` and call the whole thing like `var_dump(decryptRJ256("lkirwf897+22#bbt", "741952hheeyy66#c", "eSy8m8ygN7rtC80DMdGOUQ=="));` then I get the expected `"Asd"`. – Artjom B. Dec 24 '15 at 15:03
  • @ArtjomB. Yes, it's works fine. Can you add the answer? Maybe it can help someone. – Quiet Dec 25 '15 at 12:17
  • That's what the duplicate link is for, because the padding was the root of your problem. There should be a link above your answer to confirm the duplicate. – Artjom B. Dec 25 '15 at 12:51

0 Answers0