⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 column.cs

📁 使用viginear,column permutation,DES加密和解密
💻 CS
字号:
using System;
using System.Collections.Generic;
using System.Text;

namespace MyCAP
{
    class Column
    {
        public string GetCipher(string PlainText, string Key)
        {
            int cnt, i, j, tem;
            int[] CNum = new int[10]; ;
            string CipherText = null;
            string[] matrix = new string[10];
            string[] matrix2 = new string[10];

            while (PlainText.Length % Key.Length != 0)//将数组凑满
            {
                PlainText += 'z';//填充惰性字符z
            }
            for (i = 0; i < Key.Length; i++)//得到密钥中包含的列置换顺序
            {
                cnt = 1;
                tem = Key[i];
                for (j = 0; j < Key.Length; j++)
                {
                    if (tem > Key[j])
                        ++cnt;
                }
                CNum[i] = cnt;
            }
            tem = 0;
            for (j = 0; j < PlainText.Length / Key.Length; j++)//将明文存入矩阵
                for (i = 0; i < Key.Length; i++)
                    matrix[i] += PlainText[tem++];

            for (i = 0; i < Key.Length; i++)//进行列置换操作
                matrix2[i] = matrix[CNum[i] - 1];
            for (i = 0; i < Key.Length; i++)//将置换后的密文写入字符串
                for (j = 0; j < matrix[0].Length; j++)
                    CipherText += matrix2[i][j];

            return CipherText;
        }

        public string GetPlain(string CipherText, string Key)
        {
            int cnt, i, j, tem;
            int[] CNum = new int[10]; ;
            string PlainText = null;
            string[] matrix = new string[10];
            string[] matrix2 = new string[10];
            for (i = 0; i < Key.Length; i++)//得到密钥内包含的列置换信息
            {
                cnt = 1;
                tem = Key[i];
                for (j = 0; j < Key.Length; j++)
                {
                    if (tem > Key[j])
                        ++cnt;
                }
                CNum[i] = cnt;
            }
            tem = 0;                        //将密文写入矩阵,以便列置换
            for (i = 0; i < Key.Length; i++)
                for (j = 0; j < CipherText.Length / Key.Length; j++)
                    matrix[i] += CipherText[tem++];
            for (i = 0; i < Key.Length; i++)//将矩阵进行置换
            {
                matrix2[CNum[i] - 1] = matrix[i];
            }


            for (i = 0; i < matrix2[0].Length; i++)//将还原后的明文输入字符串
                for (j = 0; j < Key.Length; j++)
                    PlainText += matrix2[j][i];


            return PlainText;
        }
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -