C# reading encrypted file returns Black diamonds with ? inside

tim 200 Reputation points
2024-09-11T21:12:06.62+00:00

I have an encrypted file that contains the following chars.

áÒá,èÜÒè

what I see when debugging is Black diamonds containing "?".

I think my open/read file is using the wrong file data type. (unicode, ascii, etc..)

and not correctly reading the data, thus preventing my decryption code from working.

using (var stream = await AuthFile.OpenReadAsync())

                        {

                            using (var reader = new StreamReader(stream))

                            {                                   

                                while ((line = reader.ReadLine()) != null)

                                { ...

if this helps,

the file is created by a VBScript using the following code line.

Set objFileToWrite = CreateObject("Scripting.FileSystemObject").OpenTextFile("c:\vbscripts\pid.txt",2,true)

TIA

Tim

C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
11,012 questions
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. tim 200 Reputation points
    2024-09-11T21:31:38.5033333+00:00

    I think I found the problem

    I think should be using

    byte[] readText = File.ReadAllBytes(AuthFile.FullPath);

    ( now if I can get past the "not access" error)

    Tim

    0 comments No comments

  2. Jiale Xue - MSFT 46,466 Reputation points Microsoft Vendor
    2024-09-13T14:44:26.0833333+00:00

    Hi @tim , Welcome to Microsoft Q&A,

    By default, StreamReader uses UTF-8 encoding, but it seems that your file might have been created with a different encoding (such as ANSI or a different character set). To resolve this, you should explicitly specify the encoding when reading the file.

    You can modify your StreamReader to use a specific encoding, such as Encoding.Default (which uses the system's default encoding, often ANSI) or another suitable encoding like Encoding.GetEncoding("ISO-8859-1") if needed.

    When you use byte[] readText = File.ReadAllBytes(AuthFile.FullPath); to read a file, File.ReadAllBytes will directly read the byte data of the file without relying on the encoding.

    Best Regards,

    Jiale


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment". 

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.