コード例 #1
0
ファイル: Cipher.cs プロジェクト: sparkydasrath/portfolio
        public string Encrypt(string s)
        {
            sb.Clear();
            if (s == null)
            {
                return(null);
            }
            sb.Capacity = s.Length;

            for (int i = 0; i < s.Length; i++)
            {
                if (IsSymbol(s[i]))
                {
                    sb.Append(s[i]); // ignore symbols
                    continue;
                }

                if (shiftedCache.ContainsKey(s[i]))
                {
                    sb.Append(shiftedCache[s[i]]);
                }
                else
                {
                    CharDetails cd          = GetCharDetails(s[i]);
                    char        shiftedChar = GetShiftedCharacter(cd);
                    sb.Append(shiftedChar);
                    shiftedCache[cd.AsciiCode] = shiftedChar;
                }
            }

            return(sb.ToString());
        }
コード例 #2
0
ファイル: Cipher.cs プロジェクト: sparkydasrath/portfolio
        private char GetShiftedCharacter(CharDetails cd)
        {
            // no need to check for symbols as we filter those out already

            int shiftedResult = cd.AsciiCode + shift;

            if (cd.CharType == CharType.Number && shiftedResult > AsciiConstants.CeilNumber)
            {
                int overflowShift = GetOverflowShift(AsciiConstants.FloorNumber, shiftedResult, NumberDivider);

                shiftedResult = overflowShift;
            }
            else if (cd.CharType == CharType.Lower && shiftedResult > AsciiConstants.CeilLowerCase)
            {
                int overflowShift = GetOverflowShift(AsciiConstants.FloorLowerCase, shiftedResult, LetterDivider);
                shiftedResult = overflowShift;
            }
            else if (cd.CharType == CharType.Upper && shiftedResult > AsciiConstants.CeilUpperCase)
            {
                int overflowShift = GetOverflowShift(AsciiConstants.FloorUpperCase, shiftedResult, LetterDivider);
                shiftedResult = overflowShift;
            }

            return(Convert.ToChar(shiftedResult));
        }