How to encrypt a .txt file in Windows Forms C #?

0

I have a job to do where I need to save data from a list to text files. These data are for the most part, password , username , idade , nacionalidade and numeroCC . At work you are asked to encrypt username ;; password in a text file. The remaining data is stored in regular text files with fixed size.

Explaining better, I will have to create 6 files, 3 of them for pass and user encryption of each type of user of the program I am carrying out, with an organizer, several members of the organization, and adepts using the application to buy tickets have to be registered. The other files will have a fixed size to store the rest of the data.

How can I do the encryption part?

I already tried to do with the following code to encrypt.

    private static byte[] key = new byte[10] {0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
    private static byte[] iv = new byte[10] {0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };

    public static string encripta(string text)
    {
        SymmetricAlgorithm algorithm = DES.Create();
        ICryptoTransform transform = algorithm.CreateEncryptor(key, iv);
        byte[] inputbuffer = Encoding.UTF8.GetBytes(text);
        byte[] outputBuffer = transform.TransformFinalBlock(inputbuffer, 0, inputbuffer.Length);
        return Convert.ToBase64String(outputBuffer);
    }
    
asked by anonymous 06.09.2017 / 12:13

1 answer

2

You can do the following function:

Encode

public static string Base64Encode(string plainText) {
  var plainTextBytes = System.Text.Encoding.UTF8.GetBytes(plainText);
  return System.Convert.ToBase64String(plainTextBytes);
}

Decode

public static string Base64Decode(string base64EncodedData) {
  var base64EncodedBytes = System.Convert.FromBase64String(base64EncodedData);
  return System.Text.Encoding.UTF8.GetString(base64EncodedBytes);
}

And call Base64Encode before saving and call Base64Decode when reading data.

    
06.09.2017 / 13:55