<jupyter_start><jupyter_text>Binary Converter<jupyter_code>class BinaryConverter
    {
        //convert string to binary  : Encoding
        public string StringToBinary(string name)
        {
            string binaryverion = "";
            foreach (char c in name)
            {
                string binary = "";
                int no = (int)c;
                while (no > 1)
                {
                    int remainder = no % 2;
                    binary = Convert.ToString(remainder) + binary;
                    no /= 2;
                }
                binary = Convert.ToString(no) + binary;
                binaryverion = binaryverion + binary.PadLeft(8, '0');
                //Console.WriteLine("8 bit ASCII for {0} = {1} Binary = {2}", c, (int)c, binary.PadLeft(8, '0'));
                //Console.WriteLine("7 bit ASCII for {0} = {1} Binary = {2}", c, (int)c, binary.PadLeft(7, '0'));
                //Console.WriteLine();
            }
            Console.WriteLine($"String = {name} = {binaryverion}");
            return binaryverion.ToString();
        }<jupyter_output><empty_output><jupyter_text>/// DEcoding : Convert a Binary text string to a Text string<jupyter_code>public string BinaryToString(string binarydata)
        {
            //=======================Approach 1
            string result = "";
            for (int i = 0; i < binarydata.Length; i += 8)
            {
                var first8_bits = binarydata.Substring(i, 8);
                var number = Convert.ToInt32(first8_bits, 2);
                result += (char)number;
            }
            Console.WriteLine($"Binary {binarydata} to string ={result}");
            return result;
        }
    }<jupyter_output><empty_output><jupyter_text>Use of BinaryConverter in Program.cs<jupyter_code>//Task 2 : Convert your full name to binary
Console.WriteLine("\n=======================Binary Converter======================\n");
BinaryConverter base2 = new BinaryConverter();
string binaryData = base2.StringToBinary(fullName);
base2.BinaryToString(binaryData);
Hexadeciaml Converter
 class HexadecimalConverter
    {
        //converts full name to hexadecimal
        public string hexaConverter(string str)
        {
            string hexastring = "";
            foreach (char c in str)
            {
                int x = (int)c;
                string result = "";
                while (x != 0)
                {
                    if ((x % 16) < 10)
                        result = x % 16 + result;
                    else
                    {
                        string temp = "";
                        switch (x % 16)
                        {
                            case 10: temp = "A"; break;
                            case 11: temp = "B"; break;
                            case 12: temp = "C"; break;
                            case 13: temp = "D"; break;
                            case 14: temp = "E"; break;
                            case 15: temp = "F"; break;
                        }
                        result = temp + result;
                    }
                    x /= 16;
                }
                hexastring += result;
               // Console.WriteLine("ASCII for = {0}  = {1} Hexa = {2}", c, (int)c, result);
            }
            Console.WriteLine($"{str} = Hexadecimal = {hexastring}");
            return hexastring;
        }<jupyter_output><empty_output><jupyter_text>//decoding : from hexa to string<jupyter_code>public string ConvertHexToString(string hexa)
        {
            string result = "";
            for (int i = 0; i < hexa.Length; i += 2)
            {
                var first2_bits = hexa.Substring(i, 2);
                var number = Convert.ToInt32(first2_bits, 16);
                result += (char)number;
            }
            Console.WriteLine("hexa to string  = " + result);
            return result;
        }
    }<jupyter_output><empty_output><jupyter_text>Use of HexadecimalConverter in Program.cs<jupyter_code>//Task 3
Console.WriteLine("\n=======================Hexadecimnal Converter======================\n");
HexadecimalConverter base16 = new HexadecimalConverter();
string hexastring = base16.hexaConverter(fullName);
base16.ConvertHexToString(hexastring);
Base 64 Converter
    class Base64Converter
    {
        byte[] source;
        int length, length2;
        int blockCount;
        int paddingCount;
        //converts string to Base64 format
        public string Base64Encode(string input)
        {
            //string name = "A";
            byte[] bytes = Encoding.ASCII.GetBytes(input);
            //string input1 = input;
            //byte[] bytes = Encoding.ASCII.GetBytes(input);
            source = bytes;
            length = input.Length;
            if ((length % 3) == 0)
            {
                paddingCount = 0;
                blockCount = length / 3;
            }
            else
            {
                paddingCount = 3 - (length % 3);//need to add padding
                blockCount = (length + paddingCount) / 3;
            }
            length2 = length + paddingCount;//or blockCount *3
            char[] demo = GetEncoded();
            string base64 = new string(demo);
            Console.WriteLine($"{input} = Base64 value = {base64}");
            return base64;
        }
        public char[] GetEncoded()
        {
            byte[] source2;
            source2 = new byte[length2];
            //copy data over insert padding
            for (int x = 0; x < length2; x++)
            {
                if (x < length)
                {
                    source2[x] = source[x];
                }
                else
                {
                    source2[x] = 0;
                }
            }
            byte b1, b2, b3;
            byte temp, temp1, temp2, temp3, temp4;
            byte[] buffer = new byte[blockCount * 4];
            char[] result = new char[blockCount * 4];
            for (int x = 0; x < blockCount; x++)
            {
                b1 = source2[x * 3];
                b2 = source2[x * 3 + 1];
                b3 = source2[x * 3 + 2];
                temp1 = (byte)((b1 & 252) >> 2);//first
                temp = (byte)((b1 & 3) << 4);
                temp2 = (byte)((b2 & 240) >> 4);
                temp2 += temp; //second
                temp = (byte)((b2 & 15) << 2);
                temp3 = (byte)((b3 & 192) >> 6);
                temp3 += temp; //third
                temp4 = (byte)(b3 & 63); //fourth
                buffer[x * 4] = temp1;
                buffer[x * 4 + 1] = temp2;
                buffer[x * 4 + 2] = temp3;
                buffer[x * 4 + 3] = temp4;
            }
            for (int x = 0; x < blockCount * 4; x++)
            {
                result[x] = sixbit2char(buffer[x]);
            }
            //covert last "A"s to "=", based on paddingCount
            switch (paddingCount)
            {
                case 0: break;
                case 1: result[blockCount * 4 - 1] = '='; break;
                case 2:
                    result[blockCount * 4 - 1] = '=';
                    result[blockCount * 4 - 2] = '=';
                    break;
                default: break;
            }
            return result;
        }
        private char sixbit2char(byte b)
        {
            char[] lookupTable = new char[64]
                {  'A','B','C','D','E','F','G','H','I','J','K','L','M',
            'N','O','P','Q','R','S','T','U','V','W','X','Y','Z',
            'a','b','c','d','e','f','g','h','i','j','k','l','m',
            'n','o','p','q','r','s','t','u','v','w','x','y','z',
            '0','1','2','3','4','5','6','7','8','9','+','/'};
            if ((b >= 0) && (b <= 63))
            {
                return lookupTable[(int)b];
            }
            else
            {
                //should not happen;
                return ' ';
            }
        }
        //decoding base64 to original string
        public string Base64StringDecode(string encodedString)
        {
            var bytes = Convert.FromBase64String(encodedString);
            var decodedString = Encoding.UTF8.GetString(bytes);
           Console.WriteLine($"Base64 = {encodedString} = To Original = {decodedString}");
            return decodedString;
        }
    }<jupyter_output><empty_output><jupyter_text>Use of Base64 in Program.cs<jupyter_code>//Task 4
Console.WriteLine("\n=======================Base64 Converter======================\n");
Base64Converter base64 = new Base64Converter();
string base64str = base64.Base64Encode(fullName);
base64.Base64StringDecode(base64str);
EncrypterDecrypter
    class EncryptDecrypt
    {
        public EncryptDecrypt(string originalText, int[] encryptionCipher = null, int encryptionDepth = 1)
        {
            OriginalText = originalText;
            if (encryptionCipher != null)
            {
                if (EncryptionCipher != encryptionCipher)
                {
                    EncryptionCipher = encryptionCipher;
                }
            }
            if (EncryptionDepth != encryptionDepth)
            {
                EncryptionDepth = encryptionDepth;
            }
            ConvertText(OriginalText, EncryptionCipher, EncryptionDepth);
        }
        public void ConvertText(string originalText, int[] encryptionCipher = null, int encryptionDepth = 1)
        {
            //Deep Cipher encrypt
            CipherEncrypted = DeepEncryptWithCipher(OriginalText, EncryptionCipher, EncryptionDepth);
        }
        public string OriginalText { get; set; }
        public int[] EncryptionCipher { get; } = new[] { 1, 1, 2, 3, 5, 8 }; //Fibonacci Sequence
        public int EncryptionDepth { get; } = 1;
        public string CipherEncrypted { get; set; }
        public static string DeepEncryptWithCipher(string originalText, int[] encryptionCipher, int encryptionDepth)
        {
            string result = originalText;
            //For demonstration
            //string[] encryptedValues = new string[encryptionDepth + 1];
            //encryptedValues[0] = result;
            //Encrypt result encryptionDepth times
            for (int depth = 0; depth < encryptionDepth; depth++)
            {
                //Apply Encryption Cipher on current value of result
                result = EncryptWithCipher(result, encryptionCipher);
                //Add new encrypted result to the encrypted array fro demonstration
                //encryptedValues[depth + 1] = result;
            }
            return result;
        }
        /// <summary>
        /// Applies a Cipher to a string
        /// </summary>
        /// <param name="text">Text to encrypt</param>
        /// <param name="encryptionCipher">new[] { 1,2,3,4,5,6 }</param>
        /// <returns></returns>
        public static string EncryptWithCipher(string text, int[] encryptionCipher)
        {
            if (encryptionCipher == null || encryptionCipher.Length == 0)
            {
                return text;
            }
            //Store the original string converted to bytes
            //Convert the text data to Unicode byte in order to handle non ASCII value character
            byte[] bytearray = Encoding.Unicode.GetBytes(text);
            //Build byte array from the original byte array that will receive the encrypted values
            byte[] bytearrayresult = bytearray;
            int encryptionCipherIndex = 0;
            //Apply Encryption Cipher
            for (int i = 0; i < bytearray.Length; i++)
            {
                //Set the Cipher index
                encryptionCipherIndex = i;
                //We reset the current encryption position to 0 to restart at the beginning of the encryptionCipher
                if (encryptionCipherIndex >= encryptionCipher.Length)
                {
                    //Reset the cryper postion to zero and restart sequence
                    encryptionCipherIndex = 0;
                }
                //These lines are for demonstration to show values
                //byte bytecharacter = bytearray[i];
                //int CipherChar = encryptionCipher[encryptionCipherIndex];
                //Change the value of the current character by the values received from the encryptionCipher array
                //int newchar = bytearray[i] + encryptionCipher[encryptionCipherIndex];
                //Change the value of the current character by the values received from the encryptionCipher array
                if (bytearray[i] != 0)
                {
                    bytearrayresult[i] = (byte)(bytearray[i] + encryptionCipher[encryptionCipherIndex]);
                }
            }
            string newresult = Encoding.Unicode.GetString(bytearrayresult);
            return newresult;
        }
        /// <summary>
        /// Decrypts a deep cipher encrypted string
        /// </summary>
        /// <param name="originalText">Cipher encrypted string</param>
        /// <param name="encryptionCipher">Sequence of whole numbers in an array</param>
        /// <param name="encryptionDepth">Depth of the encryption</param>
        /// <returns>Decrypted string</returns>
        public static string DeepDecryptWithCipher(string originalText, int[] encryptionCipher, int encryptionDepth)
        {
            string result = originalText;
            //For demonstration
            string[] encryptedValues = new string[encryptionDepth + 1];
            encryptedValues[0] = result;
            //Encrypt result encryptionDepth times
            for (int depth = 0; depth < encryptionDepth; depth++)
            {
                //Apply Encryption Cipher on current value of result
                result = DecryptWithCipher(result, encryptionCipher);
                //Add new encrypted result to the encrypted array fro demonstration
                encryptedValues[depth + 1] = result;
            }
            return result;
        }
        /// <summary>
        /// Decrypts a cipher encrypted string
        /// </summary>
        /// <param name="text"></param>
        /// <param name="encryptionCipher"></param>
        /// <returns></returns>
        public static string DecryptWithCipher(string text, int[] encryptionCipher)
        {
            //Convert the text data to Unicode byte in order to handle non ASCII value character
            byte[] bytearray = Encoding.Unicode.GetBytes(text);
            //Build byte array from the original byte array that will receive the encrypted values
            byte[] bytearrayresult = bytearray;
            int encryptionCipherIndex = 0;
            for (int i = 0; i < bytearray.Length; i++)
            {
                //Set the Cipher index
                encryptionCipherIndex = i;
                //We reset the current encryption position to 0 to restart at the beginning of the encryptionCipher
                if (encryptionCipherIndex >= encryptionCipher.Length)
                {
                    //Reset the cryper postion to zero and restart sequence
                    encryptionCipherIndex = 0;
                }
                if (bytearray[i] != 0)
                {
                    bytearrayresult[i] = (byte)(bytearray[i] - encryptionCipher[encryptionCipherIndex]);
                }
            }
            string newresult = Encoding.Unicode.GetString(bytearrayresult);
            return newresult;
        }
    }<jupyter_output><empty_output><jupyter_text>Use of EncrypterDecrypter in Program.cs<jupyter_code>//Task 5
//entryption decryption
string plaintext = fullName;
int encryptionDepth = 10;
Console.WriteLine("\n=======================entryption decryption======================\n");
int[] key = plaintext.ToString().Select(o => Convert.ToInt32(o)).ToArray();
for (int i = 0; i < key.Length; i++)
{
     Console.Write(key[i] +"\t");
}
Console.WriteLine("\n\n");
string cipherasString = String.Join(",", key.Select(x => x.ToString())); //For display purposes
//EncryptDecrypt encrypter = new EncryptDecrypt(plaintext, key, encryptionDepth);
EncryptDecrypt encrypter = new EncryptDecrypt(plaintext, key, encryptionDepth);
//Single Level Encrytion
string nameEncryptWithCipher = EncryptDecrypt.EncryptWithCipher(plaintext, key);
Console.WriteLine($"Encrypted once using the cipher {{{cipherasString}}} {nameEncryptWithCipher}");
string nameDecryptWithCipher = EncryptDecrypt.DecryptWithCipher(nameEncryptWithCipher, key);
Console.WriteLine($"Decrypted once using the cipher {{{cipherasString}}} {nameDecryptWithCipher}");
//Deep Encrytion
Console.WriteLine("\n");
string nameDeepEncryptWithCipher = EncryptDecrypt.DeepEncryptWithCipher(plaintext, key, encryptionDepth);
Console.WriteLine($"Deep Encrypted {encryptionDepth} times using the cipher {{{cipherasString}}} {nameDeepEncryptWithCipher}");
string nameDeepDecryptWithCipher = EncryptDecrypt.DeepDecryptWithCipher(nameDeepEncryptWithCipher, key, encryptionDepth);
Console.WriteLine($"Deep Decrypted {encryptionDepth} times using the cipher {{{cipherasString}}} {nameDeepDecryptWithCipher}");<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.12162083260349156, 'fraction_numerical': 0.015764582238570676, 'mean_word_length': 1.7321741904609986, 'pattern_counts': {'":': 0, '<': 72, '<?xml version=': 0, '>': 67, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '7', 'dir': 'jupyter-structured-clean-dedup', 'id': '524742', 'license': 'MIT', 'n_tokens_mistral': 4741, 'n_tokens_neox': 4398, 'n_words': 1190, 'path': 'Encoding C# code.ipynb', 'repo_name': 'Bin103/Encoding-repo-ASCII-', 'size': '17127'}