0

I'm facing a problem with converting the following string the result of php is different from c#

please note that the text and $text = "Salted__12345678 �V�*>�����ܛ��(\˖��\�Zf�`��ȯE��I�W��0�Җ0���z������"

now in c#

byte[] Opencipher = **** ;
string utfString = Encoding.UTF8.GetString(Opencipher, 0, Opencipher.Length);
string text = "Salted__12345678" + utfString // here it match the $text in php
byte[] plainTextBytes = System.Text.Encoding.UTF8.GetBytes(text);
return System.Convert.ToBase64String(plainTextBytes);

the result is

U2FsdGVkX18xMjM0NTY3OA3vv70LVg/vv71/Kj7vv73vv73vv73vv73vv73cm++/ve+/vShcy5YH77+977+9XO+/vVpmEe+/vQUcYBXvv73vv73Ir0Xvv73vv71J77+9V++/vQPvv70w77+90pYw77+977+977+9egLvv73vv73vv73vv73vv73vv70=

when I use PHP

// $ct is a byte array or salt key
$text = "Salted__" . $ct // here it match the text in c#
base64_encode($text);

the result is

U2FsdGVkX18xMjM0NTY3OA2rC1YP+38qPqenpI/d3Ju9xChcy5YHotxcjlpmEbUFHGAV6PXIr0WEi0ndV9gDvTCb0pYwte7WegK4i+L1neE=

but when I'm using c#

I tried all the Encoding types (UTF8, UTF7, Default ... etc) but it doesn't work I think the problem here that PHP accept concatenate between string and Byte[] and c# encoding is different from PHP when there are spacial carachters
the PHP code is correct but I need to get the same results from c# code

Thank you

Iyad Bacdounes
  • 75
  • 1
  • 12
  • 1
    The point is that once you've got that string, you've lost the values of some bits and bytes, because not every byte can be represented as a character. It's probably not really a string. https://www.base64encode.org/ yields the same output as C#. Read [ask] and provide a [mre], including how you get that string. – CodeCaster Jul 15 '20 at 23:51
  • That's an odd string - are you sure it's a "string"?. Those "?-in-a-diamond" characters are the Unicode *REPLACEMENT CHARACTER* (U+FFFD) - https://en.wikipedia.org/wiki/Specials_(Unicode_block) . It looks more like a salt string followed by a "bunch of bytes". Bunches of bytes tend to get messed up when converting to characters (unicode encodings, illegal byte combinations, etc.) – Flydog57 Jul 16 '20 at 00:17
  • 1
    I forgot to mention. The REPLACEMENT CHARACTER has semantics attached to it - basically "I can't display this data properly". Or, in the words of Wikipedia: *The replacement character � (often a black diamond with a white question mark or an empty square box) is a symbol found in the Unicode standard at code point U+FFFD in the Specials table. It is used to indicate problems when a system is unable to render a stream of data to a correct symbol. It is usually seen when the data is invalid and does not match any character.* – Flydog57 Jul 16 '20 at 00:20
  • The code shown works correctly (and this question asked many times already as indicated by the duplicate I selected). Please review [MCVE] guidance on posting code and make sure to include code and data inline in the sample to demonstrate the problem (if you [edit] post it may be re-opened if indeed there is enough information to justify that). At this point there is no way to help as code that actually introduces the difference is not present in the question. – Alexei Levenkov Jul 16 '20 at 01:17
  • @AlexeiLevenkov I updated the question please have another look and re open it – Iyad Bacdounes Jul 16 '20 at 05:32
  • I added new post https://stackoverflow.com/questions/62928969/c-sharp-convert-tobase64string-vs-php-base64-encode-concatenate-between-string-a – Iyad Bacdounes Jul 16 '20 at 06:42

0 Answers0