- //对字符串按照规则进行加密,返回加密后的字符串
- static string Encrypt(string str)
- {
- //将字符串转换成char数组
- char[] charArr = str.ToCharArray();
- //创建一个新的char数组用于存放完成加密的char
- char[]encrypeCharArr=new char[charArr.Length];
- //依次对单个字符进行加密
- for (int i = 0; i < charArr.Length; i++)
- {
- //将字符转为ascii编码值
- int charNumber = charArr[i];
- //当字符处于a~x或A~X之间时,将字符编码增加3
- if ((charNumber >= 'a' && charNumber < 'x') || (charNumber >= 'A' && charNumber < 'X'))
- {
- encrypeCharArr[i] = (char) (charNumber + 3);
- }
- //当字符处于x~z或X~Z之间时,将字符编码减去23
- else if ((charNumber >= 'x' && charNumber <= 'z') || (charNumber >= 'X' && charNumber <= 'Z'))
- {
- encrypeCharArr[i] = (char) (charNumber - 23);
- }
- //其它的字符不进行加密
- else
- {
- encrypeCharArr[i] = (char)charNumber;
- }
- }
- //将加密后的字符数组转成字符串返回
- return new string(encrypeCharArr);
- }
复制代码 |