/// <summary> /// Encrypts a string by using it's UTF-8 Value and change it to it's binary value with diffrent methods /// Takes account for all characters that exist in UTF-8 /// </summary> /// <param name="str">The String to Encrypt</param> /// <param name="toBinaryType">Method used to change the UTF-8 Value to Binary</param> /// <param name="fillBits">Number of 0 to get a certain length</param> /// <param name="seperator">A string between each Character</param> /// <returns></returns> public static string ToBinaryEncryption(string str, ToBinaryType toBinaryType = ToBinaryType.Normal, int fillBits = 0, string seperator = ",") { string result = null; foreach (char character in str) { string convertedCharacter = IntegerToBinary(Convert.ToInt32(character), toBinaryType); while (convertedCharacter.Length < fillBits) { convertedCharacter = '0' + convertedCharacter; } result = result + convertedCharacter + seperator; } return(result); }
private static string IntegerToBinary(int number, ToBinaryType type) { string result = null; switch (type) { case ToBinaryType.Normal: result = Convert.ToString(number, 2); break; case ToBinaryType.CP1: result = Convert.ToString(number, 2); char[] resultToArray = result.ToCharArray(); for (int i = 0; i < resultToArray.Length; i++) { if (resultToArray[i].Equals('1')) { resultToArray[i] = '0'; } else { resultToArray[i] = '1'; } } result = new string(resultToArray); break; case ToBinaryType.CP2: result = Convert.ToString(number, 2); char[] toArray = result.ToCharArray(); for (int i = 0; i < toArray.Length; i++) { if (toArray[i].Equals('1')) { toArray[i] = '0'; } else { toArray[i] = '1'; } } result = new string(toArray); result = Convert.ToString(Convert.ToInt32(result, 2) + 1, 2); break; } return(result); }