设计一个方法 可以将字符串按下面要求加密:
字符串最后一个字母放在加密后的第一位,其它字母的值偏移3.
主程序负责接收用户键入的字符串,之后调用该方法加密,加密后主程序显示在屏幕上.
例: 明文=“Welcome” 密文=“eZhofrp”
(注意:A-Z和a-z之间还有很多ASII字符,所以要用到模运算)
字符串最后一个字母放在加密后的第一位,其它字母的值偏移3.
主程序负责接收用户键入的字符串,之后调用该方法加密,加密后主程序显示在屏幕上.
例: 明文=“Welcome” 密文=“eZhofrp”
(注意:A-Z和a-z之间还有很多ASII字符,所以要用到模运算)
ArrayList al = new ArrayList();
for (int i = 0; i < 明文.Length - 1; i++)
{
al.Add((char)(明文[i] + 3));
}
al.Insert(0,明文[明文.Length - 1]);
string 密文 = new string ((char[])al.ToArray(typeof(char)));
//大概是这么个意思 自己根据效率调整
for(int 0 =1;i<str.Length-1;i++)
str[i]=(char)(((int)str[i]+3)%128);
str=str.SubString(str.Length-2)+str.SubString(0,str.Length-1)
str[i]=(char)(((int)str[i]+3)%128);
str=str.SubString(str.Length-2)+str.SubString(0,str.Length-1)
{
if (string.IsNullOrEmpty(targestr))
return targestr;
targestr = targestr.Trim();
char[] list = targestr.ToCharArray ();
char b = list[list.Length - 1];
char a = list[0];
char c = list[0];
for (int i = 0; i < list.Length-1; i++)
{
c=(char)( (int)a+3);
if (list[i] > 'Z' && list[i] < 'a')
list[i] = 'A';
else
{
if( list[i] > 'z')
list[i] = 'a';
}
a = list[i + 1];
list[i + 1] = c; }
list[0] = b;
return new string(list);
}
已经过测试.
因为楼主么有说清楚,如果是X则加密后为A,Y为B ,等等z为c; private string methord(string targestr)
{
if (string.IsNullOrEmpty(targestr))
return targestr;
targestr = targestr.Trim();
char[] list = targestr.ToCharArray ();
char b = list[list.Length - 1];
char a = list[0];
char c = list[0];
for (int i = 0; i < list.Length-1; i++)
{
c=(char)( (int)a+3);
if (c > 'Z' && c < 'a')
c =(char )( 'A'+c-'Z'-1);
else
{
if( c > 'z')
c = (char )('a'+c-'z'-1);
}
a = list[i + 1];
list[i + 1] = c; }
list[0] = b;
return new string(list);
}
{
class Program
{
static void Main(string[] args)
{
string pwdLibrary = "defghijklmnopqrstuvwxyzabcDEFGHIJKLMNOPQRSTUVWXYZABC";
string test = "sahjdshHDSGHDc";
string pwd = test[test.Length-1].ToString();
for (int i = 0; i < test.Length - 1; i++)
{
char c = test[i];
pwd += (c >= 'a' ? pwdLibrary[c - 'a'].ToString() : pwdLibrary[26+c - 'A'].ToString());
}
Console.WriteLine(pwd);
}
}
}
{
public const int offset = 3;
static void Main()
{
string laymu = "Welcome";
Program p=new Program();
Console.WriteLine("The converted string is: {0}", p.getEcho(laymu));
Console.ReadLine();
} public string getEcho(string str)//装换字符串
{
char[] ch = str.ToCharArray();
char[] result = new char[ch.Length];
result[0] = ch[ch.Length - 1];
try
{
for (int i = 0; i < ch.Length - 1; i++)
{
result[i + 1] = Program.getChar(ch[i]);
}
}
catch (IndexOutOfRangeException e)
{
Console.WriteLine(e.Message);
} string s = new string(result);
return s;
} public static char getChar(char c)//转换一个字符
{
char result=' ';
if ('A' <= c && c <= 'Z')
{
result=(char)((c-'A'+offset)%26+'A');
}
if ('a' <= c && c <= 'z')
{
result=(char)((c-'a'+offset)%26+'a');
}
return result;
}
}