2

Scenario - I have multiple files on my server and as per policy we can keep only encrypted files. We are doing some data migration, for this we have to move these files on to cloud and as per documentations Base64 is the best way to transfer encrypted data over network. I am new in encryption and stuck between this. Let me know if this help you.

How to save file in base64 format?

================================================================

I am new in Encryption and got below code from net and trying to encrypting files using RijndaelManaged and the following code is working fine

public static void EncryptFile(string inputFile, string outputFile)
{

        try
        {
            string password = @"myKey123"; // Your Key Here
            UnicodeEncoding UE = new UnicodeEncoding();
            byte[] key = UE.GetBytes(password);

            string cryptFile = outputFile;
            FileStream fsCrypt = new FileStream(cryptFile, FileMode.Create);

            RijndaelManaged RMCrypto = new RijndaelManaged();

            CryptoStream cs = new CryptoStream(fsCrypt,
                RMCrypto.CreateEncryptor(key, key),
                CryptoStreamMode.Write);

            FileStream fsIn = new FileStream(inputFile, FileMode.Open);

            int data;
            while ((data = fsIn.ReadByte()) != -1)
                cs.WriteByte((byte)data);


            fsIn.Close();
            cs.Close();
            fsCrypt.Close();
        }
        catch
        {
        }
    }

When I try to decrypt using Convert.FromBase64String, it returns an error

public static string DecryptFile(string inputFile)
        {
            var myRijndael = new RijndaelManaged { Key = _key, IV = _key, Padding = PaddingMode.PKCS7 };
            _decryptor = myRijndael.CreateDecryptor(myRijndael.Key, myRijndael.IV);

            // Create the streams used for decryption.
            using (MemoryStream msDecrypt = new MemoryStream(Convert.FromBase64String(inputFile)))
            {
                using (CryptoStream csDecrypt = new CryptoStream(msDecrypt, _decryptor, CryptoStreamMode.Read))
                {
                    using (StreamReader srDecrypt = new StreamReader(csDecrypt))
                    {
                        return srDecrypt.ReadToEnd();
                    }
                }
            }
        }

Please help me to solve this issue

Ajay
  • 783
  • 3
  • 16
  • 37
  • 4
    Since you're not writing data to your file as `Base64` format - it is very strange, why do you expect it to be in this format and converting from it.... – Andrey Korneyev Feb 09 '17 at 14:08
  • 4
    what "error" are you getting? this is crucial to solve the problem – NicoRiff Feb 09 '17 at 14:09
  • 2
    BTW, you should use `using` statements in `EncryptFile`, or at least a `finally`. Oh, and add one around your `RijndaelManaged` instances too, it implements `IDisposable`. – gobes Feb 09 '17 at 14:14
  • `key = UE.GetBytes(password);` is very wrong. Look up `PasswordDeriveBytes`. – H H Feb 09 '17 at 14:17
  • 5
    Lots of erroneous code "runs fine" with an empty `catch{}` behind it. – H H Feb 09 '17 at 14:20
  • 1. There is no need to Base64 encode file data. 2. The key should be the exact length of supported key sizes , for AES (Rijndael with a 16-byte block size) tose are 128, 192 and 256 bits other wise undefined padding will be added in an undefined manor. – zaph Feb 09 '17 at 14:40
  • How to decode without Base64 format? – Ajay Feb 09 '17 at 14:43
  • There is no need to encode or decode, just use the data. Encryption works on data bytes. – zaph Feb 09 '17 at 14:44
  • I a new in Encryption and tried to decrypt using Base64 and Bytes but getting error. – Ajay Feb 09 '17 at 14:46
  • 1
    You need to achieve symmetry - when the encryptor does not use Base64 then neither should the decryptor. – H H Feb 09 '17 at 14:50
  • Hi.. I know, I am not storing file in Base64 format. I was trying to do but not succeed. I am getting Unhandled exception from user code: `The input is not a valid Base-64 string as it contains a non-base 64 character, more than two padding characters, or an illegal character among the padding characters`. How I can store encrypted file in Base64 format? – Ajay Feb 10 '17 at 05:45
  • Why would you want base64? Your question is still unclear. Edit it and be clear about your actual objectives. – H H Feb 10 '17 at 11:11
  • Please see edits (Top of Question)...... – Ajay Feb 10 '17 at 12:39
  • 1
    _Base64 is the best way to transfer encrypted data over network_ is only true for transport over http or other text based protocols. Shouldn't apply to the actual storage in any case. Try to find existing encryption and transport solutions. DIY security is never a good idea. – H H Feb 10 '17 at 18:24
  • We r facing encryption issues n struggling to save file in base64 format. – Ajay Feb 10 '17 at 19:03
  • "it returns an error" I don't believe you. I read the entire question and I saw no exception message. You should [edit], add the full exception details and prove me a liar. –  Feb 10 '17 at 19:16
  • I am not here for time pass. Read 6-7 comments from below. I posted error message too. – Ajay Feb 11 '17 at 02:01
  • I only understand your problem/question vaguely. There is too much missing and too much wrong to give you a direct answer. Best advice is to separate this in a) encryption using `byte[]` and b) transport/storage using Base64 strings. – H H Feb 11 '17 at 09:25

0 Answers0