泛型List长度可变,所以不用数组存储字符串;str.Replace()用来去掉字符串空格;for循环(i+=3)保证3位一批;if(i+2<str.Length)保证字符串字符即使不足3个,也不会超出List集合界限;list.Add(str.Substring(i,3))用来截取字符串三位数并赋给泛型list; 泛型遍历条件是list.Count; (char)数值——把一个数值转换为字符。
Console.Write("请输入要解密的内容:");
string str = Console.ReadLine();
str = str.Replace(" ", "");
List<string> list = new List<string>();
for (int i = 0; i < str.Length; i += 3)
{
if (i + 2 < str.Length)
{
list.Add(str.Substring(i, 3));
}
}
try
{
for (int j = 0; j < list.Count; j++)
Console.Write((char)Convert.ToInt32(list[j], 8));
}
catch (Exception ex)
{
Console.Write(ex.Message);
}
Console.WriteLine();
数据的编码方式为ASCII(ASCII我认为是错的,现在未知2017.12.20)(基于输入3位数字)
Console.WriteLine("请输入要加密的内容:");
string str = Console.ReadLine();
char char1;
string str1;
List<string> list = new List<string>();
int k, l, n;
string m;
for (int i = 0; i < str.Length; i += 1)
{
char1 = Convert.ToChar(str.Substring(i, 1));
str1 = Convert.ToString(char1, 8);
if (str1.Length < 3)
{
str1 = "0" + str1;
list.Add(str1);
}
else list.Add(str1);
}
try
{
if (list.Count % 2 == 0)
{
l = list.Count - 1;
for (k = 0; k < list.Count / 2; k++)
{
m = list[k];
list[k] = list[l];
list[l] = m;
--l;
}
}
else
{
l = list.Count - 2;
for (n = 0; n < (list.Count - 1) / 2; n++)
{
m = list[n];
list[n] = list[l];
list[l] = m;
--l;
}
}
}
catch (Exception ex)
{
Console.Write(ex.Message);
}
Console.WriteLine("下面是加密后的内容:");
for (int j = 0; j < list.Count; j++)
{
Console.Write(list[j]);
}
Console.ReadKey();
Console.WriteLine("请输入要解密的内容:");
string str = Console.ReadLine();
int k, l, n;
string m;
str = str.Replace(" ", "");
List<string> list = new List<string>();
for (int i = 0; i < str.Length; i += 3)
{
if (i + 2 < str.Length)
{
list.Add(str.Substring(i, 3));
}
}
try
{
if (list.Count % 2 == 0)
{
l = list.Count - 1;
for (k = 0; k < list.Count / 2; k++)
{
m = list[k];
list[k] = list[l];
list[l] = m;
--l;
}
}
else
{
l = list.Count - 2;
for (n = 0; n < (list.Count - 1) / 2; n++)
{
m = list[n];
list[n] = list[l];
list[l] = m;
--l;
}
}
Console.WriteLine("下面是已解密的内容:");
for (int j = 0; j < list.Count; j++)
{
Console.Write((char)Convert.ToInt32(list[j], 8));
}
}
catch (Exception ex)
{
Console.Write(ex.Message);
}
Console.ReadKey();
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApp1
{
class Program
{
static void Main(string[] args)
{
try
{
Console.WriteLine("请输入要加密的内容:");
string str = Console.ReadLine();
char char1;
string str1;
List<string> list = new List<string>();
int k, l, n;
string m;
for (int i = 0; i < str.Length; i += 1)
{
char1 = Convert.ToChar(str.Substring(i, 1));
str1 = Convert.ToString(char1, 8);
if (str1.Length < 2)
{
str1 = "00000" + str1;
list.Add(str1);
}
else if (str1.Length < 3)
{
str1 = "0000" + str1;
list.Add(str1);
}
else if (str1.Length < 4)
{
str1 = "000" + str1;
list.Add(str1);
}
else if (str1.Length < 5)
{
str1 = "00" + str1;
list.Add(str1);
}
else if (str1.Length < 6)
{
str1 = "0" + str1;
list.Add(str1);
}
else list.Add(str1);
}
if (list.Count % 2 == 0)
{
l = list.Count - 1;
for (k = 0; k < list.Count / 2; k++)
{
m = list[k];
list[k] = list[l];
list[l] = m;
--l;
}
}
else
{
l = list.Count - 2;
for (n = 0; n < (list.Count - 1) / 2; n++)
{
m = list[n];
list[n] = list[l];
list[l] = m;
--l;
}
}
}
catch (Exception ex)
{
Console.Write(ex.Message);
}
Console.WriteLine("下面是加密后的内容:");
for (int j = 0; j < list.Count; j++)
{
Console.Write(list[j]);
}
Console.ReadKey();
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApp1
{
class Program
{
static void Main(string[] args)
{
int k, l, n;
string m;
Console.WriteLine("请输入要解密的内容:");
string str = Console.ReadLine();
str = str.Replace(" ", "");
List<string> list = new List<string>();
for (int i = 0; i < str.Length; i += 6)
{
if (i + 5 < str.Length)
{
list.Add(str.Substring(i, 6));
}
}
try
{
if (list.Count % 2 == 0)
{
l = list.Count - 1;
for (k = 0; k < list.Count / 2; k++)
{
m = list[k];
list[k] = list[l];
list[l] = m;
--l;
}
}
else
{
l = list.Count - 2;
for (n = 0; n < (list.Count - 1) / 2; n++)
{
m = list[n];
list[n] = list[l];
list[l] = m;
--l;
}
}
Console.WriteLine("下面是已解密的内容:");
for (int j = 0; j < list.Count; j++)
{
Console.Write((char)Convert.ToInt32(list[j], 8));
}
}
catch (Exception ex)
{
Console.Write(ex.Message);
}
Console.WriteLine();
}
}
}