none
C# byte from string RRS feed

  • Question

  • I am having an issue creating an encryption method, then base64 encoding it to work with PHP.
    The issue is converting the encrypted string to a byte array.
    The encrypted data is ('K"3LPt¡'). The byte data for this comes out as 

    [253,2,95,253,14,253,253,93,127,75,34,51,76,80,116,253,2]

    In PHP it's

    [137,2,95,164,14,186,206,93,127,75,34,51,76,80,116,161,2]

    The code in C#

                string PHP = File.ReadAllText("someToConvert.txt");
                byte[] ToConvertAndStuff = new byte[PHP.Length];
                char[] Chars = PHP.ToCharArray();
                for(int I = 0; I < PHP.Length; I++)
                {
                    ToConvertAndStuff[I] = (byte)Chars[I];
                }

    However, if I use

    plainTextBytes = Encoding.ASCII.GetBytes(PHP);

    It does not retrieve the proper byte values either.
    This keeps supplying the wrong byte[] no matter what encoding I use.


    Monday, November 18, 2019 9:39 AM

All replies

  • Hi McSwampy, 

    Thank you for posting here.

    Acccording to your question, I make a test based on your code, but I cannot reproduce your problem.

    Could you share your ‘someToConvert.txt’ file including some related encrypted data here? It will help us to make a test.

    Besides, the different result may caused by the differences between C# and PHP.

    Can you decrypt the data correctly in C# when using the different byte array?

    We are waiting for your update.

    Best Regards,

    Xingyu Zhao


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Tuesday, November 19, 2019 9:05 AM
    Moderator