Exemple #1
0
        public static string DecryptASCII(byte[] cipherText, byte[] key)
        {
            if (cipherText == null)
            {
                throw new ArgumentNullException("cipherText");
            }
            if (key == null)
            {
                throw new ArgumentNullException("key");
            }
            var convKey = KeyToShiftValues(key).ToArray();

            if (convKey.Length == 0)
            {
                throw new ArgumentException("Converted key is empty. Please use alphabetic characters.");
            }
            // copy byte array into work buffer for manipulation
            byte[] buff = new byte[cipherText.Length];
            Buffer.BlockCopy(cipherText, 0, buff, 0, cipherText.Length);
            int keyNdx = 0;

            for (int z = 0; z < buff.Length; z++)
            {
                if (Utils.IsAsciiByteUpper(buff[z]) || Utils.IsAsciiByteLower(buff[z]))
                {
                    buff[z] = Caesar.DecryptOneByteCaesarASCII(buff[z], convKey[keyNdx++]);
                    if (keyNdx >= convKey.Length)
                    {
                        keyNdx = 0; // wrap key back to beginning
                    }
                }
            }
            return(Encoding.ASCII.GetString(buff));
        }
Exemple #2
0
        public static string EncryptASCII(byte[] plainText, byte[] key)
        {
            if (plainText == null)
            {
                throw new ArgumentNullException("plaintext");
            }
            if (key == null)
            {
                throw new ArgumentNullException("key");
            }
            // select all alpha, convert to upper case, and turn characters into individual shift values for Caesar byte-by-byte encryption
            var convKey = KeyToShiftValues(key).ToArray();

            if (convKey.Length == 0)
            {
                throw new ArgumentException("Converted key is empty. Please use alphabetic characters.");
            }
            // copy byte array into work buffer for manipulation
            byte[] buff = new byte[plainText.Length];
            Buffer.BlockCopy(plainText, 0, buff, 0, plainText.Length);
            int keyNdx = 0;

            for (int z = 0; z < buff.Length; z++)
            {
                if (Utils.IsAsciiByteUpper(buff[z]) || Utils.IsAsciiByteLower(buff[z]))
                {
                    buff[z] = Caesar.EncryptOneByteCaesarASCII(buff[z], convKey[keyNdx++]);
                    if (keyNdx >= convKey.Length)
                    {
                        keyNdx = 0; // wrap key back to beginning
                    }
                }
            }
            return(Encoding.ASCII.GetString(buff));
        }