/* * Algorithm decryption and encryption */ private void deAndenCryption(byte[] source, operationTypeEnum operationType) { if (operationType == operationTypeEnum.Encryption) { buttonEncryption.Enabled = false; buttonDecryption.Enabled = true; } else if (operationType == operationTypeEnum.Decryption) { buttonEncryption.Enabled = true; buttonDecryption.Enabled = false; } byte[] key = Encoding.UTF8.GetBytes(textBoxKey.Text); //key ushort[] numKey = new ushort[key.Length]; //number key ushort keyLength = (ushort)key.Length; //Counter ushort maxCharVal = 0; //Max char value ushort ind = 0; //current index /*string key to number key*/ for (ushort i = 0; i < key.Length; i++) { for (ushort j = 0; j < key.Length; j++) { if (maxCharVal < key[j] && numKey[j] == 0) { maxCharVal = key[j]; ind = j; } } numKey[ind] = keyLength--; maxCharVal = 0; ind = 0; } /*encryption*/ byte[] result = new byte[source.Length]; uint sizeEncrpttion = (uint)((source.Length / numKey.Length) * numKey.Length); uint remainder = (uint)(source.Length - sizeEncrpttion); for (uint i = 0; i < sizeEncrpttion; i += (uint)numKey.Length) { for (uint j = 0; j < numKey.Length; j++) { if (operationType == operationTypeEnum.Encryption) { result[i + j] = source[i + numKey[j] - 1]; } else if (operationType == operationTypeEnum.Decryption) { result[i + numKey[j] - 1] = source[i + j]; } } } /*end of the array without encryption*/ Array.Copy(source, sizeEncrpttion, result, sizeEncrpttion, remainder); /*Show to richEditor*/ currentFile = result; richTextBoxFile.Text = Encoding.UTF8.GetString(result); }
/* * Algorithm decryption and encryption */ private void deAndenCryption(operationTypeEnum operationType, byte[] source) { /* * Warning about wrong a one-time notepad */ if ((oneTimeNotepad.Length != currentFile.Length) && operationType == operationTypeEnum.Encryption) { DialogResult result; // Displays the MessageBox. result = MessageBox.Show( this, "The length of a one-time notepad does not equal the source file. Do you want to continue this encryption?", "Warning", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1, MessageBoxOptions.RightAlign ); if (result == DialogResult.No) { return; } } byte[] newSource = new byte[source.Length]; for (int i = 0, j = 0; i < source.Length; i++, j++) { if (j < oneTimeNotepad.Length) { j = 0; } newSource[i] = (byte)(source[i] ^ oneTimeNotepad[j]); } currentFile = newSource; richTextBoxFile.Text = Encoding.UTF8.GetString(newSource); }