using System; using System.Collections.Generic; using System.Linq; using System.Text;namespace encreption { class Program { static void Main(string[] args) { //thinking way : char => int (process them in int ) => char (back to char) //ezhofrp char [] c = {'e','z','h','o','f','r','p',};
int[] c_int = new int[c.Length]; char[] output = new char[c.Length+1]; output [output.Length -2] = c[0]; for (int i = 1; i <= c.Length - 1;i++ ) { if ((c[i] >= 'a' && c[i] <= 'z') || (c[i] >= 'A' && c[i] >= 'Z')) { c_int[i]= (int)c [i]- 3; if (c_int[i] > 90 && c_int[i] <= 87 || c_int[i] > 122) { c_int[i] -= 26; } output [i-1] = (char)c_int[i]; }
{
if (string.IsNullOrEmpty(str))
{
return "";
}
byte[] buff = Encoding.Default.GetBytes(str);
int len=mstr.Length;
StringBuilder sb = new StringBuilder();
sb.Append(buff[i-1].ToString());
for (int i = 0; i < buff.Length; i++)
{
j = (int)buff[i];
j=+3;
sb.Append((char)j);//要考虑是否超出范围
}
return sb.ToString();
}
{
StringBuilder strS=new StringBuilder(str.Substring(str.Length-1).ToString());
char[] str1=str.Substring(0,str.Length-1).ToCharArray();
int temp;
for (int i = 0; i < str1.Length; i++)
{
temp=(int)str1[i]+3;
if (temp <= 122)//z的ASCII码值122
{
strS.Append((char)temp);
}
else
{
int sub = temp - 122;
strS.Append((char)(96 + sub));//a的ASCII码值96
}
}
return strS.ToString();
}
以上算法是加密前后字符串个数不变!
这样加密之后解密就不容易了,因为加密时最后一个字符舍弃了。
让各位费心了,加分以表歉意!
using System.Collections.Generic;
using System.Linq;
using System.Text;namespace encreption
{
class Program
{ static void Main(string[] args)
{
//thinking way : char => int (process them in int ) => char (back to char)
//ezhofrp
char [] c = {'e','z','h','o','f','r','p',};
int[] c_int = new int[c.Length];
char[] output = new char[c.Length+1];
output [output.Length -2] = c[0]; for (int i = 1; i <= c.Length - 1;i++ )
{
if ((c[i] >= 'a' && c[i] <= 'z') || (c[i] >= 'A' && c[i] >= 'Z'))
{
c_int[i]= (int)c [i]- 3;
if (c_int[i] > 90 && c_int[i] <= 87 || c_int[i] > 122)
{
c_int[i] -= 26;
}
output [i-1] = (char)c_int[i];
}
}
for (int j = 0;j <= output .Length -1; j++)
Console.Write("{0}", output[j]);
}
}
}