Ejemplo n.º 1
0
        public static string GiaiMaTinyDES(char charcangiaima, string khoanhapvao)
        {
            char   kitucangiama = charcangiaima;
            int    sodecima     = Convert.ToInt32(kitucangiama);
            string chuoinhiphan = Convert.ToString(sodecima, 2);

            chuoinhiphan = ChuyenThanhDecima8KiTu(chuoinhiphan);
            string L3   = chuoinhiphan.Substring(0, 4);
            string R3   = chuoinhiphan.Substring(4);
            string khoa = khoanhapvao;
            string KL3  = khoa.Substring(0, 4);
            string KR3  = khoa.Substring(4);
            string P    = "";

            //Giải mã
            for (int i = 1; i <= 3; i++)
            {
                string R2 = L3;
                string KL2, KR2;
                string expandR2 = GiaiThuatTinyDES.Expand(R2);
                if (i == 2)
                {
                    KL2 = GiaiThuatTinyDES.DichVongTruoc(KL3, 2);
                    KR2 = GiaiThuatTinyDES.DichVongTruoc(KR3, 2);
                }
                else
                {
                    KL2 = GiaiThuatTinyDES.DichVongTruoc(KL3, 1);
                    KR2 = GiaiThuatTinyDES.DichVongTruoc(KR3, 1);
                }
                string K3            = GiaiThuatTinyDES.Compress(KL3, KR3);
                string xorexpandR2K3 = GiaiThuatTinyDES.Xor2Chuoi(expandR2, K3);
                string sbox          = GiaiThuatTinyDES.Sbox(xorexpandR2K3);
                string F3            = GiaiThuatTinyDES.Pbox(sbox);
                string L2            = GiaiThuatTinyDES.Xor2Chuoi(R3, F3);
                P   = L2 + R2;
                L3  = L2;
                R3  = R2;
                KL3 = KL2;
                KR3 = KR2;
            }
            int  soketquagiaima = Convert.ToInt32(P, 2);
            char kituketqua     = Convert.ToChar(soketquagiaima);

            return(kituketqua.ToString());
        }
Ejemplo n.º 2
0
        public static string GiaiMaTinyDesTest(string chuoinhiphan, string khoanhapvao)
        {
            string khoa = khoanhapvao;
            string L3   = chuoinhiphan.Substring(0, 4);
            string R3   = chuoinhiphan.Substring(4);
            string KL3  = khoa.Substring(0, 4);
            string KR3  = khoa.Substring(4);
            string P    = "";

            //Giải mã
            for (int i = 1; i <= 3; i++)
            {
                string R2 = L3;
                string KL2, KR2;
                string expandR2 = GiaiThuatTinyDES.Expand(R2);
                if (i == 2)
                {
                    KL2 = GiaiThuatTinyDES.DichVongTruoc(KL3, 2);
                    KR2 = GiaiThuatTinyDES.DichVongTruoc(KR3, 2);
                }
                else
                {
                    KL2 = GiaiThuatTinyDES.DichVongTruoc(KL3, 1);
                    KR2 = GiaiThuatTinyDES.DichVongTruoc(KR3, 1);
                }
                string K3            = GiaiThuatTinyDES.Compress(KL3, KR3);
                string xorexpandR2K3 = GiaiThuatTinyDES.Xor2Chuoi(expandR2, K3);
                string sbox          = GiaiThuatTinyDES.Sbox(xorexpandR2K3);
                string F3            = GiaiThuatTinyDES.Pbox(sbox);
                string L2            = GiaiThuatTinyDES.Xor2Chuoi(R3, F3);
                P   = L2 + R2;
                L3  = L2;
                R3  = R2;
                KL3 = KL2;
                KR3 = KR2;
            }
            return(P);
        }