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.
RijndaelManaged, it should be 16-bytes (128-bits) for AES.string str =Encoding.UTF7.GetString(encrypted);on the random binary data thatencryptStringToBytes_AESwill be outputting is not garunteed to work. You should never callGetStringon bytes that do not represent a string.