CryptoStream behaves differently on local and aws

67 Views Asked by At

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.

0

There are 0 best solutions below