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


namespace DecryptionToEncryption
{
    class Program
    {
        static void Main(string[] args)
        {
            //Create byte arrays to hold original, encrypted, and decrypted data.
            **byte[] encryptedstring = {0x7B,0x35,0x30,0x36,0x30,0x32,0x36,0x30,0x34,0x7C,0x55,0x38,0x30,0x30,0x45,0x44,0x45,0x37,0x33,0x46,0x32,0x34,0x31,0x41,0x43,0x32,0x45,0x35,0x38,0x41,0x37,0x44,0x37,0x34,0x43,0x38,0x37,0x39,0x44,0x31,0x44,0x37,0x37,0x7C,0x34,0x44,0x42,0x36,0x43,0x34,0x7D};**
            string data = Encoding.UTF7.GetString(encryptedstring);

            byte[] key = new byte[16];
            for (int i = 0; i < 16; ++i)
            {
                key[i] = 1;
            }

            byte[] iv = new byte[16];
            for (int i = 0; i < 16; ++i)
            {
                iv[i] = 1;
            }


            RijndaelManaged myRijndael = new RijndaelManaged();

            myRijndael.Key = key;
            myRijndael.IV = iv;
            byte[] encrypted = encryptStringToBytes_AES(data, myRijndael.Key, myRijndael.IV);
            string str =Encoding.UTF7.GetString(encrypted);
            char[] charValues = str.ToCharArray();
            string hexOutput = "";
            foreach (char _eachChar in charValues)
            {
                // Get the integral value of the character.
                int value = Convert.ToInt32(_eachChar);
                // Convert the decimal value to a hexadecimal value in string form.
                hexOutput += String.Format("{0,10:X}", value);
                // to make output as your eg 
                //  hexOutput +=" "+ String.Format("{0:X}", value);

            }


            Console.WriteLine(hexOutput);


            Console.ReadLine();

            // sends the byte array via active tcp connection
          //  _transport.SendEncryptedData(encrypted);
        }



        static byte[] encryptStringToBytes_AES(string plainText, byte[] Key, byte[] IV)
        {
            // Check arguments.
            if (plainText == null || plainText.Length <= 0)
                throw new ArgumentNullException("plainText");
            if (Key == null || Key.Length <= 0)
                throw new ArgumentNullException("Key");
            if (IV == null || IV.Length <= 0)
                throw new ArgumentNullException("IV");

            // Declare the stream used to encrypt to an in memory
            // array of bytes.
            MemoryStream msEncrypt = null;

            // Declare the RijndaelManaged object
            // used to encrypt the data.
            RijndaelManaged aesAlg = null;

            try
            {
                // Create a RijndaelManaged object
                // with the specified key and IV.
                aesAlg = new RijndaelManaged();
                aesAlg.Key = Key;
                aesAlg.IV = IV;

                // Create an encrypto to perform the stream transform.
                ICryptoTransform encryptor = aesAlg.CreateEncryptor(aesAlg.Key, aesAlg.IV);

                // Create the streams used for encryption.
                msEncrypt = new MemoryStream();
                using (CryptoStream csEncrypt = new CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write))
                {
                    using (StreamWriter swEncrypt = new StreamWriter(csEncrypt))
                    {
                        //Write all data to the stream.
                        swEncrypt.Write(plainText);

                    }
                }
            }

            finally
            {
                // Clear the RijndaelManaged object.
                if (aesAlg != null)
                    aesAlg.Clear();enter code here
            }

            // Return the encrypted bytes from the memory stream.
            return msEncrypt.ToArray();
        }
    }
}

i am running this c# code to get a encryption using AES/CBC. my input is given below.and my output is as: 36 E7 78 F8 B8 97 15 6C C3 73 EA A8 1B 12 71 C2 A0 5A F2 40 60 89 6B 8 70 90 C9 B6 75 57 F7 22 73 3D 15 AB B1 D5 E5 73 85 E A5 7E A9 D2 7C F2 48 C9 2D DF 6A 4E CA CB 31 AC D1 2 B2 C3 DB 89

But in Online Tool it is showing some differently...(last line is different from mine output): 36 e7 78 f8 b8 97 15 6c c3 73 ea a8 1b 12 71 c2 a0 5a f2 40 60 89 6b 08 70 90 c9 b6 75 57 f7 22 73 3d 15 ab b1 d5 e5 73 85 0e a5 7e a9 d2 7c f2 ff aa 96 0e 3e f6 aa 0c 7e c5 15 2e 97 2f fd be

please help me out to get same output as shown in online tool.

4
  • Question Review : Provide the community with the details of the online tool you are using. Commented Aug 10, 2016 at 13:59
  • Provide the encrypted data. It is best to provide the block size with RijndaelManaged, it should be 16-bytes (128-bits) for AES. Commented Aug 10, 2016 at 15:27
  • Doing string str =Encoding.UTF7.GetString(encrypted); on the random binary data that encryptStringToBytes_AES will be outputting is not garunteed to work. You should never call GetString on bytes that do not represent a string. Commented Aug 10, 2016 at 15:57
  • Do you really mean UTF-7 and not UTF-8? Are you sure this is AES encrypted data? Commented Aug 10, 2016 at 16:33

2 Answers 2

2

The encrypted string is 51 character long.

byte[] encryptedstring = {0x7B,0x35,0x30,0x36,0x30,0x32,0x36,0x30,0x34,0x7C,0x55,0x38,0x30,0x30,0x45,0x44,0x45,0x37,0x33,0x46,0x32,0x34,0x31,0x41,0x43,0x32,0x45,0x35,0x38,0x41,0x37,0x44,0x37,0x34,0x43,0x38,0x37,0x39,0x44,0x31,0x44,0x37,0x37,0x7C,0x34,0x44,0x42,0x36,0x43,0x34,0x7D};

That is not a possible length for CBC mode.

AES is a block cipher that works with blocks of data in most modes including CBC.

Input to decryption that is to short will result in the decryption code padding the missing bytes with something, probably whatever garbage follows. Since this is the last block the decryptions will probably be the different for different implementations.

But looking at the data as a UTF-8 string it is:

"{50602604|U800EDE73F241AC2E58A7D74C879D1D77|4DB6C4}"

which is not what one would expect from encrypted data which should appear with no patterns and look like random bytes.

It looks like formatted data with three components:

50602604
U800EDE73F241AC2E58A7D74C879D1D77
4DB6C4

With the first character of the second component standing out as the only non-hexadecimal character so it may be an indicator for the remaining 32 characters.

No wonder it will not decrypt properly.

Sign up to request clarification or add additional context in comments.

9 Comments

@zaph...I agreed with you.bt the ancryptrd string is coming as a length of 51..I was doing padding to have total 64 length.then also otpyt was not giving properly.
I have used cryptomath aes calculator .where I am adding key in hex.like given below and for iv
But what makes you think the data is AES encrypted, it does not look like encrypted data as a whole. Why are you using UTF-7, that is rather non-standard.
It is my assignment.. And the input wch is converted into hex is given to me as it is.I did both method one by padding 0(30 in hex)and 2nd without padding in input string.I am using utf-7 because I have tried all methods like utf-8,16,default,Unicode.using utf-7 my half output is at least coming. I have compared with online encryption aes tool
What exactly I have to do ? To get same output.because I cannot change my input text.
|
0
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Security.Cryptography;
using System.IO;
using System.ComponentModel;

namespace final_encryption
{
    class Program
    {
        static void Main(string[] args)
        {
            try
            {

                string original = "{50602604|U800EDE73F241AC2E58A7D74C879D1D77|4DB6C4}"; //"Here is some data to encrypt!";
                Console.WriteLine("Original:   " + original);
                Console.ReadLine();

                // Create a new instance of the RijndaelManaged
                // class.  This generates a new key and initialization 
                // vector (IV).
                byte[] key = new byte[16];
                for (int i = 0; i < 16; ++i)
                {
                    key[i] = 1;
                }

                byte[] iv = new byte[16];
                for (int i = 0; i < 16; ++i)
                {
                    iv[i] = 1;
                }
                using (RijndaelManaged myRijndael = new RijndaelManaged())
                {
                    // myRijndael.GenerateKey();
                    //myRijndael.GenerateIV();
                    myRijndael.Key = key;
                    myRijndael.IV = iv;
                    // Encrypt the string to an array of bytes.
                    byte[] encrypted = EncryptStringToBytes(original, myRijndael.Key, myRijndael.IV);

                    StringBuilder s = new StringBuilder();
                    foreach (byte item in encrypted)
                    {
                        s.Append(item.ToString("X2") + " ");
                    }
                    Console.WriteLine("Encrypted:   " + s);
                    Console.ReadLine();

                    // Decrypt the bytes to a string.
                    string decrypted = DecryptStringFromBytes(encrypted, myRijndael.Key, myRijndael.IV);

                    //Display the original data and the decrypted data.
                    Console.WriteLine("Decrypted:    " + decrypted);
                    Console.ReadLine();
                }

            }
            catch (Exception ex)
            {
                Console.WriteLine("Error: {0}", ex.Message);
                Console.ReadLine();

            }
        }
        static byte[] EncryptStringToBytes(string plainText, byte[] Key, byte[] IV)
        {
            // Check arguments.
            if (plainText == null || plainText.Length <= 0)
                throw new ArgumentNullException("plainText");
            if (Key == null || Key.Length <= 0)
                throw new ArgumentNullException("Key");
            if (IV == null || IV.Length <= 0)
                throw new ArgumentNullException("Key");
            byte[] encrypted;
            // Create an RijndaelManaged object
            // with the specified key and IV.
            using (RijndaelManaged rijAlg = new RijndaelManaged())
            {
                rijAlg.Key = Key;
                rijAlg.IV = IV;
                rijAlg.Mode = CipherMode.CBC;
                rijAlg.Padding = PaddingMode.Zeros;

                // Create a decrytor to perform the stream transform.
                ICryptoTransform encryptor = rijAlg.CreateEncryptor(rijAlg.Key, rijAlg.IV);

                // Create the streams used for encryption.
                using (MemoryStream msEncrypt = new MemoryStream())
                {
                    using (CryptoStream csEncrypt = new CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write))
                    {
                        using (StreamWriter swEncrypt = new StreamWriter(csEncrypt))
                        {



                            //Write all data to the stream.

                            swEncrypt.Write(plainText);
                        }
                        encrypted = msEncrypt.ToArray();
                    }
                }
            }


            // Return the encrypted bytes from the memory stream.
            return encrypted;

        }

        static string DecryptStringFromBytes(byte[] cipherText, byte[] Key, byte[] IV)
        {
            // Check arguments.
            if (cipherText == null || cipherText.Length <= 0)
                throw new ArgumentNullException("cipherText");
            if (Key == null || Key.Length <= 0)
                throw new ArgumentNullException("Key");
            if (IV == null || IV.Length <= 0)
                throw new ArgumentNullException("Key");

            // Declare the string used to hold
            // the decrypted text.
            string plaintext = null;

            // Create an RijndaelManaged object
            // with the specified key and IV.
            using (RijndaelManaged rijAlg = new RijndaelManaged())
            {
                rijAlg.Key = Key;
                rijAlg.IV = IV;
                rijAlg.Mode = CipherMode.CBC;
                rijAlg.Padding = PaddingMode.Zeros;

                // Create a decrytor to perform the stream transform.
                ICryptoTransform decryptor = rijAlg.CreateDecryptor(rijAlg.Key, rijAlg.IV);

                // Create the streams used for decryption.
                using (MemoryStream msDecrypt = new MemoryStream(cipherText))
                {
                    using (CryptoStream csDecrypt = new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read))
                    {
                        using (StreamReader srDecrypt = new StreamReader(csDecrypt))
                        {

                            // Read the decrypted bytes from the decrypting stream
                            // and place them in a string.
                            plaintext = srDecrypt.ReadToEnd();
                        }
                    }
                }

            }

            return plaintext;
        }
    }
}

i solved the problem which is now showing proper answer with online tool.

MY output: 36 E7 78 F8 B8 97 15 6C C3 73 EA A8 1B 12 71 C2 A0 5A F2 40 60 89 6B 08 70 90 C9 B6 75 57 F7 22 73 3D 15 AB B1 D5 E5 73 85 0E A5 7E A9 D2 7C F2 FF AA 96 0E 3E F6 AA 0C 7E C5 15 2E 97 2F FD BE

2 Comments

This is an interesting and unexpected decryption question. Essentially it is decrypting data that is not encrypted as an assignment.
Thanks All for all conversation s regarding my question

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.