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 btnEncode_Click(object sender, EventArgs e) { try { this.tbEncoded.Text = AlgoTea.Encrypt(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)));