/// <summary> /// Convert a string like '6162636465666768696A6B6C6D6E6F707172737475767778797A' to decimal string like 'abcdefghijklmnopqrstuvwxyz' /// ONLY ASCII IN THIS FUNCTION /// </summary> /// <param name="bufferToManage">String to convert</param> /// <returns></returns> public static string ConvertHexadecimalStringToAsciiString(string bufferToManage) { if (bufferToManage == null) { throw new NolmeArgumentNullException(); } int Offset = 0; string ResultBuffer = StringUtility.CreateEmptyString(); byte[] TextBytesArray = new byte [bufferToManage.Length / 2]; for (int i = 0; i < bufferToManage.Length / 2; i++) { TextBytesArray [Offset] = (byte)Int32.Parse(bufferToManage.Substring(i * 2, 2), System.Globalization.NumberStyles.HexNumber, CultureInfo.InvariantCulture); Offset++; } char[] AsciiCharacterArray = new char[Encoding.ASCII.GetCharCount(TextBytesArray, 0, TextBytesArray.Length)]; Encoding.ASCII.GetChars(TextBytesArray, 0, TextBytesArray.Length, AsciiCharacterArray, 0); ResultBuffer = new string(AsciiCharacterArray); return(ResultBuffer); }
/// <summary> /// Fast & very simple crypt to hexa string /// </summary> public static string CryptAsciiStringToHexadecimalString(string bufferToManage, byte cryptKey, bool cryptOrDecrypt) { if (bufferToManage == null) { throw new NolmeArgumentNullException(); } StringBuilder LocalStringBuilder; string TemporaryBuffer; string ResultBuffer = StringUtility.CreateEmptyString(); byte[] TextBytesArray = null; if (cryptOrDecrypt) { TextBytesArray = Encoding.ASCII.GetBytes(bufferToManage); /* * foreach (byte a in TextBytesArray) * { * byte bCryptedByte = (byte)(a + cryptKey); * if (bCryptedByte<16) * ResultBuffer += "0" + bCryptedByte.ToString ("X", CultureInfo.InvariantCulture); * else * ResultBuffer += bCryptedByte.ToString ("X", CultureInfo.InvariantCulture); * } */ LocalStringBuilder = new StringBuilder(); foreach (byte a in TextBytesArray) { byte bCryptedByte = (byte)(a + cryptKey); if (bCryptedByte < 16) { TemporaryBuffer = "0" + bCryptedByte.ToString("X", CultureInfo.InvariantCulture); } else { TemporaryBuffer = bCryptedByte.ToString("X", CultureInfo.InvariantCulture); } LocalStringBuilder.Append(TemporaryBuffer); } ResultBuffer = LocalStringBuilder.ToString(); } else { int Offset = 0; TextBytesArray = new byte [bufferToManage.Length / 2]; for (int i = 0; i < bufferToManage.Length / 2; i++) { TextBytesArray [Offset] = (byte)Int32.Parse(bufferToManage.Substring(i * 2, 2), System.Globalization.NumberStyles.HexNumber, CultureInfo.InvariantCulture); TextBytesArray [Offset] = (byte)(TextBytesArray [Offset] - cryptKey); // DECRYPT Offset++; } char[] AsciiCharacterArray = new char[Encoding.ASCII.GetCharCount(TextBytesArray, 0, TextBytesArray.Length)]; Encoding.ASCII.GetChars(TextBytesArray, 0, TextBytesArray.Length, AsciiCharacterArray, 0); ResultBuffer = new string(AsciiCharacterArray); } return(ResultBuffer); }