0

I have encryption decryption code which is perfectly working fine in local. From local system using code first approach I created database in aws which created successfully with seeds values in which I have decrypted the password. Now, I have published the .net6 application in aws ec2 instance. On logging it is giving error of incorrect credentials. I have logged the username and passwords and rechecked the scenario. The issue I have found is the encryption is changed. I have updated the password and successfully logged in. But now the problem is with roles. I have applied checks on encrypted role ids which are not maching now. Can anyone please help me here on this issue please? `

using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;

namespace Common
{
    public static class EncyptionDcryption
    {
        static string key = "85OIbnI9";
        static string vector = "eH90BDl0";
        ////////////////////////////////////////////////////////////////////////////////
        // Decryption
        ////////////////////////////////////////////////////////////////////////////////
        public static string GetDecryptedText(string txt)
        {
            txt = txt.Replace(' ', '+');
            DESCryptoServiceProvider key = new DESCryptoServiceProvider();

            key.Key = ASCIIEncoding.ASCII.GetBytes(key); // decryption key

            key.IV = ASCIIEncoding.ASCII.GetBytes(vector);// initialization vector

            int length = txt.Length;
            byte[] buffer = new byte[length];
            buffer = Convert.FromBase64String(txt);

            string decText = Decrypt(buffer, key);

            return decText;
        }

        public static string Decrypt(byte[] CypherText, SymmetricAlgorithm key)
        {
            // Create a memory stream to the passed buffer.
            MemoryStream ms = new MemoryStream(CypherText);

            // Create a CryptoStream using the memory stream and the 
            // CSP DES key. 

            CryptoStream encStream = new CryptoStream(ms, key.CreateDecryptor(), CryptoStreamMode.Read);
            // Create a StreamReader for reading the stream.

            StreamReader sr = new StreamReader(encStream);
            // Read the stream as a string.

            string val = sr.ReadLine();

            // Close the streams.
            sr.Close();
            encStream.Close();
            ms.Close();

            return val;
        }

        ////////////////////////////////////////////////////////////////////////////////
        // Encryption
        ////////////////////////////////////////////////////////////////////////////////
        public static byte[] Encrypt(string PlainText, SymmetricAlgorithm key)
        {
            // Create a memory stream.
            MemoryStream ms = new MemoryStream();

            // Create a CryptoStream using the memory stream and the 
            // CSP DES key.  
            CryptoStream encStream = new CryptoStream(ms, key.CreateEncryptor(), CryptoStreamMode.Write);

            // Create a StreamWriter to write a string
            // to the stream.
            StreamWriter sw = new StreamWriter(encStream);

            // Write the plaintext to the stream.
            sw.WriteLine(PlainText);

            // Close the StreamWriter and CryptoStream.
            sw.Close();
            encStream.Close();

            // Get an array of bytes that represents
            // the memory stream.
            byte[] buffer = ms.ToArray();

            // Close the memory stream.
            ms.Close();

            // Return the encrypted byte array.
            return buffer;
        }

        public static string GetEncryptedText(string txt)
        {
            DESCryptoServiceProvider key = new DESCryptoServiceProvider();
            key.Key = ASCIIEncoding.ASCII.GetBytes(key); // decryption key
            key.IV = ASCIIEncoding.ASCII.GetBytes(vector);// initialization vector

            //  Encrypt a string to a byte array.
            byte[] buffer = Encrypt(txt, key);
            string encText;
            encText = Convert.ToBase64String(buffer);

            return encText;
        }
    }
}

`

Why it behaves differently on server and local? But no clue.

  • The question is unclear - what's the actual problem? The code should work the exact same locally or anywhere. Can you show us the input, real output and expected output on both local and the AWS machine? That may help. – Ermiya Eskandary Nov 12 '22 at 23:32
  • Apparently, it should work the same but unfortunately not. On local, for password admin I got 9/JKsS/GGE0= and on aws I get QGz3egWpeFw= That's why it is my question that it should work the same on local and aws. Maybe there is something that I don't know. – Fahad Altaf Nov 17 '22 at 18:12
  • Is it because on local I am using windows and on the server it is Linux? – Fahad Altaf Nov 21 '22 at 04:41

0 Answers0