Esempio n. 1
0
 void MaHoa()
 {
     key = new Khoa(textBoxKEY.Text);
     if (key.KiemTraKhoa())
     {
         string text = textBoxText.Text;
         des = new Des();
         string cipher = des.MaHoa(text, key, chose: MaHoaOrGiaMa);
         textBoxReturn.Text = cipher;
     }
     else
     {
         MessageBox.Show("Key không hợp lệ!");
     }
 }
Esempio n. 2
0
        static void Main(string[] args)
        {
            Console.OutputEncoding = Encoding.Unicode;

            Des des = new Des();

            WriteLine("Input string: ");
            string str = ReadLine();

            str = des.ToRigthLength(str);
            Console.WriteLine($"input to right length: {str}");
            string shifr = "";

            foreach (string s in des.MakeBlocks4Letter(str))
            {
                shifr += des.Shifr(s, des.SdvigLeft, "home");
            }
            WriteLine(shifr);
            WriteLine("\n\n");
            WriteLine("desh");
            string        deshifr = "";
            List <string> dmb     = des.MakeBlocks64Bit(shifr);

            for (int i = 0; i < dmb.Count; i++)
            {
                deshifr += des.Desifr(dmb[i], des.SdvigLeft, i);
            }
            WriteLine(deshifr + "\n\n\n");

            StringBuilder check1 = new StringBuilder();
            StringBuilder check2 = new StringBuilder();

            for (int i = 0; i < 64; i++)
            {
                check1.Append('0');
                check2.Append('0');
            }
            check2[63] = '1';
            des.CheckL(check1.ToString(), check2.ToString(), "home");
        }
Esempio n. 3
0
 //通信使用报文
 public string SendPackage(string msg,string DesKey)
 {
     string result = null;
     string cipher = null;
     string cipherKey = null;
     Des d = new Des();
     int l;
     if((l=DesKey.Length)!=8)
     {
         for(int i=l;i<8;i++)
         {
             DesKey += "0";
         }
     }
     cipher=d.Encrypy(msg,DesKey);
     cipherKey = ser.Encrypy(DesKey, SerEkey,SerNkey);
     result = "s;" + cipherKey + ";" + cipher;
     return result;
 }
Esempio n. 4
0
 //会话请求拆包
 public Cer getClient(string msg,Cer data)
 {
     Cer a = new Cer();
     a = data;
     string m=null;
     string[] array = msg.Split(';');
     int lengthStr = array.Length;
        //接受加密会话信息
        if(array[0]=="s")
        {
        if(lengthStr!=3)
        {
            //请求报文缺失
            a.other = "请求报文缺失";
            return a;
        }
        string desKey = ser.Decrypt(array[1], PrivateKey, nKey);
        Des des = new Des();
        a.other=des.Decrypt(array[2],desKey);
        a.check = "SendOK"; //代表加密拆解正常
        return a;
        }
        //接受会话请求(证书交换)
        else if(array[0]=="r")
        {
        if(lengthStr!=9)
        {
            //请求会话包报文缺失
            a.other = "请求会话包报文缺失";
            return a;
        }
        //判断证书
        int s=ser.check("CA",array[7],array[5],array[6]);
        if(s==1)
        {
            //判断签名
            //可在此添加版本
            if(ser.check(array[2],array[8],array[3],array[4])==1)
            {
                a.ekey = array[3];
                a.nkey = array[4];
                a.ID = array[2];
                a.other = "c;" + cer + ";" + sign;
                a.check = "RequestOK"; //请求回话成功
                return a;
            }
            else
            {
                //假签名
                a.other = "签名错误";
                return a;
            }
        }
        else
        {
            //假证书
            a.other = "证书错误";
            return a;
        }
        }
        else
        {
        a.other = "报文格式错误";
        return a;
        }
 }