Search code examples
adminprivileges

Can I make a txt file unreadable to normal users but still have a program access it?


My question is if i have a c# application that the non admin users can launch and that application reads from the file. Is there a way to make it so that the program can read it but the users cant just open it? I'm guessing if I make the file need admin privileges the program would not be able to read it either.


Solution

  • You could try encryption, and make your own kind of encryption unlike the common encryption alogorithms, like AES or RSA.

    Sure, they are the standard and pretty good, but if you REALLY want any (non-admin privileged user) to not access the file's contents, you could try making your own kind of encyption*, a cipher is a simpler form of encyption, and eaiser to implement, example below;

    (EDIT: * I say this because a user could upload the file's contents to an AES/RSA decrypter and decrypt the file, hence using advanced/obscure methods may help in less chances of experienced users decrypting your file(s))

    The quick brown fox jumps over the lazy dog
    

    we could shift each letter by the next one;

    Uif rvjdl cspxo gpy kvnqt pwfs uif mbaz eph
    

    the C# code is below;

    static string Encrypt(string plaintext)
    {
        string ciphertext = "";
        foreach (char c in plaintext)
        {
            if (char.IsLetter(c))
            {
                char shifted = (char)(c + 1); // shifting each letter by one
                if ((char.IsLower(c) && shifted > 'z') || (char.IsUpper(c) && shifted > 'Z'))
                {
                    shifted = (char)(c - 25); // Here we make sure if we have the letter z, we can make sure it goes to letter a, without any errors
                }
                ciphertext += shifted;
            }
            else
            {
                ciphertext += c; // Keep non-letter characters unchanged
            }
        }
        return ciphertext;
    }
    
    static void Main()
    {
        string plaintext = "The quick brown fox jumps over the lazy dog";
        string encryptedText = Encrypt(plaintext);
        Console.WriteLine("Encrypted text: " + encryptedText);
    }
    
    //Your basic Caeser Cipher (for shifting by 1 letter)
    
    

    (IMPORTANT NOTE: This only works for .txt files (Text files) and not for anything outside of that, sure it may "work" but it wont be a proper kind of encrption, as some non-alphabet characters wont be encrpyted)

    (Code for decryption any encrpted text)

    static string Decrypt(string ciphertext)
    {
        string plaintext = "";
        foreach (char c in ciphertext)
        {
            if (char.IsLetter(c))
            {
                char shifted = (char)(c - 1); // reverse the shift by one
                if ((char.IsLower(c) && shifted < 'a') || (char.IsUpper(c) && shifted < 'A'))
                {
                    shifted = (char)(c + 25); // again wrapping around incase
                }
                plaintext += shifted;
            }
            else
            {
                plaintext += c; // keeping non-letter characters unchanged
            }
        }
        return plaintext;
    }