locked
Encrypt string

    Question

  • How can I modify this method to make the encrypted

    public static string Encrypt(string clearText, string Password)
        {
    
    
          byte[] clearBytes =
           System.Text.Encoding.Unicode.GetBytes(clearText);
    
    
          PasswordDeriveBytes pdb = new PasswordDeriveBytes(Password,
            new byte[] {0x49, 0x76, 0x61, 0x6e, 0x20, 0x4d, 
          0x65, 0x64, 0x76, 0x65, 0x64, 0x65, 0x76});
    
          byte[] encryptedData = Encrypt(clearBytes,
               pdb.GetBytes(32), pdb.GetBytes(16));
    
    
    
          return Convert.ToBase64String(encryptedData);
    
        }
    

    string valid to be used as a file name in the windows system??

     

    Tuesday, July 26, 2011 5:16 PM

Answers

  • You have to remove the invalid path characters. However, if you do that you need a way to backtrack the changes assuming you want to later decrypt the value used for the file name.

    string encrypted = "...";
    char[] invalidChars = System.IO.Path.GetInvalidFileNameChars();
    char[] encryptedChars = encrypted.ToCharArray();
    
    var validChars = encryptedChars.Except(invalidChars);
    string validString = new string(validChars.ToArray());

    • Marked as answer by Cookie Luo Thursday, August 04, 2011 3:18 AM
    Tuesday, July 26, 2011 5:38 PM

All replies

  • You have to remove the invalid path characters. However, if you do that you need a way to backtrack the changes assuming you want to later decrypt the value used for the file name.

    string encrypted = "...";
    char[] invalidChars = System.IO.Path.GetInvalidFileNameChars();
    char[] encryptedChars = encrypted.ToCharArray();
    
    var validChars = encryptedChars.Except(invalidChars);
    string validString = new string(validChars.ToArray());

    • Marked as answer by Cookie Luo Thursday, August 04, 2011 3:18 AM
    Tuesday, July 26, 2011 5:38 PM
  • How excrypted does it need to be?  I suggest writing a custom "encryption" routine that only uses valid path characters and uses a simple substitution.  Obviously if you really need to encrypt your path names (which are still discoverable btw) this won't work for you.
    Tuesday, July 26, 2011 5:43 PM
  • Have you validated to check if the value as is can be used as a file name? I believe the name as is should work fine. But have you seen a case where it does not?
    Tuesday, July 26, 2011 6:35 PM
  • I'd also like to point out that Windows File names are not case sensitive. Even though the chances of having two encrypted then converted to Base64String strings the same with the only difference being an "A" as opposed to an "a" are probably astronomically small, if you encrypt enough files you might eventually get to that point. I realize it's not a serious consideration but thought I'd put it out there.
    Tuesday, July 26, 2011 8:19 PM