字符串的加密,解密;加密规则为:字符串中的每个字符加4,之后字符串进行倒置; 如用户输入为“abcd“,
3个回答
展开全部
System.Console.WriteLine("请选择输入要加密的字符串(输入1)还是输入要解密的字符串(输入2):");
int temp = int.Parse(System.Console.ReadLine());
if (temp == 1)
{
System.Console.WriteLine("请输入要加密的字符串:");
String str = System.Console.ReadLine(), strc = "";
char[] chs = new char[str.Length];
int j = 0;
foreach (char ch in str)
{
chs[j++] = ch;
}
for (j = 0; j < chs.Length; j++)
{
chs[j] = Convert.ToChar(Convert.ToInt32(chs[j]) + 4);
}
for (j = chs.Length - 1; j >= 0; j--)
{
strc = strc + chs[j];
}
System.Console.WriteLine("加密后的字符串:");
System.Console.WriteLine(strc);
}
else if (temp == 2)
{
System.Console.WriteLine("请输入要解密的字符串:");
String str = System.Console.ReadLine(), strc = "";
char[] chs = new char[str.Length];
int j = 0;
foreach (char ch in str)
{
chs[j++] = ch;
}
for (j = 0; j < chs.Length; j++)
{
chs[j] = Convert.ToChar(Convert.ToInt32(chs[j]) - 4);
}
for (j = chs.Length - 1; j >= 0; j--)
{
strc = strc + chs[j];
}
System.Console.WriteLine("解密后的字符串:");
System.Console.WriteLine(strc);
}
else
{
System.Console.WriteLine("输入有误,退出");
return;
}
System.Console.WriteLine();
int temp = int.Parse(System.Console.ReadLine());
if (temp == 1)
{
System.Console.WriteLine("请输入要加密的字符串:");
String str = System.Console.ReadLine(), strc = "";
char[] chs = new char[str.Length];
int j = 0;
foreach (char ch in str)
{
chs[j++] = ch;
}
for (j = 0; j < chs.Length; j++)
{
chs[j] = Convert.ToChar(Convert.ToInt32(chs[j]) + 4);
}
for (j = chs.Length - 1; j >= 0; j--)
{
strc = strc + chs[j];
}
System.Console.WriteLine("加密后的字符串:");
System.Console.WriteLine(strc);
}
else if (temp == 2)
{
System.Console.WriteLine("请输入要解密的字符串:");
String str = System.Console.ReadLine(), strc = "";
char[] chs = new char[str.Length];
int j = 0;
foreach (char ch in str)
{
chs[j++] = ch;
}
for (j = 0; j < chs.Length; j++)
{
chs[j] = Convert.ToChar(Convert.ToInt32(chs[j]) - 4);
}
for (j = chs.Length - 1; j >= 0; j--)
{
strc = strc + chs[j];
}
System.Console.WriteLine("解密后的字符串:");
System.Console.WriteLine(strc);
}
else
{
System.Console.WriteLine("输入有误,退出");
return;
}
System.Console.WriteLine();
展开全部
JAVA实现:
public class Cat {
public static void main(String[] args) {
String str = "abcd";
String encrypt = encrypt(str);
String decode = decode(encrypt);
System.out.println(str + " -->Encryed string is: " + encrypt);
System.out.println(encrypt + "--> Original string is: " + decode);
}
private static String encrypt(String str) {
StringBuilder sb = new StringBuilder();
for(int i = 0; i < str.length(); i++){
char c = str.charAt(i);
int cAdded = ((int)c) + 4;
sb.append((char)cAdded);
}
return sb.reverse().toString();
}
private static String decode(String str) {
StringBuilder sb = new StringBuilder();
for(int i = 0; i < str.length(); i++){
char c = str.charAt(i);
int value = (int)c - 4;
sb.append((char)value);
}
return sb.reverse().toString();
}
}
----------------
abcd -->Encryed string is: hgfe
hgfe--> Original string is: abcd
public class Cat {
public static void main(String[] args) {
String str = "abcd";
String encrypt = encrypt(str);
String decode = decode(encrypt);
System.out.println(str + " -->Encryed string is: " + encrypt);
System.out.println(encrypt + "--> Original string is: " + decode);
}
private static String encrypt(String str) {
StringBuilder sb = new StringBuilder();
for(int i = 0; i < str.length(); i++){
char c = str.charAt(i);
int cAdded = ((int)c) + 4;
sb.append((char)cAdded);
}
return sb.reverse().toString();
}
private static String decode(String str) {
StringBuilder sb = new StringBuilder();
for(int i = 0; i < str.length(); i++){
char c = str.charAt(i);
int value = (int)c - 4;
sb.append((char)value);
}
return sb.reverse().toString();
}
}
----------------
abcd -->Encryed string is: hgfe
hgfe--> Original string is: abcd
追问
C#
追答
我不会用C#哦。。
不过算法C#也类似。取得每一个字符,加密的时候+4,然后反向。解密的时候每个字符-4,然后再反向
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
expression是一个长度为零的字符串(""),Split则返回一个空数组,即没有元素和数据的数组。
写得一个方法,调用就好
private string Code(string s)
{
int length = s.Length;
char[] code = new char[100];
string final = "";
for (int i = 0; i < s.Length; i++)
{
length--;
code[length] = (char)(s[i] + 4);
}
for (int j = 0; j < s.Length; j++)
{
final += code[j];
}
return final.Trim();
}
写得一个方法,调用就好
private string Code(string s)
{
int length = s.Length;
char[] code = new char[100];
string final = "";
for (int i = 0; i < s.Length; i++)
{
length--;
code[length] = (char)(s[i] + 4);
}
for (int j = 0; j < s.Length; j++)
{
final += code[j];
}
return final.Trim();
}
追问
纠结 我怎么想不到
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询