Beispiel #1
0
        /// <summary>
        /// 构造函数,确定用哪一种对称加密算法来初始化加密对象。
        /// </summary>
        /// <param name="cryptType">加密算法类型,CryptType枚举</param>
        public SymmetricCrypt(CryptType cryptType)
        {
            switch (cryptType)
            {
            case CryptType.Rc2:
                _symmetricAlgorithm = new RC2CryptoServiceProvider();
                break;

            case CryptType.Rijndael:
                _symmetricAlgorithm = new RijndaelManaged();
                break;

            case CryptType.TripleDes:
                _symmetricAlgorithm = new TripleDESCryptoServiceProvider();
                break;

            case CryptType.Aes:
                _symmetricAlgorithm = new AesCryptoServiceProvider();
                break;

            case CryptType.Des:
                _symmetricAlgorithm = new DESCryptoServiceProvider();
                break;
            }
        }
        /// <summary>
        /// Encrypt/Decypt the given cryptData string with the given password
        /// </summary>
        /// <param name="cryptData">string data to encrypt</param>
        /// <param name="cryptPwd">password string</param>
        /// <param name="cryptType">crypt type</param>
        /// <returns>encrypted/decrypted data</returns>
        /// <remarks>if keySalt is null, then default keySalt is used</remarks>
        public static string GetCrypt(string cryptData, string cryptPwd,byte[] keySalt, CryptType cryptType)
        {
            try
            {
                byte[] cryptBytes = null;
                switch (cryptType)
                {
                    case CryptType.Encrypt:
                        cryptBytes = Encoding.Unicode.GetBytes(cryptData);
                        break;
                    case CryptType.Decrypt:
                        cryptBytes = Convert.FromBase64String(cryptData);
                        break;
                }

                byte[] retData = GetCrypt(cryptBytes, cryptPwd,keySalt, cryptType);
                switch (cryptType)
                {
                    case CryptType.Encrypt:
                        return Convert.ToBase64String(retData);
                    case CryptType.Decrypt:
                        return Encoding.Unicode.GetString(retData);
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message + " >" + ex.StackTrace);
            }
            return null;
        }
Beispiel #3
0
        private char CryptCharacter(char cipherCharacter, CryptType cryptType)
        {
            char[,] substitutionBox = SubstitutionBox;
            int[] cipherCharacterIndex = GetGridIndex(substitutionBox, cipherCharacter);
            Tile  markedTile           = new Tile(substitutionBox[MarkerIndex[0], MarkerIndex[1]]);

            int[] cryptedCipherCharacterIndex = new int[2];

            switch (cryptType)
            {
            case CryptType.Encrypt:
                cryptedCipherCharacterIndex[0] = (cipherCharacterIndex[0] + markedTile.X) % 6;
                cryptedCipherCharacterIndex[1] = (cipherCharacterIndex[1] + markedTile.Y) % 6;
                break;

            case CryptType.Decrypt:
                cryptedCipherCharacterIndex[0] = (cipherCharacterIndex[0] - markedTile.X + 6) % 6;
                cryptedCipherCharacterIndex[1] = (cipherCharacterIndex[1] - markedTile.Y + 6) % 6;
                break;
            }

            char cryptedCipherCharacter = substitutionBox[cryptedCipherCharacterIndex[0], cryptedCipherCharacterIndex[1]];

            return(cryptedCipherCharacter);
        }
Beispiel #4
0
        public static string Encrypt(string encryptString, CryptType format)
        {
            switch (format)
            {
            default:
            case CryptType.MD5:
                MD5           md5Hasher   = MD5.Create();
                byte[]        md5data     = md5Hasher.ComputeHash(Encoding.UTF8.GetBytes(encryptString));
                StringBuilder md5sBuilder = new StringBuilder();
                for (int i = 0; i < md5data.Length; i++)
                {
                    md5sBuilder.Append(md5data[i].ToString("x2"));
                }
                return(md5sBuilder.ToString());

            case CryptType.SHA1:
                SHA1          sha1Hasher   = SHA1.Create();
                byte[]        sha1data     = sha1Hasher.ComputeHash(Encoding.UTF8.GetBytes(encryptString));
                StringBuilder sha1sBuilder = new StringBuilder();
                for (int i = 0; i < sha1data.Length; i++)
                {
                    sha1sBuilder.Append(sha1data[i].ToString("x2"));
                }
                return(sha1sBuilder.ToString());

            case CryptType.DES:
                return(EncryptDES(encryptString));
            }
        }
        // ##########################################################################################
        //  xxx     x   x   xxxxx   xxxxx    xxx    x   x    xxxx
        //  x  x    x   x     x       x     x   x   xx  x   x
        //  xxxx    x   x     x       x     x   x   x x x    xxx
        //  x   x   x   x     x       x     x   x   x  xx       x
        //  xxxx     xxx      x       x      xxx    x   x   xxxx
        // ##########################################################################################
        private void buttonClient_Click(object sender, EventArgs e)
        {
            Errors    error_code = Errors.NO_ERROR;
            CryptType ctype      = (CryptType)comboBoxMenuCrypt.SelectedIndex;

            if (!DataCheck.CheckLogin(textBoxMenuLogin.Text, out error_code))
            {
                Messages.IncorrectLoginError(error_code);
            }
            if (!DataCheck.CheckIP(textBoxMenuIP.Text, out error_code))
            {
                Messages.IncorrectIPError(error_code);
            }
            if (!DataCheck.CheckPort(textBoxMenuPort.Text, out error_code))
            {
                Messages.IncorrectPortError(error_code);
            }
            int.TryParse(textBoxMenuPort.Text, out int port);

            client                     = new Client(textBoxMenuLogin.Text, textBoxMenuIP.Text, port, ctype);
            client.CliOutput           = rTextBoxClientMessage;
            client.CliList             = listViewClientUsers;
            client.FuncShowMessage     = ClientShowMessage;
            client.FuncDisconnect      = ClientDisableSend;
            client.FuncShutDown        = ClientLogout;
            panelMenu.Visible          = false;
            panelClient.Visible        = true;
            menuClient.Visible         = true;
            textBoxClient.Enabled      = true;
            buttonClientSend.Enabled   = true;
            rTextBoxClientMessage.Text = "";
            bgWorkerMenuTime.WorkerSupportsCancellation = true;
            bgWorkerMenuTime.RunWorkerAsync();
            client.Start();
        }
 public static BaseCrypter Create(CryptType cryptType)
 {
     switch (cryptType)
     {
     default:
         throw new ArgumentOutOfRangeException("Crypt type not found");
     }
 }
Beispiel #7
0
        public string CryptText(string plainText, CryptType cryptType)
        {
            while (plainText.Length % _blockSize != 0)
            {
                plainText += new char();
            }

            var roundKeys = (_cipherMode == CipherMode.CFB) ? _keys : (cryptType == CryptType.Encrypt) ? _keys : _keysReverse;

            var plainTextBytes = Encoding.Default.GetBytes(plainText);

            byte[] iv    = new byte[_blockSize];
            byte[] ivNew = new byte[_blockSize];

            if (_cipherMode == CipherMode.CBC)
            {
                iv = _IVcbc;
            }
            else if (_cipherMode == CipherMode.CFB)
            {
                iv = _IVcfb;
            }

            var result        = "";
            var blockPosition = 0;

            while (blockPosition < plainTextBytes.Length)
            {
                var blockBytes = new byte[_blockSize];
                Array.Copy(plainTextBytes, blockPosition, blockBytes, 0, _blockSize);
                if (_cipherMode == CipherMode.None)
                {
                    blockBytes = CryptBlock(blockBytes, roundKeys, cryptType);
                }
                else if (_cipherMode == CipherMode.CBC)
                {
                    blockBytes = CryptBlockCBC(blockBytes, ref iv, ref ivNew, roundKeys, cryptType);
                }
                else if (_cipherMode == CipherMode.CFB)
                {
                    blockBytes = CryptBlockCFB(blockBytes, ref iv, roundKeys, cryptType);
                }
                else
                {
                    throw new Exception("Undefined cipher mode");
                }

                result        += Encoding.Default.GetString(blockBytes);
                blockPosition += _blockSize;
            }
            return(result.Trim('\0'));
        }
Beispiel #8
0
        private char GetNewCh(int key, char ch, CryptType type)
        {
            var chIndex = Alphabet.IndexOf(ch);

            if (type == CryptType.Decrypt)
            {
                key = Alphabet.Length - key;
            }

            var copyAlphabetWithShift = Alphabet.Substring(key) + Alphabet.Substring(0, key);

            return(copyAlphabetWithShift.ElementAt(chIndex));
        }
Beispiel #9
0
        public static Key GenerateKey(Container container, string message, CryptType cryptType, EmbedType embedType, IKeyAware keyParser, string stringkey)
        {
            if (!keyParser.ValidateKey(stringkey))
                throw new ArgumentException("Key is not valid.");

            Key key = new Key();
            key.CryptType = cryptType;
            key.EmbedType = embedType;
            key.MessageLength = Constants.DEFAULT_ENCODING.GetByteCount(message);
            key.Value = keyParser.ParseKey(stringkey);

            return key;
        }
Beispiel #10
0
        public string Crypt(int key, string value, CryptType type)
        {
            var result  = "";
            var counter = new Counter(Alphabet.Length, key);

            foreach (var ch in value)
            {
                var newCh = GetNewCh(counter.GetNewValue(), ch, type);
                result += newCh;
            }

            return(result);
        }
Beispiel #11
0
        private char CryptCharacter(
            int wheelNumber,
            int orientation,
            char cipherLetter,
            CryptType cryptType)
        {
            HandleCryptCharacterErrors(orientation, cipherLetter);

            List <char> pattern;

            switch (wheelNumber)
            {
            case 0:
                pattern = new List <char>()
                {
                    'A', 'Z', '|', 'B', 'Y', '|', 'C', 'X', '|', 'D', 'W', '|',
                    'E', 'V', '|', 'F', 'U', '|', 'G', 'T', '|', 'H', 'S', '|',
                    'I', 'R', '|', 'J', 'Q', '|', 'K', 'P', '|', 'L', 'O', '|',
                    'M', 'N', '|'
                };
                break;

            case 1:
                pattern = new List <char>()
                {
                    'A', 'D', 'T', 'Q', 'R', 'G', 'U', 'P', 'X', 'E', 'V', 'O', 'W',
                    'Y', 'Z', 'N', 'I', 'C', 'L', 'M', 'B', 'K', 'J', 'H', 'S', 'F', '|'
                };
                break;

            case 2:
                pattern = new List <char>()
                {
                    'A', 'B', 'D', 'H', 'P', 'E', 'J', 'T', 'M', 'Z', 'Y', 'W', 'S', 'K',
                    'V', 'Q', 'G', 'N', '|', 'C', 'F', 'L', 'X', 'U', 'O', '|', 'I', 'R', '|'
                };
                break;

            default:
                throw new ArgumentException("wheelNumber must be an integer between 0 and 2");
            }

            List <char> adjustedPattern = new List <char>(OrientatePattern(pattern, orientation));

            adjustedPattern = GetSubPattern(adjustedPattern, cipherLetter);
            int preMappedLetterPosition = adjustedPattern.IndexOf(cipherLetter);

            return(GetCryptedCharacter(preMappedLetterPosition, cryptType, adjustedPattern));
        }
Beispiel #12
0
 private byte[] CryptBlock(byte[] blockBytes, IEnumerable <byte[]> roundKeys, CryptType cryptType)
 {
     foreach (var key in roundKeys)
     {
         if (_feistelType == FeistelType.ClassicFeistel)
         {
             blockBytes = PerformEncryptionClassic(blockBytes, key, key.SequenceEqual(roundKeys.Last()));
         }
         else
         {
             blockBytes = PerformEncryptionVariant1(blockBytes, key, cryptType);
         }
     }
     return(blockBytes);
 }
        public static string SubstitutionCipher(string text, int n, CryptType cryptType)
        {
            string result = string.Empty;
            substitutionCipher = new SubstitutionCipher(n);
            switch (cryptType)
            {
                case CryptType.Encrypt:
                    result = substitutionCipher.Encrypt(text);
                    break;
                case CryptType.Decrypt:
                    result = substitutionCipher.Decrypt(text);
                    break;
            }

            return result;
        }
        public static string CaesarCipher(string text, int shiftLength, CryptType cryptType)
        {
            string result = string.Empty;
            caesarCipher = new CaesarCipher(shiftLength);
            switch (cryptType)
            {
                case CryptType.Encrypt:
                    result = caesarCipher.Encrypt(text);
                    break;
                case CryptType.Decrypt:
                    result = caesarCipher.Decrypt(text);
                    break;
            }

            return result;
        }
        public static string FractionalCipher(string text, CryptType cryptType)
        {
            string result = string.Empty;
            fractionalCipher = new FractionalCipher();
            switch (cryptType)
            {
                case CryptType.Encrypt:
                    result = fractionalCipher.Encrypt(text);
                    break;
                case CryptType.Decrypt:
                    result = fractionalCipher.Decrypt(text);
                    break;
            }

            return result;
        }
        public static string PlayFairCipher(string text, CryptType cryptType)
        {
            string result = string.Empty;
            playFairCipher = new PlayFairCipher();
            switch (cryptType)
            {
                case CryptType.Encrypt:
                    result = playFairCipher.Encrypt(text);
                    break;
                case CryptType.Decrypt:
                    result = playFairCipher.Decrypt(text);
                    break;
            }

            return result;
        }
        public static string RunningKeyCipher(string text, string sensText, CryptType cryptType)
        {
            string result = string.Empty;
            runningKeyCipher = new RunningKeyCipher(sensText);
            switch (cryptType)
            {
                case CryptType.Encrypt:
                    result = runningKeyCipher.Encrypt(text);
                    break;
                case CryptType.Decrypt:
                    result = runningKeyCipher.Decrypt(text);
                    break;
            }

            return result;
        }
Beispiel #18
0
        /// <summary>
        /// 读取加密cookie
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="strName">名称</param>
        /// <param name="cryptType">加密方式</param>
        /// <returns>加密前的对象</returns>
        public static T GetCryptCookie <T>(string strName, CryptType cryptType = CryptType.Aes)
        {
            try
            {
                string jsonString  = null;
                string cryptString = GetCookie(strName);
                #if DEBUG
                jsonString = cryptString;
                if (string.IsNullOrWhiteSpace(jsonString))
                {
                    return(default(T));
                }
                return(JsonConvert.DeserializeObject <T>(jsonString));
                #endif
                switch (cryptType)
                {
                case CryptType.Aes:
                {
                    jsonString = AesEncryptHelper.DecryptAes(cryptString);
                    break;
                }

                case CryptType.Des:
                {
                    jsonString = DesEncryptHelper.DecryptDes(cryptString);
                    break;
                }

                default:
                {
                    jsonString = EncryptUtils.Base64Decrypt(cryptString);
                    break;
                }
                }

                if (string.IsNullOrWhiteSpace(jsonString))
                {
                    return(default(T));
                }
                return(JsonConvert.DeserializeObject <T>(jsonString));
            }
            catch (Exception e)
            {
                LogHelper.Error($@"CookieHelper ReadError {e.Message}", e);
                return(default(T));
            }
        }
Beispiel #19
0
        // ##########################################################################################
        /// <summary> Konstruktor obiektu klasy Client. </summary>
        /// <param name="username"> Rozpoznawalna nazwa użytkownika. </param>
        /// <param name="ip"> Adres protokołu internetowego serwera. </param>
        /// <param name="port"> Port protokołu sieciowego serwera. </param>
        /// <param name="ctype"> Rodzaj szyfrowania wiadomości. </param>
        public Client(string username, string ip, int port, CryptType ctype = 0)
        {
            this.Username  = username;
            this.ServerIP  = ip;
            this.Port      = port;
            this.cryptType = ctype;

            switch (this.cryptType)
            {
            case CryptType.RSA:
                this.encryptionServices = new ERSA();
                break;

            case CryptType.ElGamal:
                this.encryptionServices = new ElGamal();
                break;
            }
        }
 public ICryptor GetInstance(CryptType cryptType)
 {
     switch (cryptType)
     {
         case CryptType.None:
             return new MockCryptor();
         case CryptType.DES:
             return new DESCryptor();
         case CryptType.ThreeDES:
             return new TreepleDESCryptor();
         case CryptType.AES:
             return new AESCryptor();
         case CryptType.RSA:
             return new RSACryptor();
         default:
             throw new NotSupportedException("Crypt type is not supperted");
     }
 }
Beispiel #21
0
        private byte[] PerformEncryptionVariant1(byte[] block, byte[] roundKey, CryptType cryptType)
        {
            var q1 = new byte[block.Length / 4];
            var q2 = new byte[block.Length / 4];
            var q3 = new byte[block.Length / 4];
            var q4 = new byte[block.Length / 4];

            Array.Copy(block, q1, q1.Length);
            Array.Copy(block, q1.Length, q2, 0, q2.Length);
            Array.Copy(block, q1.Length + q2.Length, q3, 0, q3.Length);
            Array.Copy(block, q1.Length + q2.Length + q3.Length, q4, 0, q4.Length);

            var q2new = new byte[q2.Length];

            if (cryptType == CryptType.Encrypt)
            {
                q2new = ApplyRoundFunction(q1, roundKey);
                q2new = XOR(q2new, q2);
            }
            else
            {
                q2new = ApplyRoundFunction(q4, roundKey);
                q2new = XOR(q2new, q1);
            }

            var result = new byte[block.Length];

            if (cryptType == CryptType.Encrypt)
            {
                Array.Copy(q2new, result, q2new.Length);
                Array.Copy(q3, 0, result, q2new.Length, q3.Length);
                Array.Copy(q4, 0, result, q2new.Length + q3.Length, q4.Length);
                Array.Copy(q1, 0, result, q2new.Length + q3.Length + q4.Length, q1.Length);
            }
            else
            {
                Array.Copy(q4, result, q4.Length);
                Array.Copy(q2new, 0, result, q4.Length, q2new.Length);
                Array.Copy(q2, 0, result, q4.Length + q2new.Length, q2.Length);
                Array.Copy(q3, 0, result, q4.Length + q1.Length + q2new.Length, q3.Length);
            }
            return(result);
        }
Beispiel #22
0
        private char GetCryptedCharacter(
            int preMappedLetterPosition,
            CryptType cryptType,
            List <char> pattern)
        {
            char mappedLetter;

            switch (cryptType)
            {
            case CryptType.Encrypt:
                mappedLetter = pattern[(preMappedLetterPosition + 1) % pattern.Count];
                break;

            case CryptType.Decrypt:
                mappedLetter = pattern[(preMappedLetterPosition - 1 + pattern.Count) % pattern.Count];
                break;

            default:
                throw new ArgumentException("cryptType must be Encrypt or Decrypt");
            }

            return(mappedLetter);
        }
Beispiel #23
0
 public static string Encrypt(string encryptString, CryptType format)
 {
     switch (format)
     {
         default:
         case CryptType.MD5:
             MD5 md5Hasher = MD5.Create();
             byte[] md5data = md5Hasher.ComputeHash(Encoding.UTF8.GetBytes(encryptString));
             StringBuilder md5sBuilder = new StringBuilder();
             for (int i = 0; i < md5data.Length; i++)
                 md5sBuilder.Append(md5data[i].ToString("x2"));
             return md5sBuilder.ToString();
         case CryptType.SHA1:
             SHA1 sha1Hasher = SHA1.Create();
             byte[] sha1data = sha1Hasher.ComputeHash(Encoding.UTF8.GetBytes(encryptString));
             StringBuilder sha1sBuilder = new StringBuilder();
             for (int i = 0; i < sha1data.Length; i++)
                 sha1sBuilder.Append(sha1data[i].ToString("x2"));
             return sha1sBuilder.ToString();
         case CryptType.DES:
             return EncryptDES(encryptString);
     }
 }
Beispiel #24
0
        internal string Crypt(string cipher)
        {
            CryptType cryptType = SetCryptType(cipher);

            var cryptedCipher = new StringBuilder();

            if (cryptType == CryptType.Encrypt)
            {
                cipher = cipher.Substring(1);
            }

            for (int i = 0; i < cipher.Length; i++)
            {
                char cryptedCharacter = CryptCharacter(cipher[i], cryptType);

                SubstitutionBox = cryptType == CryptType.Encrypt ?
                                  PermuteSubstitutionBox(cipher[i], cryptedCharacter) :
                                  PermuteSubstitutionBox(cryptedCharacter, cipher[i]);

                if (cryptType == CryptType.Encrypt)
                {
                    MoveMarker(cryptedCharacter);
                }
                else
                {
                    MoveMarker(cipher[i]);
                }

                cryptedCipher.Append(cryptedCharacter);
            }

            ResetSubstitutionBox();
            ResetMarkedTile();

            return(cryptedCipher.ToString());
        }
        public static string AutoKeyCipher(string text, string key, ref string generateKey, ref AutoKeyCipher autoKeyCipher, AutoKeyCipherType autoKeyCipherType, CryptType cryptType)
        {
            string result = string.Empty;
            if (autoKeyCipher == null) autoKeyCipher = new AutoKeyCipher(key, autoKeyCipherType);
            switch (cryptType)
            {
                case CryptType.Encrypt:
                    result = autoKeyCipher.Encrypt(text);
                    generateKey = autoKeyCipher.PrimaryKey;
                    break;
                case CryptType.Decrypt:
                    result = autoKeyCipher.Decrypt(text);
                    generateKey = autoKeyCipher.PrimaryKey;
                    break;
            }

            return result;
        }
        /// <summary>
        /// Encrypt/Decypt the given cryptData with the given password
        /// </summary>
        /// <param name="cryptData">data to crypt</param>
        /// <param name="offset">offset of cryptData for crypt</param>
        /// <param name="count">length for crypt</param>
        /// <param name="cryptPwd">password string</param>
        /// <param name="keySalt">salt string</param>
        /// <param name="cryptType">crypt type</param>
        /// <returns>encrypted/decrypted data</returns>
        /// <remarks>if keySalt is null, then default keySalt is used</remarks>
        public static byte[] GetCrypt(byte[] cryptData,int offset, int count, string cryptPwd, byte[] keySalt, CryptType cryptType)
        {
            if (keySalt == null)
                keySalt = new byte[] { 0x54, 0x81, 0x45, 0x4A, 0x3B, 0x5E, 0x52, 0x15, 0x86, 0x5A, 0x40, 0x3B, 0xB4 };
            PasswordDeriveBytes pwdBytes = new PasswordDeriveBytes(cryptPwd, keySalt);

            Rijndael crypAlg = Rijndael.Create();
            crypAlg.Key = pwdBytes.GetBytes(32);
            crypAlg.IV = pwdBytes.GetBytes(16);

            try
            {
                ICryptoTransform cryptoTranform = (cryptType == CryptType.Encrypt) ? crypAlg.CreateEncryptor() : crypAlg.CreateDecryptor();

                MemoryStream memStream = new MemoryStream();
                CryptoStream cryptStream = new CryptoStream(memStream, cryptoTranform, CryptoStreamMode.Write);
                cryptStream.Write(cryptData, offset, count);
                cryptStream.FlushFinalBlock();
                return memStream.ToArray();
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message + " >" + ex.StackTrace);
            }
            return null;
        }
 /// <summary>
 /// Encrypt/Decypt the given cryptData string with the given password
 /// </summary>
 /// <param name="cryptData">string data to encrypt</param>
 /// <param name="cryptPwd">password string</param>
 /// <param name="cryptType">crypt type</param>
 /// <returns>encrypted/decrypted data</returns>
 public static string GetCrypt(string cryptData, string cryptPwd, CryptType cryptType)
 {
     return GetCrypt(cryptData, cryptPwd, null, cryptType);
 }
 /// <summary>
 /// Encrypt/Decypt the given cryptData with the given password
 /// </summary>
 /// <param name="cryptData">data to crypt</param>
 /// <param name="offset">offset of cryptData for crypt</param>
 /// <param name="count">length for crypt</param>
 /// <param name="cryptPwd">password string</param>
 /// <param name="cryptType">crypt type</param>
 /// <returns>encrypted/decrypted data</returns>
 public static byte[] GetCrypt(byte[] cryptData, int offset, int count, string cryptPwd, CryptType cryptType)
 {
     return GetCrypt(cryptData, offset, count, cryptPwd, null, cryptType);
 }
 /// <summary>
 /// Encrypt/Decypt the given cryptData with the given password
 /// </summary>
 /// <param name="cryptData">data to crypt</param>
 /// <param name="cryptPwd">password string</param>
 /// <param name="keySalt">salt string</param>
 /// <param name="cryptType">crypt type</param>
 /// <returns>encrypted/decrypted data</returns>
 /// <remarks>if keySalt is null, then default keySalt is used</remarks>
 public static byte[] GetCrypt(byte[] cryptData, string cryptPwd, byte[] keySalt, CryptType cryptType)
 {
     return GetCrypt(cryptData, 0, cryptData.Length, cryptPwd, keySalt, cryptType);
 }
Beispiel #30
0
 /// <summary>
 /// Encrypt/Decypt the given cryptData with the given password
 /// </summary>
 /// <param name="algoType">Algorithm type</param>
 /// <param name="cryptData">data to crypt</param>
 /// <param name="offset">offset of cryptData for crypt</param>
 /// <param name="count">length for crypt</param>
 /// <param name="cryptPwd">password string</param>
 /// <param name="keySalt">salt string</param>
 /// <param name="cryptType">crypt type</param>
 /// <returns>encrypted/decrypted data</returns>
 /// <remarks>if keySalt is null, then default keySalt is used</remarks>
 public static byte[] GetCrypt(CryptAlgo algoType, byte[] cryptData,int offset, int count, string cryptPwd, byte[] keySalt, CryptType cryptType)
 {
     byte[] retBytes = null;
     switch (algoType)
     {
         case CryptAlgo.Aes:
             retBytes = AesCrypt.GetCrypt(cryptData,offset,count, cryptPwd, keySalt, cryptType);
             break;
         case CryptAlgo.Rijndael:
             retBytes = RijndaelCrypt.GetCrypt(cryptData,offset,count, cryptPwd, keySalt, cryptType);
             break;
     }
     return retBytes;
 }
        public static string VermanCipher(string text, int keyLength, out string generatedKey, ref VermanCipher vermanCipher, CryptType cryptType)
        {
            string result = string.Empty;
            if (vermanCipher == null) vermanCipher = new VermanCipher(keyLength);
            generatedKey = vermanCipher.GeneratedKey;
            switch (cryptType)
            {
                case CryptType.Encrypt:
                    result = vermanCipher.Encrypt(text);
                    break;
                case CryptType.Decrypt:
                    result = vermanCipher.Decrypt(text);
                    break;
            }

            return result;
        }
Beispiel #32
0
 /// <summary>
 /// Encrypt/Decypt the given cryptData with the given password
 /// </summary>
 /// <param name="cryptData">data to crypt</param>
 /// <param name="cryptPwd">password string</param>
 /// <param name="keySalt">salt string</param>
 /// <param name="cryptType">crypt type</param>
 /// <returns>encrypted/decrypted data</returns>
 /// <remarks>if keySalt is null, then default keySalt is used</remarks>
 public static byte[] GetCrypt(byte[] cryptData, string cryptPwd, byte[] keySalt, CryptType cryptType)
 {
     return(GetCrypt(cryptData, 0, cryptData.Length, cryptPwd, keySalt, cryptType));
 }
Beispiel #33
0
        /// <summary>
        /// Encrypt/Decypt the given cryptData with the given password
        /// </summary>
        /// <param name="cryptData">data to crypt</param>
        /// <param name="offset">offset of cryptData for crypt</param>
        /// <param name="count">length for crypt</param>
        /// <param name="cryptPwd">password string</param>
        /// <param name="keySalt">salt string</param>
        /// <param name="cryptType">crypt type</param>
        /// <returns>encrypted/decrypted data</returns>
        /// <remarks>if keySalt is null, then default keySalt is used</remarks>
        public static byte[] GetCrypt(byte[] cryptData, int offset, int count, string cryptPwd, byte[] keySalt, CryptType cryptType)
        {
            if (keySalt == null)
            {
                keySalt = new byte[] { 0x54, 0x81, 0x45, 0x4A, 0x3B, 0x5E, 0x52, 0x15, 0x86, 0x5A, 0x40, 0x3B, 0xB4 }
            }
            ;
            PasswordDeriveBytes pwdBytes = new PasswordDeriveBytes(cryptPwd, keySalt);

            Rijndael crypAlg = Rijndael.Create();

            crypAlg.Key = pwdBytes.GetBytes(32);
            crypAlg.IV  = pwdBytes.GetBytes(16);

            try
            {
                ICryptoTransform cryptoTranform = (cryptType == CryptType.Encrypt) ? crypAlg.CreateEncryptor() : crypAlg.CreateDecryptor();

                MemoryStream memStream = new MemoryStream();
                using (CryptoStream cryptStream = new CryptoStream(memStream, cryptoTranform, CryptoStreamMode.Write))
                {
                    cryptStream.Write(cryptData, offset, count);
                    cryptStream.FlushFinalBlock();
                    return(memStream.ToArray());
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message + " >" + ex.StackTrace);
            }
            return(null);
        }
    }
        /// <summary>
        /// Apply Encryption
        /// </summary>
        public void Apply(CryptType Crypt, CString Buffer)
        {
            // Apply the correct decryption algorithm
            switch (mGeneration)
            {
            // No Encryption
                case Generation.GEN1:
                case Generation.GEN2:
                    return;

            // Single byte insertion/zlib compression
                case Generation.GEN3:
                    mIterator[(Int32)Crypt] *= 0x8088405;
                    mIterator[(Int32)Crypt] += mKey;
                    Buffer.Remove((((Int32)mIterator[(Int32)Crypt] & 0x0FFFF) % Buffer.Length), 1);
                    break;

            // Partial packet encryption/none, zlib, bz2 compression methods.
            // Gen 4 is only bz2.
                case Generation.GEN4:
                case Generation.GEN5:
                    for (int i = 0; i < Buffer.Length; i++)
                    {
                        if (i % 4 == 0)
                        {
                            if (mLimit == 0)
                                return;

                            mIterator[(Int32)Crypt] *= 0x8088405;
                            mIterator[(Int32)Crypt] += mKey;
                            if (mLimit > 0)
                                mLimit--;
                        }

                        Buffer[i] ^= (Byte)((mIterator[(Int32)Crypt] >> ((i % 4) * 8)) & 0xFF);
                    }
                    break;

            // Future Encryption Method (Graal V6)
                case Generation.GEN6:
                    break;
            }
        }
Beispiel #35
0
 /// <summary>
 /// Encrypt/Decypt the given cryptData with the given password
 /// </summary>
 /// <param name="cryptData">data to crypt</param>
 /// <param name="offset">offset of cryptData for crypt</param>
 /// <param name="count">length for crypt</param>
 /// <param name="cryptPwd">password string</param>
 /// <param name="cryptType">crypt type</param>
 /// <returns>encrypted/decrypted data</returns>
 public static byte[] GetCrypt(byte[] cryptData, int offset, int count, string cryptPwd, CryptType cryptType)
 {
     return(GetCrypt(cryptData, offset, count, cryptPwd, null, cryptType));
 }
 /// <summary>
 /// Convenience Function -> Limit/Apply Encryption
 /// </summary>
 public void Apply(CryptType Crypt, CString Buffer, Type Type)
 {
     Limit(Type);
     Apply(Crypt, Buffer);
 }
 public BlowFish(CryptType type)
 {
     randomSource = new RNGCryptoServiceProvider();
     string keyString = type == CryptType.Encrypt ? "6#26FRL$ZWD" : "R=U!LH$O2B#";
     SetupKey(System.Text.Encoding.UTF8.GetBytes(keyString));
 }
        public static string TranspositionCipher(string text, string transpositionArray, CryptType cryptType)
        {
            string result = string.Empty;
            var strArray = transpositionArray.Split(' ');
            var keyArray = new int[strArray.Length];
            for (int i = 0; i < strArray.Length; i++) keyArray[i] = int.Parse(strArray[i]);
            transpositionCipher = new TranspositionCipher(keyArray);
            switch (cryptType)
            {
                case CryptType.Encrypt:
                    result = transpositionCipher.Encrypt(text);
                    break;
                case CryptType.Decrypt:
                    result = transpositionCipher.Decrypt(text);
                    break;
            }

            return result;
        }
        public static string NGrammarSubstitutionCipher(string text, out string showText, ref NGrammarSubstitutionCipher grammarSubstitutionCipher, CryptType cryptType)
        {
            string result = string.Empty;
            if (grammarSubstitutionCipher == null) grammarSubstitutionCipher = new NGrammarSubstitutionCipher();
            var dictionary = grammarSubstitutionCipher.GetGrammarDictionary();
            showText = string.Empty;
            int index = 0;
            foreach (var kvp in dictionary)
            {
                showText += kvp.Key + "=" + kvp.Value + "; ";
                index++;
                if (index == 26)
                {
                    showText += "\n";
                    index = 0;
                }
            }
            switch (cryptType)
            {
                case CryptType.Encrypt:
                    result = grammarSubstitutionCipher.Encrypt(text);
                    break;
                case CryptType.Decrypt:
                    result = grammarSubstitutionCipher.Decrypt(text);
                    break;
            }

            return result;
        }
Beispiel #40
0
 /// <summary>
 /// Encrypt/Decypt the given cryptData string with the given password
 /// </summary>
 /// <param name="cryptData">string data to encrypt</param>
 /// <param name="cryptPwd">password string</param>
 /// <param name="cryptType">crypt type</param>
 /// <returns>encrypted/decrypted data</returns>
 public static string GetCrypt(string cryptData, string cryptPwd, CryptType cryptType)
 {
     return(GetCrypt(cryptData, cryptPwd, null, cryptType));
 }
Beispiel #41
0
 /// <summary>
 /// Encrypt/Decypt the given cryptData with the given password
 /// </summary>
 /// <param name="cryptData">data to crypt</param>
 /// <param name="cryptPwd">password string</param>
 /// <param name="cryptType">crypt type</param>
 /// <returns>encrypted/decrypted data</returns>
 public static byte[] GetCrypt(CryptAlgo algoType, byte[] cryptData, string cryptPwd, CryptType cryptType)
 {
     byte[] retBytes = null;
     switch (algoType)
     {
         case CryptAlgo.Aes:
             retBytes = AesCrypt.GetCrypt(cryptData, cryptPwd, null, cryptType);
             break;
         case CryptAlgo.Rijndael:
             retBytes = RijndaelCrypt.GetCrypt(cryptData, cryptPwd, null, cryptType);
             break;
     }
     return retBytes;
 }
Beispiel #42
0
        /// <summary>
        /// Encrypt/Decypt the given cryptData string with the given password
        /// </summary>
        /// <param name="cryptData">string data to encrypt</param>
        /// <param name="cryptPwd">password string</param>
        /// <param name="cryptType">crypt type</param>
        /// <returns>encrypted/decrypted data</returns>
        /// <remarks>if keySalt is null, then default keySalt is used</remarks>
        public static string GetCrypt(string cryptData, string cryptPwd, byte[] keySalt, CryptType cryptType)
        {
            try
            {
                byte[] cryptBytes = null;
                switch (cryptType)
                {
                case CryptType.Encrypt:
                    cryptBytes = Encoding.Unicode.GetBytes(cryptData);
                    break;

                case CryptType.Decrypt:
                    cryptBytes = Convert.FromBase64String(cryptData);
                    break;
                }

                byte[] retData = GetCrypt(cryptBytes, cryptPwd, keySalt, cryptType);
                switch (cryptType)
                {
                case CryptType.Encrypt:
                    return(Convert.ToBase64String(retData));

                case CryptType.Decrypt:
                    return(Encoding.Unicode.GetString(retData));
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message + " >" + ex.StackTrace);
            }
            return(null);
        }
Beispiel #43
0
         /// <summary>
 /// Encrypt/Decypt the given cryptData string with the given password
 /// </summary>
 /// <param name="cryptData">string data to encrypt</param>
 /// <param name="cryptPwd">password string</param>
 /// <param name="cryptType">crypt type</param>
 /// <returns>encrypted/decrypted data</returns>
 public static string GetCrypt(CryptAlgo algoType,string cryptData, string cryptPwd, CryptType cryptType)
 {
     string retString = null;
     switch (algoType)
     {
         case CryptAlgo.Aes:
             retString = AesCrypt.GetCrypt(cryptData, cryptPwd, null, cryptType);
             break;
         case CryptAlgo.Rijndael:
             retString = RijndaelCrypt.GetCrypt(cryptData, cryptPwd, null, cryptType);
             break;
     }
     return retString;
 }
        public static string VigenereCipher(string text, string key, VigenereCipherType vigenereCipherType, CryptType cryptType)
        {
            string result = string.Empty;
            vigenereCipher = new VigenereCipher(key.Split(' '), vigenereCipherType);
            switch (cryptType)
            {
                case CryptType.Encrypt:
                    result = vigenereCipher.Encrypt(text);
                    break;
                case CryptType.Decrypt:
                    result = vigenereCipher.Decrypt(text);
                    break;
            }

            return result;
        }