Skip to main content

Non-encrypted confidential information - Hexadecimal

Need

Protection of confidential information through encryption

Context

  • Usage of C# for developing robust and scalable applications
  • Usage of Microsoft.AspNetCore.Mvc for building web applications with ASP.NET Core
  • Usage of System.Text for string manipulation and encoding/decoding

Description

Non compliant code

public IActionResult GetConfidentialData()
{
var confidentialData = "This is confidential data";
var encodedData = BitConverter.ToString(Encoding.UTF8.GetBytes(confidentialData)).Replace("-", "");
return Ok(encodedData);
}

In the above code, we have a method GetConfidentialData() in a controller, which returns some confidential data. This method is supposed to return the confidential data in a secure way, but instead, it's encoding the data in hexadecimal format and then sending it as a response.

The Encoding.UTF8.GetBytes(confidentialData) method is used to convert the string data into a byte array. This byte array is then converted into a hexadecimal string using the BitConverter.ToString() method. The "-" character is removed from the hexadecimal string using the Replace("-", "") method.

This is a vulnerable piece of code because if an attacker intercepts this data, they can easily decode the hexadecimal string back into plain text and view the confidential information.

The hexadecimal encoding is not a secure way to transmit confidential data. It just changes the representation of the data but doesn't encrypt it. Therefore, it's recommended to use a proper encryption method to secure the confidential data.

Steps

  • Use encryption algorithms to protect the confidential data.
  • Do not store or transmit confidential data in plain text.
  • Use secure protocols (e.g., HTTPS) to transmit confidential data.
  • Implement proper access controls to restrict unauthorized access to the confidential data.
  • Regularly update and patch the application and its dependencies to address any security vulnerabilities.

Compliant code

public IActionResult GetConfidentialData()
{
var confidentialData = "This is confidential data";
var encryptedData = EncryptData(confidentialData);
return Ok(encryptedData);
}

private string EncryptData(string data)
{
var key = Encoding.UTF8.GetBytes("your-encryption-key-here"); // Replace with your encryption key
using (var aes = Aes.Create())
{
aes.Key = key;
aes.GenerateIV();
var encryptor = aes.CreateEncryptor(aes.Key, aes.IV);
using (var ms = new MemoryStream())
{
using (var cs = new CryptoStream(ms, encryptor, CryptoStreamMode.Write))
using (var sw = new StreamWriter(cs))
{
sw.Write(data);
}
var iv = aes.IV;
var encrypted = ms.ToArray();
return Convert.ToBase64String(iv.Concat(encrypted).ToArray());
}
}
}

The original code was vulnerable because it was encoding confidential data into hexadecimal format without any encryption. This means that anyone who intercepts the data can easily decode it and view the confidential information.

The updated code now encrypts the confidential data using the AES encryption algorithm before sending it. The EncryptData method takes the confidential data as input and returns the encrypted data. The encryption key is a byte array that you should replace with your own key.

The Aes.Create() method is used to create a new instance of the Aes class which performs the symmetric algorithm for encryption and decryption. The aes.Key is set to your encryption key and aes.GenerateIV() is used to generate a random initialization vector (IV).

The aes.CreateEncryptor(aes.Key, aes.IV) method is used to create a symmetric encryptor object with the specified key and IV. The CryptoStream class is used for encryption and writes the encrypted data to a MemoryStream.

Finally, the encrypted data and IV are concatenated and converted to a Base64 string. This encrypted data is then returned by the GetConfidentialData method.

This way, even if an attacker intercepts the data, they won't be able to decode it without the encryption key. This significantly improves the security of the confidential data.

References