C语言凯撒加密

 public string Caesar(int key, string str) {
            char[] c = str.ToCharArray();
            string strCaesar = "";
        for (int i = 0; i < str.Length; i++) {
            string ins = c[i].ToString();
            string outs = "";
            bool isChar = str.Contains(ins.ToLower());
            bool isToUpperChar = isChar && (ins.ToUpper() == ins);
            ins = ins.ToLower();
            if (isChar) {
                int offset = (Ascll(ins) + key - Ascll("a")) % (Ascll("z") - Ascll("a") + 1);
                outs = Convert.ToChar(offset + Ascll("a")).ToString();
                if (isToUpperChar) {
                    outs = outs.ToUpper();
                }
            } else {
                outs = ins;
            }
            strCaesar += outs;
        }
            for (int i = 0; i < str.Length; i++) {
                c[i] += key;
            }
                return strCaesar;
        }

        // 字符转ascll
        public int Ascll(string str) {
            byte[] array = new byte[1];
            array = System.Text.Encoding.ASCII.GetBytes(str);
            int asciicode = (short)(array[0]);
            return asciicode;
        }

你可能感兴趣的:(c/c++)