public static byte[] Encrypt(byte[] data, byte[] key) { uint[] key1 = AlgoTea.CreateKey(key); uint[] v = new uint[2]; byte[] buffer = new byte[AlgoTea.NextMultipleOf8(data.Length + 4)]; byte[] bytes = BitConverter.GetBytes(data.Length); Array.Copy((Array)bytes, (Array)buffer, bytes.Length); Array.Copy((Array)data, 0, (Array)buffer, bytes.Length, data.Length); using (MemoryStream memoryStream = new MemoryStream(buffer)) { using (BinaryWriter binaryWriter = new BinaryWriter((Stream)memoryStream)) { for (int startIndex = 0; startIndex < buffer.Length; startIndex += 8) { v[0] = BitConverter.ToUInt32(buffer, startIndex); v[1] = BitConverter.ToUInt32(buffer, startIndex + 4); AlgoTea.BlockEncrypt(v, key1); binaryWriter.Write(v[0]); binaryWriter.Write(v[1]); } } } return(buffer); }
public static byte[] Decrypt(byte[] data1, byte[] key) { byte[] data = AlgoTea.Encrypt(data1, key); if (data.Length % 8 != 0) { throw new ArgumentException("Длина зашифрованных данных должна быть кратна 8 байтам"); } uint[] key1 = AlgoTea.CreateKey(key); uint[] v = new uint[2]; byte[] buffer = new byte[data.Length]; Array.Copy((Array)data, (Array)buffer, data.Length); using (MemoryStream memoryStream = new MemoryStream(buffer)) { using (BinaryWriter binaryWriter = new BinaryWriter((Stream)memoryStream)) { for (int startIndex = 0; startIndex < buffer.Length; startIndex += 8) { v[0] = BitConverter.ToUInt32(buffer, startIndex); v[1] = BitConverter.ToUInt32(buffer, startIndex + 4); AlgoTea.BlockDecrypt(v, key1); binaryWriter.Write(v[0]); binaryWriter.Write(v[1]); } } } uint uint32 = BitConverter.ToUInt32(buffer, 0); if ((long)uint32 > (long)(buffer.Length - 4)) { throw new ArgumentException("Зашифрованные данные разрушены"); } byte[] numArray = new byte[(int)uint32]; Array.Copy((Array)buffer, 4L, (Array)numArray, 0L, (long)uint32); return(numArray); }
private void btnDecode_Click(object sender, EventArgs e) { try { this.tbDecoded.Text = AlgoTea.Decrypt(this.tbSource.Text, this.tbPassword.Text); } catch (Exception ex) { int num = (int)MessageBox.Show((IWin32Window)this, ex.Message, "Декодирование", MessageBoxButtons.OK, MessageBoxIcon.Hand); } }
public static string Encrypt(string data, string key) => Encoding.Default.GetString(AlgoTea.Encrypt(Encoding.Default.GetBytes(data), Encoding.Default.GetBytes(key)));