Crisis.Ionic.Zip.ZipCrypto.EncryptMessage C# (CSharp) Method

EncryptMessage() public method

This is the converse of DecryptMessage. It encrypts the plaintext and produces a ciphertext.
public EncryptMessage ( byte plainText, int length ) : byte[]
plainText byte The plain text buffer.
length int /// The number of bytes to encrypt. /// Should be less than or equal to plainText.Length. ///
return byte[]
        public byte[] EncryptMessage(byte[] plainText, int length)
        {
            if (plainText == null)
                throw new ArgumentNullException("plaintext");

            if (length > plainText.Length)
                throw new ArgumentOutOfRangeException("length",
                                                      "Bad length during Encryption: The length parameter must be smaller than or equal to the size of the destination array.");

            byte[] cipherText = new byte[length];
            for (int i = 0; i < length; i++)
            {
                byte C = plainText[i];
                cipherText[i] = (byte)(plainText[i] ^ MagicByte);
                UpdateKeys(C);
            }
            return cipherText;
        }

Usage Example

Example #1
0
        public override void Write(byte[] buffer, int offset, int count)
        {
            if (_mode == CryptoMode.Decrypt)
            {
                throw new NotSupportedException("This stream does not Decrypt via Write()");
            }

            if (buffer == null)
            {
                throw new ArgumentNullException("buffer");
            }

            // workitem 7696
            if (count == 0)
            {
                return;
            }

            byte[] plaintext = null;
            if (offset != 0)
            {
                plaintext = new byte[count];
                for (int i = 0; i < count; i++)
                {
                    plaintext[i] = buffer[offset + i];
                }
            }
            else
            {
                plaintext = buffer;
            }

            byte[] encrypted = _cipher.EncryptMessage(plaintext, count);
            _s.Write(encrypted, 0, encrypted.Length);
        }