How to convert this PHP function to C#? - c#

I'd like to convert this function:
function DoHashPassword($username, $clear_password)
{
return strtoupper(bin2hex(strrev(hex2bin(strtoupper(hash("sha256",strtoupper(hash("sha256", strtoupper($username)).":".strtoupper($clear_password))))))));
}
to C# language.
How can I do that?
At the moment I'm at this point:
Remaining (in PHP)
strtoupper(bin2hex(strrev(hex2bin($password))));
What I've done (in C#)
public static string DoShaHashPassword256(string _email, string _password)
{
byte[] emailbyte = Encoding.ASCII.GetBytes(_email.ToUpper());
var sha_email = SHA256.Create();
byte[] bytehashemail = sha_email.ComputeHash(emailbyte);
_email = HexStringFromBytes(bytehashemail);
//now with password
byte[] passbyte = Encoding.ASCII.GetBytes(_email.ToUpper() + ":" + _password.ToUpper());
var sha_pass = SHA256.Create();
byte[] bytehashpass = sha_pass.ComputeHash(passbyte);
_password = HexStringFromBytes(bytehashpass).ToUpper();
return /* hashed password */
}
private static string HexStringFromBytes(byte[] bytes)
{
var sb = new StringBuilder();
foreach (byte b in bytes)
{
var hex = b.ToString("x2");
sb.Append(hex);
}
return sb.ToString();
}
I don't know how to go on now.

This is the answer to the question. Thanks for your bad votes to my question (you should help, not give bad vote and no answer.).
public static string DoShaHashPassword256(string _email, string _password)
{
byte[] emailbyte = Encoding.ASCII.GetBytes(_email.ToUpper());
var sha_email = SHA256.Create();
byte[] bytehashemail = sha_email.ComputeHash(emailbyte);
_email = HexStringFromBytes(bytehashemail);
//now with password
byte[] passbyte = Encoding.ASCII.GetBytes(_email.ToUpper() + ":" + _password.ToUpper());
var sha_pass = SHA256.Create();
byte[] bytehashpass = sha_pass.ComputeHash(passbyte);
_password = HexStringFromBytes(bytehashpass).ToUpper();
//hex2bin
var bindata = hex2bin(_password);
//strrev
char[] chararray = bindata.ToCharArray();
Array.Reverse(chararray);
var reversedstring = new string(chararray);
//bin2hex
byte[] bytes = Encoding.GetEncoding(1252).GetBytes(reversedstring);
string hexString = HexStringFromBytes(bytes);
return hexString.ToUpper();
}
private static string HexStringFromBytes(byte[] bytes)
{
var sb = new StringBuilder();
foreach (byte b in bytes)
{
var hex = b.ToString("x2");
sb.Append(hex);
}
return sb.ToString();
}
private static string hex2bin(string hexdata)
{
if (hexdata == null)
throw new ArgumentNullException("hexdata");
if (hexdata.Length % 2 != 0)
throw new ArgumentException("hexdata should have even length");
byte[] bytes = new byte[hexdata.Length / 2];
for (int i = 0; i < hexdata.Length; i += 2)
bytes[i / 2] = (byte)(HexValue(hexdata[i]) * 0x10
+ HexValue(hexdata[i + 1]));
return Encoding.GetEncoding(1252).GetString(bytes);
}
private static int HexValue(char c)
{
int ch = (int)c;
if (ch >= (int)'0' && ch <= (int)'9')
return ch - (int)'0';
if (ch >= (int)'a' && ch <= (int)'f')
return ch - (int)'a' + 10;
if (ch >= (int)'A' && ch <= (int)'F')
return ch - (int)'A' + 10;
throw new ArgumentException("Not a hexadecimal digit.");
}

Related

C# RNGCryptoServiceProvider and special characters

I'm looking for a way to get random characters.I need a string must be contain at 2 least uppercase letters, at least 1 number and special characters.
Here is my code:
public static string CreateRandomPassword(int Length)
{
string _Chars = "abcdefghijkmnopqrstuvwxyzABCDEFGHJKLMNOPQRSTUVWXYZ[_!23456790";
Byte[] randomBytes = new Byte[Length];
var rng = new RNGCryptoServiceProvider();
rng.GetBytes(randomBytes);
var chars = new char[Length];
int Count = _Chars.Length;
for(int i = 0;i<Length;i++)
{
chars[i] = _Chars[(int)randomBytes[i] % Count];
}
return new string(chars);
}
some results:
ZNQzvUPFKOL3x
BQSEkKHXACGO
They haven't special characters and numbers.
your code works great! I've just wrapped it with a function that validate your conditions.
I've executed the following:
public static string CreateRandomPassword(int Length)
{
string _Chars = "abcdefghijkmnopqrstuvwxyzABCDEFGHJKLMNOPQRSTUVWXYZ[_!23456790";
Byte[] randomBytes = new Byte[Length];
var rng = new RNGCryptoServiceProvider();
rng.GetBytes(randomBytes);
var chars = new char[Length];
int Count = _Chars.Length;
for (int i = 0; i < Length; i++)
{
chars[i] = _Chars[(int)randomBytes[i] % Count];
}
return new string(chars);
}
public static string CreateRandomPasswordWith2UpperAnd1NumberAnd1Special(int length)
{
while (true)
{
var pass = CreateRandomPassword(length);
int upper=0, num =0, special = 0,lower=0;
foreach (var c in pass)
{
if (c > 'A' && c < 'Z')
{
upper++;
}
else if (c > 'a' && c < 'z')
{
lower++;
}
else if (c > '0' && c < '9')
{
num++;
}
else
{
special++;
}
}
if (upper>=2&&num>=1&&1>=special)
{
return pass;
}
}
}
[Test]
public void CreateRandomPassword_Length13_RandomPasswordWithNumbers()
{
var random = CreateRandomPasswordWith2UpperAnd1NumberAnd1Special(13);
Assert.IsTrue(true);
}

How to add chars numerically in c#

So I'm building a program that takes a string and turns it into a binary number (I.E. "A" = 01000001). Then, if the user wishes, it can that binary number convert it back into a ascii character. Here the code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Threading.Tasks;
namespace NDR_011
{
class Program
{
public static Byte[] BinStr(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();
}
public static void Print(object mess)
{
string tmp = mess.ToString().ToUpper();
Console.Write(tmp);
}
private static List<string> buffer = new List<string>();
private static string outfile = "C:/tmp/bytes.bin";
static void Main(string[] args)
{
string tmp = "";
Print("NDR 011\n");
while (true)
{
Print(""); tmp = Console.ReadKey().Key.ToString().ToUpper();
if (Console.CursorLeft > 0)
{
Console.SetCursorPosition(Console.CursorLeft - 1, Console.CursorTop);
}
if (tmp == ConsoleKey.F1.ToString())
{
break;
} else if (tmp == ConsoleKey.F2.ToString())
{
comp();
continue;
} else if (tmp == ConsoleKey.F4.ToString())
{
buffer.Clear();
continue;
} else if (tmp == ConsoleKey.F5.ToString())
{
Print("N "); string a = Console.ReadLine();
outfile = a;
continue;
} else if (tmp == ConsoleKey.F5.ToString())
{
outfile = "C:/tmp/bytes.bin";
Print("Out file reset\n");
continue;
} else if (tmp == ConsoleKey.F7.ToString())
{
//Print("N "); // string a = Console.ReadLine();
string a = "C:/tmp/bytes.bin";
string[] s = File.ReadAllText(a).Split(' ');
char[] end = new char[s.Length - 1];
for (int i=0;i<end.Length;i++)
{
end[i] = (char)BinStr(s[i])[0];
//Print(end[i]);
}
//Print((char)BinStr(s[0])[0]);
if (end[0] == 'A' && end[1] == 'D' && end[2] == 'D')
{
for (int i=0+3;i<end.Length;i++)
{
int n = end[i] + end[i];
Print(n);
}
}
//decompile(a);
continue;
}
while (tmp.Length > 1)
{
char a = tmp[tmp.Length - 1];
tmp = a.ToString();
}
buffer.Add(tmp);
}
}
static void comp()
{
if (buffer == null || buffer.Count <= 0)
{
Print("Error buffer empty");
return;
}
char[] r = new char[buffer.Count];
for (int i=0;i<buffer.Count;i++)
{
r[i] = Convert.ToChar(buffer[i]);
Print(r[i]);
}
foreach (char ch in r)
{
string a = Convert.ToString((int)ch, 2);
while (a.Length != 8)
{
string b = "0";
a = b + a;
}
File.AppendAllText(outfile, a + " ");
}
Print("Compile done!\n");
}
static void decompile(string filename)
{
}
static void run()
{
}
}
}
(the indenation got messed up when I add to this post.)
The problem is this: when I try to add the values I get from the file I get random numbers like: 100102, and odd stuff like that. What am I doing wrong? Thanks
Here's how you convert a string to a binary string of 1s and 0s:
var binstring = string.Join(" ", Encoding.ASCII.GetBytes(("Welcome, World!")).Select(byt => Convert.ToString(byt, 2).PadLeft(8, '0')));
To convert that back into a string, we will need to a parse byte[] array from it with a method like so:
public static byte[] GetBytes(string s)
{
byte[] result = new byte[(s.Length + 7) / 8];
int i = 0;
int j = 0;
foreach (char c in s)
{
result[i] <<= 1;
if (c == '1')
result[i] |= 1;
j++;
if (j == 8)
{
i++;
j = 0;
}
}
return result;
}
Then we get those bytes and we can simply convert it into a string using:
Encoding.ASCII.GetString(GetBytes(binstring));
References: How could I encode a string of 1s and 0s for transport?How to get the binary code behind ASCII (C#)

Decrypt Function Equivalent in C#

I have this function to decrypt connection strings in VB .NET, and I try to bring it to C#:
Function DecryptText(strText As String, ByVal strPwd As String)
Dim i As Integer, c As Integer
Dim strBuff As String
#If Not CASE_SENSITIVE_PASSWORD Then
'Convert password to upper case
'if not case-sensitive
strPwd = UCase$(strPwd)
#End If
'Decrypt string
If Len(strPwd) Then
For i = 1 To Len(strText)
c = Asc(Mid$(strText, i, 1))
c = c - Asc(Mid$(strPwd, (i Mod Len(strPwd)) + 1, 1))
strBuff = strBuff & Chr$(c & &HFF)
Next i
Else
strBuff = strText
End If
DecryptText = strBuff
End Function
I tried with these two ways:
public static string DesencriptarCadena(this string cadena, string llave = "asdf")
{
try
{
cadena = cadena.Replace("\r", "").Replace("\n", "");
byte b = 0;
string strBuff = "";
System.Text.Encoding encoding = System.Text.Encoding.Default;
for (int i = 0; i < cadena.Length; i++)
{
b = encoding.GetBytes(cadena.ToCharArray(), i, 1)[0];
int ind = i % llave.Length;
b -= encoding.GetBytes(llave.ToCharArray(), ind, 1)[0];
strBuff += encoding.GetChars(new byte[] { Convert.ToByte((int)b & +0xff) })[0];
}
return strBuff;
}
catch (Exception ex)
{
return cadena;
}
}
or this other way:
public static string DesencriptarCadena(this string cadena, string llave = "asdf")
{
try
{
cadena = cadena.Replace("\r", "").Replace("\n", "");
int c;
string strBuff = "";
System.Text.Encoding encoding = System.Text.Encoding.Default;
for (int i = 0; i < cadena.Length; i++)
{
int ind = i % llave.Length;
c = cadena[i];
c -= llave[ind];
strBuff += Convert.ToChar(c & +0xff);
}
return strBuff;
}
catch (Exception ex)
{
return cadena;
}
}
But, it doesn't work. Can someone explain to me what I should do?
In the first way return:
ÞÓÚÏË×ÞÓÚÏË×ÞÓÚÏ ÞÓÚÏË×ÞÓÚÏú×ÞÓÚÏË×ÞÓÚÏË×ÞÓÚÏË×ÞÚÏË×ÞÓÚÏË×Þ
In the second: NúÎCÄÁÎî
It is not returning the decrypted connection string.
http://converter.telerik.com has always done great by me for these types of conversions. Let me know how this works.
public object DecryptText(string strText, string strPwd)
{
int i = 0;
int c = 0;
string strBuff = null;
#if Not CASE_SENSITIVE_PASSWORD
//Convert password to upper case
//if not case-sensitive
strPwd = Strings.UCase(strPwd);
#endif
//Decrypt string
if (Strings.Len(strPwd)) {
for (i = 1; i <= Strings.Len(strText); i++) {
c = Strings.Asc(Strings.Mid(strText, i, 1));
c = c - Strings.Asc(Strings.Mid(strPwd, (i % Strings.Len(strPwd)) + 1, 1));
strBuff = strBuff + Strings.Chr(c + 0xff);
}
} else {
strBuff = strText;
}
return strBuff;
}
After including the Microsoft.VisualBasic assembly, I added two using statements:
using Microsoft.VisualBasic;
using Microsoft.VisualBasic.CompilerServices;
I compiled it and decompiled it with dotPeek. It resulted in this:
public static object DecryptText(string strText, string strPwd)
{
string str = string.Empty;
strPwd = Strings.UCase(strPwd);
if ((uint)Strings.Len(strPwd) > 0U)
{
int num1 = 1;
int num2 = Strings.Len(strText);
int Start = num1;
while (Start <= num2)
{
int num3 = checked(Strings.Asc(Strings.Mid(strText, Start, 1)) - Strings.Asc(Strings.Mid(strPwd, unchecked(Start % Strings.Len(strPwd)) + 1, 1)));
str = str + Conversions.ToString(Convert.ToChar(Conversions.ToString(num3) + Conversions.ToString((int)byte.MaxValue)));
checked { ++Start; }
}
}
else
str = strText;
return (object)str;
}

convert UTF-8 HEX into emoji representation

How do we convert UTF-8 HEX: EE 94 93 into equivalent Emoji representation: 1f1e8, 1f1f3
taken from here: http://www.iemoji.com/view/emoji/175/places/regional-indicator-symbol-letters-cn
public static string ConvertEmoji2UnicodeHex(string emoji)
{
if (string.IsNullOrWhiteSpace(emoji))
return emoji;
byte[] bytes = Encoding.UTF8.GetBytes(emoji);
string firstItem = Convert.ToString(bytes[0], 2);
int iv;
if (bytes.Length == 1)
{
iv = Convert.ToInt32(firstItem, 2);
}
else
{
StringBuilder sbBinary = new StringBuilder();
sbBinary.Append(firstItem.Substring(bytes.Length + 1).TrimStart('0'));
for (int i = 1; i < bytes.Length; i++)
{
string item = Convert.ToString(bytes[i], 2);
item = item.Substring(2);
sbBinary.Append(item);
}
iv = Convert.ToInt32(sbBinary.ToString(), 2);
}
return Convert.ToString(iv, 16).PadLeft(4, '0');
}

AES to return Alphanumeric

I have an aes encryption code, i want to make it only return alphanumerical characters like {0123456789ABCDEFGHIJKLMNOPQRSTWUVYZ}
But however i could not figure out how to do that. I have almost no idea about encryption, could not figure out where to fix. I would really apreciate your feedback. Regards...
public class clsCrypto
{
private string _KEY = string.Empty;
protected internal string KEY
{
get
{
return _KEY;
}
set
{
if (!string.IsNullOrEmpty(value))
{
_KEY = value;
}
}
}
private string _IV = string.Empty;
protected internal string IV
{
get
{
return _IV;
}
set
{
if (!string.IsNullOrEmpty(value))
{
_IV = value;
}
}
}
private string CalcMD5(string strInput)
{
string strOutput = string.Empty;
if (!string.IsNullOrEmpty(strInput))
{
try
{
StringBuilder strHex = new StringBuilder();
using (MD5 md5 = MD5.Create())
{
byte[] bytArText = Encoding.Default.GetBytes(strInput);
byte[] bytArHash = md5.ComputeHash(bytArText);
for (int i = 0; i < bytArHash.Length; i++)
{
strHex.Append(bytArHash[i].ToString("X2"));
}
strOutput = strHex.ToString();
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
return strOutput;
}
private byte[] GetBytesFromHexString(string strInput)
{
byte[] bytArOutput = new byte[] { };
if ((!string.IsNullOrEmpty(strInput)) && strInput.Length % 2 == 0)
{
SoapHexBinary hexBinary = null;
try
{
hexBinary = SoapHexBinary.Parse(strInput);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
bytArOutput = hexBinary.Value;
}
return bytArOutput;
}
private byte[] GenerateIV()
{
byte[] bytArOutput = new byte[] { };
try
{
string strIV = CalcMD5(IV);
bytArOutput = GetBytesFromHexString(strIV);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
return bytArOutput;
}
private byte[] GenerateKey()
{
byte[] bytArOutput = new byte[] { };
try
{
string strKey = CalcMD5(KEY);
bytArOutput = GetBytesFromHexString(strKey);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
return bytArOutput;
}
protected internal string Encrypt(string strInput, CipherMode cipherMode)
{
string strOutput = string.Empty;
if (!string.IsNullOrEmpty(strInput))
{
try
{
byte[] bytePlainText = Encoding.Default.GetBytes(strInput);
using (RijndaelManaged rijManaged = new RijndaelManaged())
{
rijManaged.Mode = cipherMode;
rijManaged.BlockSize = 128;
rijManaged.KeySize = 128;
rijManaged.IV = GenerateIV();
rijManaged.Key = GenerateKey();
rijManaged.Padding = PaddingMode.Zeros;
ICryptoTransform icpoTransform = rijManaged.CreateEncryptor(rijManaged.Key, rijManaged.IV);
using (MemoryStream memStream = new MemoryStream())
{
using (CryptoStream cpoStream = new CryptoStream(memStream, icpoTransform, CryptoStreamMode.Write))
{
cpoStream.Write(bytePlainText, 0, bytePlainText.Length);
cpoStream.FlushFinalBlock();
}
strOutput = Encoding.Default.GetString(memStream.ToArray());
}
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
return strOutput;
}
protected internal string Decrypt(string strInput, CipherMode cipherMode)
{
string strOutput = string.Empty;
if (!string.IsNullOrEmpty(strInput))
{
try
{
byte[] byteCipherText = Encoding.Default.GetBytes(strInput);
byte[] byteBuffer = new byte[strInput.Length];
using (RijndaelManaged rijManaged = new RijndaelManaged())
{
rijManaged.Mode = cipherMode;
rijManaged.BlockSize = 128;
rijManaged.KeySize = 128;
rijManaged.IV = GenerateIV();
rijManaged.Key = GenerateKey();
rijManaged.Padding = PaddingMode.Zeros;
ICryptoTransform icpoTransform = rijManaged.CreateDecryptor(rijManaged.Key, rijManaged.IV);
using (MemoryStream memStream = new MemoryStream(byteCipherText))
{
using (CryptoStream cpoStream = new CryptoStream(memStream, icpoTransform, CryptoStreamMode.Read))
{
cpoStream.Read(byteBuffer, 0, byteBuffer.Length);
}
strOutput = Encoding.Default.GetString(byteBuffer);
}
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
return strOutput;
}
}
Encryption and decryption functions use byte arrays as parameters.
So, you must convert these arrays to base 36 string.
You can use the following class (Base36) to make these conversions:
All you have to do, is calling these two functions:
byte[] byteArray;
//To convert byte array to String
string byteArrayInBase36 = Base36.ByteArrayToBase36String(byteArray);
//To convert String to byte Array
byte[] byteArray2 = Base36.Base36StringToByteArray(byteArrayInBase36);
and, this is the class:
using System;
using System.Collections.Generic;
class Base36
{
#region public methods
public static string ByteArrayToBase36String(byte[] bytes)
{
string result = string.Empty;
result = Encode36((ulong)bytes.Length).PadLeft(BASE36_LENGTH_BLOC_SIZE_36, '0');
if (bytes.Length > 0)
{
List<byte[]> byteslist = SplitBytes(bytes, 8);
if (byteslist[byteslist.Count - 1].Length < 8)
{
byte[] newLastArray = new byte[8];
byteslist[byteslist.Count - 1].CopyTo(newLastArray, 0);
byteslist[byteslist.Count - 1] = newLastArray;
}
foreach (byte[] byteArray in byteslist)
{
ulong value = 0;
//for (int i = 0; i < byteArray.Length; i++) value = value * 256 + byteArray[i];
value = BitConverter.ToUInt64(byteArray, 0);
result = result + Encode36(value).PadLeft(BASE36_BLOC_SIZE_36, '0');
}
}
return result;
}
public static byte[] Base36StringToByteArray(string input)
{
byte[] result = new byte[0];
if (input.Length >= BASE36_LENGTH_BLOC_SIZE_36)
{
int arrayLength = (int)Decode36(input.Substring(0, BASE36_LENGTH_BLOC_SIZE_36));
string data = input.Remove(0, BASE36_LENGTH_BLOC_SIZE_36);
List<byte[]> bytesList = new List<byte[]>();
foreach (string value36 in new List<string>(SplitStringByLength(data, BASE36_BLOC_SIZE_36)))
{
byte[] byteArray = BitConverter.GetBytes(Decode36(value36));
bytesList.Add(byteArray);
}
result = JoinBytes(bytesList);
Array.Resize(ref result, arrayLength);
}
return result;
}
#endregion
#region Const
private const int BASE36_LENGTH_BLOC_SIZE_36 = 6;
private const int BASE36_BLOC_SIZE_36 = 13; //Encode36(ulong.MaxValue).Length;
#endregion
#region private methods
static string _CharList36 = string.Empty;
static private string CharList36
{
get
{
if (_CharList36.Length < 36)
{
char[] array = new char[36];
for (int i = 0; i < 10; i++) array[i] = (char)(i + 48);
for (int i = 0; i < 26; i++) array[i + 10] = (char)(i + 97);
_CharList36 = new string(array);
}
return _CharList36;
}
}
private static List<string> SplitStringByLength(string str, int chunkSize)
{
List<string> list = new List<string>();
int i;
for (i = 0; i < str.Length / chunkSize; i++)
{
list.Add(str.Substring(i * chunkSize, chunkSize));
}
i = i * chunkSize;
if (i < str.Length - 1)
list.Add(str.Substring(i, str.Length - i));
return list;
}
private static String Encode36(ulong input)
{
if (input < 0) throw new ArgumentOutOfRangeException("input", input, "input cannot be negative");
char[] clistarr = CharList36.ToCharArray();
var result = new Stack<char>();
while (input != 0)
{
result.Push(clistarr[input % 36]);
input /= 36;
}
return new string(result.ToArray()).ToUpper();
}
private static ulong Decode36(string input)
{
var reversed = ReverseString(input.ToLower());
ulong result = 0;
int pos = 0;
foreach (char c in reversed)
{
result += (ulong)CharList36.IndexOf(c) * (ulong)Math.Pow(36, pos);
pos++;
}
return result;
}
private static string ReverseString(string text)
{
char[] cArray = text.ToCharArray();
string reverse = String.Empty;
for (int i = 0; i < cArray.Length / 2; i++)
{
char c = cArray[i];
cArray[i] = cArray[cArray.Length - 1 - i];
cArray[cArray.Length - 1 - i] = c;
}
return new string(cArray);
}
private static byte[] StringToBytes(string str)
{
byte[] bytes = new byte[str.Length * sizeof(char)];
System.Buffer.BlockCopy(str.ToCharArray(), 0, bytes, 0, bytes.Length);
return bytes;
}
private static List<byte[]> SplitBytes(byte[] bytes, int length)
{
List<byte[]> result = new List<byte[]>();
int position = 0;
while (bytes.Length - position > length)
{
byte[] temp = new byte[length];
for (int i = 0; i < temp.Length; i++) temp[i] = bytes[i + position];
position += length;
result.Add(temp);
}
if (position < bytes.Length)
{
byte[] temp = new byte[bytes.Length - position];
for (int i = 0; i + position < bytes.Length; i++) temp[i] = bytes[i + position];
result.Add(temp);
}
return result;
}
private static string BytesToString(byte[] bytes)
{
char[] chars = new char[bytes.Length / sizeof(char)];
System.Buffer.BlockCopy(bytes, 0, chars, 0, bytes.Length);
return new string(chars);
}
private static byte[] JoinBytes(List<byte[]> listBytes)
{
int totalLength = 0;
foreach (byte[] bytes in listBytes) totalLength += bytes.Length;
byte[] result = new byte[totalLength];
int position = 0;
foreach (byte[] bytes in listBytes)
for (int i = 0; i < bytes.Length; i++)
{
result[position] = bytes[i];
position++;
}
return result;
}
#endregion
}
You can encode the resulting bytes using Base36 or another such binary-to-text encoding.

Categories

Resources