Convert C# code fragment into compilable code for analysis
New here? Learn about Bountify and follow @bountify to get notified of new bounties! x

I have the following C# code fragment from a web service documentation:

private string Encrypt256(string text, AesCryptoServiceProvider aes)
{
    // Convert string to byte array
    byte[] src = Encoding.Unicode.GetBytes(text);
    // encryption
    using (ICryptoTransform encrypt = aes.CreateEncryptor())
    {
        byte[] dest = encrypt.TransformFinalBlock(src, 0, src.Length);
        // Convert byte array to Base64 strings
        return Convert.ToBase64String(dest);
    }
}
...
Credentials credential = new Credentials();
credential.UserName = "username";
credential.Password = "password";
credential.ClientUtcTime = DateTime.UtcNow.ToString("yyyy-MM-ddTHH:mm:ssZ", System.Globalization.CultureInfo.InvariantCulture);

//--Serialize credential
XmlSerializer serializer = new XmlSerializer(credential.GetType());
string xmlCredential = string.Empty;
using (var stringwriter = new System.IO.StringWriter())
{
    serializer.Serialize(stringwriter, credential);
    xmlCredential = stringwriter.ToString();
}
//--Encrypt credential with AES256 symmetric
String encryptedCredential = Encrypt256(xmlCredential, aesServiceProvider);
...

I would like to see for myself how this code works, but being unfamiliar with C# I am not sure how to make this into a compilable program.

In addition to the final source code, I would like to see console output for the serialized Credentials object before and after encryption.

Can anyone help with this?

Can you p\rovide me the complete source code or the link to web service documentation.Thank you
Codeword 28 days ago
I don't have the complete source code - that is why I have posted this spec, to ask for someone to make this fragment compilable so that I can figure out how it works. This code fragment comes from the documentation itself, but unfortunately I cannot share the document itself because it contains proprietary information.
ifdSeekay 28 days ago
I have particular trouble with the Credentials part of the fragment; it doesn't seem to be from any system libraries, based on my Google results. The poster of the first solution thinks it could be a custom class, but I'm not sure about this since I only have this fragment to refer to.
ifdSeekay 28 days ago
No problem.Thank you
Codeword 28 days ago
I too think as @kefir posted that credentials is a custom class.So you can go with the solution as provided by @kefir.Thank you
Codeword 28 days ago
awarded to Kefir
Tags
c#
xml

Crowdsource coding tasks.

1 Solution

Winning solution

Below is a compilable program (console application). Credentials class seems to be a custom class, there are no system classes like this, so I had to create it and add properties as needed.

using System;
using System.IO;
using System.Security.Cryptography;
using System.Text;
using System.Xml.Serialization;

namespace ConsoleApplication
{
    class Program
    {
        static void Main(string[] args)
        {
            // Credentials seems to be a custom class, there are no system classes like this.
            Credentials credential = new Credentials();
            credential.UserName = "username";
            credential.Password = "password";
            credential.ClientUtcTime = DateTime.UtcNow.ToString("yyyy-MM-ddTHH:mm:ssZ", System.Globalization.CultureInfo.InvariantCulture);

            //--Serialize credential
            XmlSerializer serializer = new XmlSerializer(credential.GetType());
            string xmlCredential;
            using (var stringwriter = new Utf8StringWriter())
            {
                serializer.Serialize(stringwriter, credential);
                xmlCredential = stringwriter.ToString();
            }

            Console.WriteLine("XML credentials:");
            Console.WriteLine(xmlCredential);
            Console.WriteLine();


            //--Encrypt credential with AES256 symmetric
            var aesServiceProvider = new AesCryptoServiceProvider();
            String encryptedCredential = Encrypt256(xmlCredential, aesServiceProvider);

            Console.WriteLine("Encrypted credentials:");
            Console.WriteLine(encryptedCredential);
        }

        private static string Encrypt256(string text, AesCryptoServiceProvider aes)
        {
            // Convert string to byte array
            byte[] src = Encoding.Unicode.GetBytes(text);

            // Get custom key and IV from somewhere.
            byte[] key = new byte[32];
            byte[] IV = new byte[16];

            // encryption
            using (ICryptoTransform encrypt = aes.CreateEncryptor(key, IV))
            {
                byte[] dest = encrypt.TransformFinalBlock(src, 0, src.Length);
                // Convert byte array to Base64 strings
                return Convert.ToBase64String(dest);
            }
        }
    }

    public class Credentials
    {
        public string UserName { get; set; }
        public string Password { get; set; }
        public string ClientUtcTime { get; set; }
    }

    public class Utf8StringWriter : StringWriter
    {
        public override Encoding Encoding { get { return Encoding.UTF8; } }
    }
}

Output looks like this:

XML credentials:
<? xml version="1.0" encoding="utf-8"?>
<Credentials xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<UserName>username</UserName>
<Password>password</Password>
<ClientUtcTime>2017-09-26T08:12:22Z</ClientUtcTime>
</Credentials>

Encrypted credentials:
vlLURmAxYdDw0MBFkJlDiujCFxDfav5DSN0vrRrGgEuOm8s8oP96sJxaM5RDRvdmxSeH48NI74+TNPJIfVA/v654AIip7G85FHxHdyfj6LhF7VjjwDaDgvLEURwSVtJgNAMGWWycriHaeBpvi0TFyBekxxFqnwd1DS2SMzPoQ6ZWNOkueZmieu2gyVgP1QJUG7cOuHuzSZBYKZwu9DpmMRyZS1dF926DgCis9WRINr4e7k72e7mVZhzteZmtrsRH9jwgj8I4eFpVMXNdAPowUhi2/LL3eyBnsADQPSz1CSqB0Rd5zNku21sVz3MnlVK8QrbOAnRPWi8z8eBUl+qS0vtdiDp+cMfhGykzXJf+YejDuvU437NEjWj42svZmYX03beYRoqorXXnRmcbCtvVIbsKZ6kazpWiy4cxfZOOg0iGM+cIVn+bwOAwaplmELbaajMyxw83TkOQt1gHtymbSO0xN70Lo62Y6JiOvveR/R8QUHa6l/lL2TDunBASe//u/dEK8C2fw3x3k2q50qO1iOrcpXfPJP1XRZa6EUhNgeQdaWTUFTjbJVavkUnUKxDz27ZIbvE5rs6JD4YvmxVcVL/W+O25XgoJWATexNZTjeR09sDhXOThBVOq7H4lXXsKybS8UTau+HpFwmNjwm7OPNBjU7IQR52Aef/bDYmoubH7X+H9kCur9pTdcRXNY+8wZZk7Q8IVOPQui7qZBvMmWrMJvwR6AJgSi+XkIvbwX1bCsh7cc+zMtYaRR7GWspAu8O2I/G4RyyFF0N5OpuGdFw==
This is great, but I notice that the encoding is UTF-16. Is it possible to change it to UTF-8? Also, I forgot to include this in the spec, so if it isn't too much trouble, could you change the Encrypt256 method such that the encryptor is initialized with a custom IV and key?
ifdSeekay 28 days ago
Updated to UTF-8 and custom IV/key.
Kefir 28 days ago
Perfect. I'll accept this, thanks!
ifdSeekay 28 days ago