Cryptography of an array of bytes in C #

I want to create beautiful cryptography using bitwise operators. However, I cannot do this.

I want it to have bitwise operators using a byte array to encrypt and decrypt my byte array.

public class Cryptographer { private byte[] Keys { get; set; } public Cryptographer(string password) { Keys = Encoding.ASCII.GetBytes(password); } public void Encrypt(byte[] data) { for(int i = 0; i < data.Length; i++) { data[i] = (byte) (data[i] & Keys[i]); } } public void Decrypt(byte[] data) { for (int i = 0; i < data.Length; i++) { data[i] = (byte)(Keys[i] & data[i]); } } } 

I know this is wrong, that’s why I need help. I just want it to use 1 line to encrypt and decrypt all data.

+4
source share
5 answers

This is what is sometimes called "craptography" because it provides the illusion of security while being functionally useless in protecting something. Use class frameworks if you want to do cryptography correctly, because it is extremely difficult to collapse your own.

Take a look at this for advice on what you are trying to do (encryption / decryption) - http://msdn.microsoft.com/en-us/library/e970bs09.aspx . In fact, your requirements should determine which classes you decide to use. This has a nice background: http://msdn.microsoft.com/en-us/library/92f9ye3s.aspx

For simple encryption / decryption (if that's what you need), DPAPI can be the easiest way.

+26
source

It seems you are trying to implement an XOR cipher . XOR ^ in C #:

 public void Crypt(byte[] data) { for(int i = 0; i < data.Length; i++) { data[i] = (byte) (data[i] ^ Keys[i]); } ↑ } 

Since the Encrypt and Decrypt method does the same, you only need one method.

Please note that this is only a toy and is not suitable for protecting data in real-world scenarios. Check out the System.Security.Cryptography namespace , which provides many implementations of proven algorithms. Making good use of them is still hard to get right .

+10
source

Use the Xor ^ operator, not And & . Also, you should not assume that the data and key have the same length.

 public class Cryptographer { private byte[] Keys { get; set; } public Cryptographer(string password) { Keys = Encoding.ASCII.GetBytes(password); } public void Encrypt(byte[] data) { for(int i = 0; i < data.Length; i++) { data[i] = (byte) (data[i] ^ Keys[i % Keys.Length]); } } public void Decrypt(byte[] data) { for (int i = 0; i < data.Length; i++) { data[i] = (byte)(Keys[i % Keys.Length] ^ data[i]); } } } 
+6
source
  static void Main(string[] args) { Int32 a = 138; Console.WriteLine("first int: " + a.ToString()); byte[] bytes = BitConverter.GetBytes(a); var bits = new BitArray(bytes); String lol = ToBitString(bits); Console.WriteLine("bit int: " + lol); lol = lol.Substring(1, lol.Length - 1) + lol[0]; Console.WriteLine("left : " + lol); byte[] bytes_new = GetBytes(lol); byte[] key = { 12, 13, 24, 85 }; var bits2 = new BitArray(key); String lol2 = ToBitString(bits2); Console.WriteLine("key : " + lol2); byte[] cryptedBytes = Crypt(bytes_new, key); var bits3 = new BitArray(cryptedBytes); String lol3 = ToBitString(bits3); Console.WriteLine(" XOR: " + lol3); byte[] deCryptedBytes = Crypt(cryptedBytes, key); var bits4 = new BitArray(cryptedBytes); String lol4 = ToBitString(bits4); Console.WriteLine(" DEXOR: " + lol4); int a_new = BitConverter.ToInt32(bytes_new, 0); Console.WriteLine("and int: " + a_new.ToString()); Console.ReadLine(); } public static byte[] Crypt(byte[] data, byte[] key) { byte[] toCrypt = data; for (int i = 0; i < toCrypt.Length; i++) { toCrypt[i] = (byte)(toCrypt[i] ^ key[i]); } return toCrypt; } private static String ToBitString(BitArray bits) { var sb = new StringBuilder(); for (int i = bits.Count - 1; i >= 0; i--) { char c = bits[i] ? '1' : '0'; sb.Append(c); } return sb.ToString(); } private static byte[] GetBytes(string bitString) { byte[] result = Enumerable.Range(0, bitString.Length / 8). Select(pos => Convert.ToByte( bitString.Substring(pos * 8, 8), 2) ).ToArray(); List<byte> mahByteArray = new List<byte>(); for (int i = result.Length - 1; i >= 0; i--) { mahByteArray.Add(result[i]); } return mahByteArray.ToArray(); } 
0
source

Remember that there is no such thing as a β€œsecure” cipher. Any encryption method that may be recorded may be compromised. With that said, using simple bitwise encryption methods invites a not-so-bright hacker to break encryption. There are boys / girls who sit all day and nothing better to do. Use one of the encryption libraries that uses the big key, and before you use it, do something unusual. Despite this, remember that there are people who are busy and not working to do nothing but break the cryptographic messages around the world; 24 to 7. The Germans thought that in World War II they had an inextricable system. They called it Enigma. Read this and you will find that it was broken before the start of the war!

0
source

All Articles