Binary to string/string to binary - c#

I want to convert text into binary and after that, to try convert binary into string text.
How can i convert tobin back into text if it is already a string?
private void iTalk_Button_12_Click(object sender, EventArgs e)
{
ambiance_RichTextBox2.Text = tobin(ambiance_RichTextBox1.Text);
}
public string tobin(string inp)
{
StringBuilder sb = new StringBuilder();
foreach (char L in inp.ToCharArray())
{
sb.Append(Convert.ToString(L, 2).PadLeft(8, '0'));
}
return sb.ToString();
}

private void iTalk_Button_12_Click(object sender, EventArgs e)
{
ambiance_RichTextBox2.Text = BinaryToString(ambiance_RichTextBox1.Text);
//use what u need: BinaryToString or StringToBinary.
}
Convert String to Binary:
public static string StringToBinary(string data)
{
StringBuilder sb = new StringBuilder();
foreach (char c in data.ToCharArray())
{
sb.Append(Convert.ToString(c, 2).PadLeft(8, '0'));
}
return sb.ToString();
}
Convert Binary to String:
public static string BinaryToString(string data)
{
List<Byte> byteList = new List<Byte>();
for (int i = 0; i < data.Length; i += 8)
{
byteList.Add(Convert.ToByte(data.Substring(i, 8), 2));
}
return Encoding.ASCII.GetString(byteList.ToArray());
}
Good luck!

You can use like this,
public static string BinaryToString(string data)
{
List<Byte> byteList = new List<Byte>();
for (int i = 0; i < data.Length; i += 8)
{
byteList.Add(Convert.ToByte(data.Substring(i, 8), 2));
}
return Encoding.ASCII.GetString(byteList.ToArray());
}

Currently you are converting a char (which can be represented as a number) to its binary representation (this number is the ASCII number). But if you want to convert a string to binary, you should use an encoding. The encoding determines how the text is converted to binary.
For example:
static void Main(string[] args)
{
string input = "This is an example text.";
Console.WriteLine(input);
string asBin = ToBinary(input);
Console.WriteLine(asBin);
string asText = ToText(asBin);
Console.WriteLine(asText);
}
static string ToBinary(string input, System.Text.Encoding encoding = null)
{
if (encoding == null)
encoding = System.Text.Encoding.UTF8;
var builder = new System.Text.StringBuilder();
var bytes = encoding.GetBytes(input); // Convert the text to bytes using the encoding
foreach (var b in bytes)
builder.Append(Convert.ToString(b, 2).PadLeft(8, '0')); //Convert the byte to its binary representation
return builder.ToString();
}
static string ToText(string bytes, System.Text.Encoding encoding = null)
{
if (encoding == null)
encoding = System.Text.Encoding.UTF8;
var byteCount = 8;
var byteArray = new byte[bytes.Length / 8]; // An array for the bytes
for (int i = 0; i < bytes.Length / byteCount; i++)
{
var subBytes = bytes.Substring(i * byteCount, byteCount); // Get a subpart of 8 bits
var b = Convert.ToByte(subBytes.TrimStart('0'), 2); // Convert the subpart to a byte
byteArray[i] = b; // Add the byte to the array
}
return encoding.GetString(byteArray); // Convert the array to text using the right encoding.
}
Now if you want to use ASCII encoding, you can call the functions as follows:
Console.WriteLine(input);
string asBin = ToBinary(input, System.Text.Encoding.ASCII);
Console.WriteLine(asBin);
string asText = ToText(asBin, System.Text.Encoding.ASCII);
Console.WriteLine(asText);

To convert a string to binary
string s = "hai";
byte []arr = System.Text.Encoding.ASCII.GetBytes(s);
To convert binary to string
byte[] arr ;
string s = Encoding.ASCII.GetString(arr);

Related

My symmetric encryption adds data to my rsa key I want to save and load encrypted

Currently I am trying to implement a save function for my RSA key with the help of bouncycastle. I am running into problems if I try to save my public or private key encrypted and load it afterwards.
As a little example here the original public key:
305C300D06092A864886F70D0101010500034B00304802410096B4751049165D1E046063EA22E8FFA0F90AE1DD997A3876DA5F79C7DE97951F009AC9ACA3EB91114F8A32C04F48293B6665CD6DD5C406C81CD13270A2AB61130203010001
What I get after loading it (it adds 4 zeroes, bigger key means more zeroes added):
305C300D06092A864886F70D0101010500034B00304802410096B4751049165D1E046063EA22E8FFA0F90AE1DD997A3876DA5F79C7DE97951F009AC9ACA3EB91114F8A32C04F48293B6665CD6DD5C406C81CD13270A2AB611302030100010000
I found out it has something to do with my implementation of the symmetric encryption and the padding used there. Normal text no matter how long it is just works fine without extra data getting added.
This is the code I am using for my AES encryption:
Encryption
byte[] outputBytes = new byte[0];
AesEngine aesengine = new AesEngine();
CbcBlockCipher aesblockCipher = new CbcBlockCipher(aesengine);
PaddedBufferedBlockCipher aescipher = new PaddedBufferedBlockCipher(aesblockCipher);
KeyParameter aeskeyParameter = new KeyParameter(Hash.HashDataBlock(password, Hash.HashAlgorithm.SHA3).Bytes);
aescipher.Init(true, aeskeyParameter);
outputBytes = new byte[aescipher.GetOutputSize(inputBytes.Bytes.Length)];
int aeslength = aescipher.ProcessBytes(inputBytes.Bytes, outputBytes, 0);
aescipher.DoFinal(outputBytes, aeslength);
Decryption
byte[] inputBytes = input.Bytes;
byte[] outputBytes = new byte[0];
AesEngine aesengine = new AesEngine();
CbcBlockCipher aesblockCipher = new CbcBlockCipher(aesengine);
PaddedBufferedBlockCipher aescipher = new PaddedBufferedBlockCipher(aesblockCipher);
KeyParameter aeskeyParameter = new KeyParameter(Hash.HashDataBlock(password, Hash.HashAlgorithm.SHA3).Bytes);
aescipher.Init(false, aeskeyParameter);
outputBytes = new byte[aescipher.GetOutputSize(inputBytes.Length)];
int aeslength = aescipher.ProcessBytes(inputBytes, outputBytes, 0);
aescipher.DoFinal(outputBytes, aeslength);
My Functions to save and load the keys. The DataBlock class just converts data to needed formats like UTF8, Base64 or just byte arrays:
public static void SaveKeyEncrypted(DataBlock key, string path, DataBlock password)
{
StreamWriter sw = new StreamWriter(path);
DataBlock encrypted = SymmetricEncryption.Encrypt(key, password, SymmetricEncryption.SymmetricAlgorithms.AES);
sw.Write(encrypted.Base64);
sw.Close();
}
public static DataBlock ReadKeyEncrypted(string path, DataBlock password)
{
StreamReader sr = new StreamReader(path);
DataBlock readData = new DataBlock(sr.ReadLine(), DataBlock.DataType.Base64);
sr.Close();
return SymmetricEncryption.Decrypt(readData, password, SymmetricEncryption.SymmetricAlgorithms.AES);
}
For reproduction my other code that has to do with this problem:
public class DataBlock
{
private byte[] _data;
public DataBlock()
{
this._data = new byte[0];
}
public enum DataType
{
UTF8,
UTF7,
UTF32,
ASCII,
Unicode,
Hex,
Base64,
Base32
}
public DataBlock(string data, DataType dataType) : this()
{
switch (dataType)
{
case DataType.UTF8:
this._data = Encoding.UTF8.GetBytes(data);
break;
case DataType.UTF7:
this._data = Encoding.UTF7.GetBytes(data);
break;
case DataType.UTF32:
this._data = Encoding.UTF32.GetBytes(data);
break;
case DataType.ASCII:
this._data = Encoding.ASCII.GetBytes(data);
break;
case DataType.Unicode:
this._data = Encoding.Unicode.GetBytes(data);
break;
case DataType.Hex:
this._data = new byte[data.Length / 2];
for (int i = 0; i < data.Length; i += 2)
{
this._data[i / 2] = Convert.ToByte(data.Substring(i, 2), 16);
}
break;
case DataType.Base64:
this._data = Convert.FromBase64String(data);
break;
case DataType.Base32:
this._data = this.FromBase32String(data);
break;
}
}
public DataBlock(byte[] data)
{
this._data = data;
}
public string UTF8
{
get
{
return Encoding.UTF8.GetString(this._data);
}
}
public string UTF7
{
get
{
return Encoding.UTF7.GetString(this._data);
}
}
public string UTF32
{
get
{
return Encoding.UTF32.GetString(this._data);
}
}
public string ASCII
{
get
{
return Encoding.ASCII.GetString(this._data);
}
}
public string Unicode
{
get
{
return Encoding.Unicode.GetString(this._data);
}
}
public string Hex
{
get
{
return BitConverter.ToString(this._data).Replace("-", "");
}
}
public string Base64
{
get
{
return Convert.ToBase64String(this._data);
}
}
public string Base32
{
get
{
return this.ToBase32String(this._data);
}
}
public byte[] Bytes
{
get
{
return this._data;
}
}
private string ValidChars = "QAZ2WSX3" + "EDC4RFV5" + "TGB6YHN7" + "UJM8K9LP";
private string ToBase32String(byte[] bytes)
{
StringBuilder sb = new StringBuilder();
byte index;
int hi = 5;
int currentByte = 0;
while (currentByte < bytes.Length)
{
if (hi > 8)
{
index = (byte)(bytes[currentByte++] >> (hi - 5));
if (currentByte != bytes.Length)
{
index = (byte)(((byte)(bytes[currentByte] << (16 - hi)) >> 3) | index);
}
hi -= 3;
}
else if (hi == 8)
{
index = (byte)(bytes[currentByte++] >> 3);
hi -= 3;
}
else
{
index = (byte)((byte)(bytes[currentByte] << (8 - hi)) >> 3);
hi += 5;
}
sb.Append(ValidChars[index]);
}
return sb.ToString();
}
public byte[] FromBase32String(string str)
{
int numBytes = str.Length * 5 / 8;
byte[] bytes = new Byte[numBytes];
str = str.ToUpper();
int bit_buffer;
int currentCharIndex;
int bits_in_buffer;
if (str.Length < 3)
{
bytes[0] = (byte)(ValidChars.IndexOf(str[0]) | ValidChars.IndexOf(str[1]) << 5);
return bytes;
}
bit_buffer = (ValidChars.IndexOf(str[0]) | ValidChars.IndexOf(str[1]) << 5);
bits_in_buffer = 10;
currentCharIndex = 2;
for (int i = 0; i < bytes.Length; i++)
{
bytes[i] = (byte)bit_buffer;
bit_buffer >>= 8;
bits_in_buffer -= 8;
while (bits_in_buffer < 8 && currentCharIndex < str.Length)
{
bit_buffer |= ValidChars.IndexOf(str[currentCharIndex++]) << bits_in_buffer;
bits_in_buffer += 5;
}
}
return bytes;
}
}
Function to generate a keypair
public static DataBlock[] GenerateKeyPair(KeyPairSize keyPairSize)
{
RsaKeyPairGenerator keyPairGenerator = new RsaKeyPairGenerator();
keyPairGenerator.Init(new KeyGenerationParameters(new SecureRandom(new CryptoApiRandomGenerator()), (int) keyPairSize));
AsymmetricCipherKeyPair keyPair = keyPairGenerator.GenerateKeyPair();
PrivateKeyInfo pkInfo = PrivateKeyInfoFactory.CreatePrivateKeyInfo(keyPair.Private);
SubjectPublicKeyInfo info = SubjectPublicKeyInfoFactory.CreateSubjectPublicKeyInfo(keyPair.Public);
DataBlock[] keyPairData = new DataBlock[2];
keyPairData[0] = new DataBlock(pkInfo.GetDerEncoded());
keyPairData[1] = new DataBlock(info.GetDerEncoded());
return keyPairData;
}
Code to reproduce the error:
DataBlock[] keyPair = AsymmetricEncryption.GenerateKeyPair(AsymmetricEncryption.KeyPairSize.Bits512);
DataBlock pass = new DataBlock("1234", DataBlock.DataType.UTF8);
DataBlock orig = new DataBlock("Hello World", DataBlock.DataType.UTF8);
DataBlock encrypted = AsymmetricEncryption.Encrypt(orig, keyPair[1]);
AsymmetricEncryption.SaveKeyEncrypted(keyPair[0], "D:\\privateenc", pass);
AsymmetricEncryption.SaveKeyEncrypted(keyPair[1], "D:\\publicenc", pass);
DataBlock privateKey = AsymmetricEncryption.ReadKeyEncrypted("D:\\privateenc", pass);
DataBlock publicKey = AsymmetricEncryption.ReadKeyEncrypted("D:\\publicenc", pass);
DataBlock decrypted = AsymmetricEncryption.Decrypt(encrypted, privateKey);
Console.WriteLine(decrypted.UTF8);
The encryption/decryption method is not needed because the error already happens after reading the encrypted key on my harddrive.
Why/where is the extra data added and how can I fix it?
I was able to fix it by adding the initial byte array length of the key to the encrypted text and read it later on. In the read function I cut everything after the original size of the key.
The main problem is still present and this is just a workaround.

Convert little endian to big endian in c#

I want to convert string "8BABEEF9D2472E65" to big endian.
I already input in this function, I receive UINT Size error.. How can i do?
Function :
string bigToLittle(string data)
{
int number = Convert.ToInt32(data, 16);
byte[] bytes = BitConverter.GetBytes(number);
string retval = "";
foreach (byte b in bytes)
retval += b.ToString("X2");
return retval;
}
I think you can use System.Runtime.Remoting.Metadata.W3cXsd2001.SoapHexBinary class.
string input = "8BABEEF9D2472E65";
var output = new SoapHexBinary(SoapHexBinary.Parse(input).Value.Reverse().ToArray())
.ToString();
Output: 652E47D2F9EEAB8B
Or Maybe
var output = IPAddress.HostToNetworkOrder(long.Parse(input, NumberStyles.HexNumber))
.ToString("X");
As we can not guess what exacly you want as output I let myself write missing possibilities(in code comments)... you can choose which you trully need:
internal class Program
{
private static int ReverseBytes(long val)
{
byte[] intAsBytes = BitConverter.GetBytes(val);
Array.Reverse(intAsBytes);
return BitConverter.ToInt32(intAsBytes, 0);
}
private static string IntToBinaryString(long v)
{
string s = Convert.ToString(v, 2);
string t = s.PadLeft(32, '0');
string res = "";
for (int i = 0; i < t.Length; ++i)
{
if (i > 0 && i%8 == 0)
res += " ";
res += t[i];
}
return res;
}
private static void Main(string[] args)
{
string sValue = "8BABEEF9D2472E65";
long sValueAsInt = long.Parse(sValue, System.Globalization.NumberStyles.HexNumber);
//output {-8382343524677898651}
string sValueAsStringAgain = IntToBinaryString(sValueAsInt);
//output {10001011 10101011 11101110 11111001 11010010 01000111 00101110 01100101}
byte[] data = Encoding.BigEndianUnicode.GetBytes(sValue);
string decodedX = Encoding.BigEndianUnicode.GetString(data);
string retval = data.Aggregate("", (current, b) => current + b.ToString("X2"));
//output {0038004200410042004500450046003900440032003400370032004500360035}
char[] decodedX2 = Encoding.BigEndianUnicode.GetString(data).Reverse().ToArray();
StringBuilder retval2 = new StringBuilder(); //output {56E2742D9FEEBAB8}
foreach (var b in decodedX2)
retval2.Append(b);
Console.ReadLine();
}
}
}
and bout yours Method:
public static string bigToLittle(string data)
{
long sValueAsInt = long.Parse(data, System.Globalization.NumberStyles.HexNumber);
byte[] bytes = BitConverter.GetBytes(sValueAsInt);
string retval = "";
foreach (byte b in bytes)
retval += b.ToString("X2");
return retval; //output {652E47D2F9EEAB8B}
}
I liked #Sebatsian's answer but I ran into issues with the entry being padded by extra zeroes, so I modified it slightly, in case you are doing something like converting Linux VMUUID's in Azure over to standard format. I made use of IPAddress.HostToNetworkOrder, which handles breaking your long into bytes and reversing them.
private string BigToLittle(string data)
{
long sValueAsInt = long.Parse(data, System.Globalization.NumberStyles.HexNumber);
var length = data.Length;
return IPAddress.HostToNetworkOrder(sValueAsInt).ToString("X2").Substring(0, length);
}
In usage:
string uuid = "090556DA-D4FA-764F-A9F1-63614EDA0163";
private string BigToLittle(string data)
{
long sValueAsInt = long.Parse(data, System.Globalization.NumberStyles.HexNumber);
var length = data.Length;
return IPAddress.HostToNetworkOrder(sValueAsInt).ToString("X2").Substring(0, length);
}
var elements = uuid.Split(new char[] { '-' });
var outMe = new List<string> { };
foreach (var item in elements)
{
outMe.Add(BigToLittle(item));
}
var output = String.Join("-", outMe);
//DA560509-FAD4-4F76-F1A9-6301DA4E6163

Converting a String with plain text to byte array in hex style?

i can find a lot of answer about how to convert a string in hex format to a hex byte array but I would like to know how I can convert a string with text to a byte array.
To give you an idea, here's the code for converting text into a byte array using hex format:
FileStream fs = File.OpenRead(filePath);
byte[] fileInBytes;
using (BinaryReader br = new BinaryReader(fs))
{
List<byte> bytesList = new List<byte>();
while (fs.Position < fs.Length)
{
bytesList.Add(byte.Parse(Encoding.ASCII.GetString(br.ReadBytes(2)),
NumberStyles.HexNumber));
}
fileInBytes = bytesList.ToArray();
}
return fileInBytes;
How can I achieve this using a String?
public static byte[] getBytesFromString(String str)
{
//What now?
}
Basically if I enter a string that has 16 characters, I would like to return a byte array of 8 bytes.
I'm not sure about the number of bytes you would get back, but see below.
public static byte[] getBytesFromString(String str)
{
return Encoding.ASCII.GetBytes(str)
}
If I've understand what you meam your code should look like:
public byte[] getBytesFromString2(string str)
{
IList<byte> retValue = null;
if (!string.IsNullOrEmpty(str) && str.Length == 16)
{
MemoryStream s_stream;
using (s_stream = new MemoryStream(Encoding.ASCII.GetBytes(str)))
{
using (var br = new BinaryReader(s_stream))
{
retValue = new List<byte>();
while (s_stream.Position < s_stream.Length)
{
retValue.Add(byte.Parse(Encoding.ASCII.GetString(br.ReadBytes(2)),
System.Globalization.NumberStyles.HexNumber));
}
}
}
}
return retValue.ToArray();
}

Binary To Corresponding ASCII String Conversion

Hi i was able to convert a ASCII string to binary using a binarywriter .. as 10101011 . im required back to convert Binary ---> ASCII string .. any idea how to do it ?
This should do the trick... or at least get you started...
public Byte[] GetBytesFromBinaryString(String binary)
{
var list = new List<Byte>();
for (int i = 0; i < binary.Length; i += 8)
{
String t = binary.Substring(i, 8);
list.Add(Convert.ToByte(t, 2));
}
return list.ToArray();
}
Once the binary string has been converted to a byte array, finish off with
Encoding.ASCII.GetString(data);
So...
var data = GetBytesFromBinaryString("010000010100001001000011");
var text = Encoding.ASCII.GetString(data);
If you have ASCII charters only you could use Encoding.ASCII.GetBytes and Encoding.ASCII.GetString.
var text = "Test";
var bytes = Encoding.ASCII.GetBytes(text);
var newText = Encoding.ASCII.GetString(bytes);
Here is complete code for your answer
FileStream iFile = new FileStream(#"c:\test\binary.dat",
FileMode.Open);
long lengthInBytes = iFile.Length;
BinaryReader bin = new BinaryReader(aFile);
byte[] byteArray = bin.ReadBytes((int)lengthInBytes);
System.Text.Encoding encEncoder = System.Text.ASCIIEncoding.ASCII;
string str = encEncoder.GetString(byteArray);
Take this as a simple example:
public void ByteToString()
{
Byte[] arrByte = { 0, 1, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0 };
string x = Convert.ToBase64String(arrByte);
}
This linked answer has interesting details about this kind of conversion:
binary file to string
Sometimes instead of using the built in tools it's better to use "custom" code.. try this function:
public string BinaryToString(string binary)
{
if (string.IsNullOrEmpty(binary))
throw new ArgumentNullException("binary");
if ((binary.Length % 8) != 0)
throw new ArgumentException("Binary string invalid (must divide by 8)", "binary");
StringBuilder builder = new StringBuilder();
for (int i = 0; i < binary.Length; i += 8)
{
string section = binary.Substring(i, 8);
int ascii = 0;
try
{
ascii = Convert.ToInt32(section, 2);
}
catch
{
throw new ArgumentException("Binary string contains invalid section: " + section, "binary");
}
builder.Append((char)ascii);
}
return builder.ToString();
}
Tested with 010000010100001001000011 it returned ABC using the "raw" ASCII values.

Convert PHP encryption code to C#

I'm trying to convert this piece of code from PHP to C#. It's part of a Captive Portal. Could somebody explain what it does?
$hexchal = pack ("H32", $challenge);
if ($uamsecret) {
$newchal = pack ("H*", md5($hexchal . $uamsecret));
} else {
$newchal = $hexchal;
}
$response = md5("\0" . $password . $newchal);
$newpwd = pack("a32", $password);
$pappassword = implode ("", unpack("H32", ($newpwd ^ $newchal)));
I also encountered the need of php's pack-unpack functions in c# but did not get any good resource.
So i thought to do it myself. I have verified the function's input with pack/unpack/md5 methods found at onlinephpfunctions.com. Since i have done code only as per my requirements. This can be extended for other formats
Pack
private static string pack(string input)
{
//only for H32 & H*
return Encoding.Default.GetString(FromHex(input));
}
public static byte[] FromHex(string hex)
{
hex = hex.Replace("-", "");
byte[] raw = new byte[hex.Length / 2];
for (int i = 0; i < raw.Length; i++)
{
raw[i] = Convert.ToByte(hex.Substring(i * 2, 2), 16);
}
return raw;
}
MD5
private static string md5(string input)
{
byte[] asciiBytes = Encoding.Default.GetBytes(input);
byte[] hashedBytes = MD5CryptoServiceProvider.Create().ComputeHash(asciiBytes);
string hashedString = BitConverter.ToString(hashedBytes).Replace("-", "").ToLower();
return hashedString;
}
Unpack
private static string unpack(string p1, string input)
{
StringBuilder output = new StringBuilder();
for (int i = 0; i < input.Length; i++)
{
string a = Convert.ToInt32(input[i]).ToString("X");
output.Append(a);
}
return output.ToString();
}
Eduardo,
if you take a look at the pack manual, pack is used to convert a string in (hex, octal, binary )to his number representation.
so
$hexcal = pack('H32', $challenge);
would convert a string like 'cca86bc64ec5889345c4c3d8dfc7ade9' to the actual 0xcca... de9
if $uamsecret exist do the same things with the MD5 of hexchal concacteate with the uamsecret.
if ($uamsecret) {
$newchal = pack ("H*", md5($hexchal . $uamsecret));
} else {
$newchal = $hexchal;
}
$response = md5("\0" . $password . $newchal);
MD% '\0' + $password + $newchal
$newpwd = pack("a32", $password);
pad password to 32 byte
$pappassword = implode ("", unpack("H32", ($newpwd ^ $newchal)));
do a xor newpwd and newchal and convert it to a hexadecimal string, I don't get the implode() maybe it's to convert to string to an array of character.

Categories

Resources