コード例 #1
0
ファイル: Khoa.cs プロジェクト: mandatvo1997/Encryption
        public ChuoiNhiPhan He16SangNhiPhan(string hexstring)
        {
            string binarystring = "";

            for (int i = 0; i < hexstring.Length; i++)
            {
                int hex = Convert.ToInt32(hexstring[i].ToString(), 16);

                int factor = 8;

                for (int j = 0; j < 4; j++)
                {
                    if (hex >= factor)
                    {
                        hex          -= factor;
                        binarystring += "1";
                    }
                    else
                    {
                        binarystring += "0";
                    }
                    factor /= 2;
                }
            }


            ChuoiNhiPhan chuoiNhiPhan = new ChuoiNhiPhan(binarystring.Length);

            for (int i = 0; i < binarystring.Length; i++)
            {
                chuoiNhiPhan.MangNhiPhan[i] = int.Parse(binarystring[i].ToString());
            }
            return(chuoiNhiPhan);
        }
コード例 #2
0
ファイル: DES64Bit.cs プロジェクト: mandatvo1997/Encryption
        public ChuoiNhiPhan ThucHienDES(ChuoiNhiPhan ChuoiVaoDai, int MaHoaHayGiaiMa)// 1 ma hoa, -1 giai ma
        {
            KhoaDES.SinhKhoaCon(MaHoaHayGiaiMa);


            ChuoiNhiPhan[] DSChuoiVao = ChuoiVaoDai.Chia(ChuoiVaoDai.DoDai / 64);
            ChuoiNhiPhan   ChuoiVao, ChuoiKQ;

            ChuoiKQ = new ChuoiNhiPhan(0);
            for (int k = 0; k < DSChuoiVao.Length; k++)
            {
                ChuoiVao = DSChuoiVao[k];
                ChuoiNhiPhan[] ChuoiSauIP;
                ChuoiNhiPhan   ChuoiSauIP_1;
                ChuoiNhiPhan   L, R, F, TG;
                ChuoiSauIP = CacThongSo.TinhIP(ChuoiVao, MaHoaHayGiaiMa);
                L          = ChuoiSauIP[0];
                R          = ChuoiSauIP[1];

                for (int i = 0; i < 16; i++)
                {
                    F  = HamF(R, KhoaDES.DayKhoaPhu[i]);
                    L  = L.XOR(F);
                    TG = L;
                    L  = R;
                    R  = TG;
                }
                ChuoiSauIP_1 = CacThongSo.TinhIP_1(R, L, MaHoaHayGiaiMa);
                ChuoiKQ      = ChuoiKQ.Cong(ChuoiSauIP_1);
            }
            return(ChuoiKQ);
        }
コード例 #3
0
ファイル: DES64Bit.cs プロジェクト: mandatvo1997/Encryption
        private ChuoiNhiPhan HamF(ChuoiNhiPhan chuoiVao, ChuoiNhiPhan KhoaCon)
        {
            ChuoiNhiPhan KQ = CacThongSo.TinhE(chuoiVao);

            KQ = KQ.XOR(KhoaCon);
            KQ = CacThongSo.TinhSBox(KQ);
            KQ = CacThongSo.TinhP(KQ);
            return(KQ);
        }
コード例 #4
0
        public static int ChuyenNhiPhanSangSo(ChuoiNhiPhan ChuoiVao)
        {
            int KQ = 0;

            for (int i = ChuoiVao.DoDai - 1; i >= 0; i--)
            {
                KQ += ChuoiVao.MangNhiPhan[i] * (int)Math.Pow(2, ChuoiVao.DoDai - i - 1);
            }
            return(KQ);
        }
コード例 #5
0
ファイル: Khoa.cs プロジェクト: mandatvo1997/Encryption
        public Khoa(string khoa)
        {
            //KhoaChinh = new ChuoiHexa(khoa);
            //if (!KhoaChinh.KiemTra())
            //{
            //    return;
            //}
            string hex = ChuyenChuSangHe16(khoa);

            KhoaChinhNhiPhan = He16SangNhiPhan(hex);
        }
コード例 #6
0
        public static string ChuyenNhiPhanSangChu(ChuoiNhiPhan ChuoiVao)
        {
            int soChu = ChuoiVao.DoDai / 8;

            ChuoiNhiPhan[] MangChuoi = ChuoiVao.Chia(soChu);
            string         KQ        = "";

            foreach (var ch in MangChuoi)
            {
                KQ += (char)ChuoiNhiPhan.ChuyenNhiPhanSangSo(ch);
            }
            return(KQ);
        }
コード例 #7
0
ファイル: DES64Bit.cs プロジェクト: mandatvo1997/Encryption
        public string ThucHienDES(string ChuoiVao, int MaHoaHayGiaiMa)// 1 ma hoa, -1 giai ma
        {
            ChuoiNhiPhan chuoiNhiPhan = ChuoiNhiPhan.ChuyenChuSangNhiPhan(ChuoiVao);

            //ChuoiHexa chuoiHexa = new ChuoiHexa(ChuoiVao);
            //chuoiNhiPhan = chuoiHexa.chuoiNhiPhan;

            ChuoiNhiPhan KQ = ThucHienDES(ChinhDoDaiChuoiVao(chuoiNhiPhan), MaHoaHayGiaiMa);

            return(KQ.Text);

            return(ChuoiNhiPhan.ChuyenNhiPhanSangChu(KQ));
        }
コード例 #8
0
        public ChuoiNhiPhan Cong(ChuoiNhiPhan chuoi2)
        {
            ChuoiNhiPhan ChuoiKQ = new ChuoiNhiPhan(chuoi2.DoDai + this.DoDai);

            for (int i = 0; i < DoDai; i++)
            {
                ChuoiKQ.MangNhiPhan[i] = MangNhiPhan[i];
            }
            for (int i = 0; i < chuoi2.DoDai; i++)
            {
                ChuoiKQ.MangNhiPhan[DoDai + i] = chuoi2.MangNhiPhan[i];
            }
            return(ChuoiKQ);
        }
コード例 #9
0
        public ChuoiHexa(string chuoi)
        {
            this.Chuoi = chuoi.ToUpper();
            ChuoiNhiPhan chNP;

            if (KiemTra())
            {
                chuoiNhiPhan = new ChuoiNhiPhan(0);
                foreach (var ch in Chuoi)
                {
                    chNP         = ChuoiNhiPhan.ChuyenSoSangNhiPhan(ChuoiHexa.ChuyenHexaSangHe10(ch), 4);
                    chuoiNhiPhan = chuoiNhiPhan.Cong(chNP);
                }
            }
        }
コード例 #10
0
        public ChuoiNhiPhan[] ChiaDoi( )
        {
            ChuoiNhiPhan ChuoiTrai = new ChuoiNhiPhan(this.DoDai / 2);
            ChuoiNhiPhan ChuoiPhai = new ChuoiNhiPhan(DoDai - ChuoiTrai.DoDai);

            for (int i = 0; i < ChuoiTrai.DoDai; i++)
            {
                ChuoiTrai.MangNhiPhan[i] = MangNhiPhan[i];
            }
            for (int i = 0; i < ChuoiPhai.DoDai; i++)
            {
                ChuoiPhai.MangNhiPhan[i] = MangNhiPhan[i + ChuoiTrai.DoDai];
            }
            return(new ChuoiNhiPhan[] { ChuoiTrai, ChuoiPhai });
        }
コード例 #11
0
        public ChuoiNhiPhan DichTraiBit(int SoBitDich)
        {
            ChuoiNhiPhan KQ  = new ChuoiNhiPhan(MangNhiPhan);
            int          tam = 0;

            for (int i = 0; i < SoBitDich; i++)
            {
                tam = MangNhiPhan[0];
                for (int j = 0; j < MangNhiPhan.Length - 1; j++)
                {
                    KQ.MangNhiPhan[j] = MangNhiPhan[j + 1];
                }
                KQ.MangNhiPhan[MangNhiPhan.Length - 1] = tam;
            }
            return(KQ);
        }
コード例 #12
0
        public static ChuoiNhiPhan ChuyenSoSangNhiPhan(int SoInput, int doDai)
        {
            //int doDai = (int)Math.Log(SoInput, 2);
            //if (SoInput == 0)
            //    doDai = 0;
            ChuoiNhiPhan ChuoiKQ = new ChuoiNhiPhan(doDai);
            int          i       = doDai - 1;

            while (SoInput > 0)
            {
                ChuoiKQ.MangNhiPhan[i] = SoInput % 2;
                SoInput = SoInput / 2;
                i--;
            }
            return(ChuoiKQ);
        }
コード例 #13
0
ファイル: DES64Bit.cs プロジェクト: mandatvo1997/Encryption
 public ChuoiNhiPhan ChinhDoDaiChuoiVao(ChuoiNhiPhan vao)
 {
     if (vao.DoDai % 64 != 0)
     {
         int   mode  = vao.DoDai % 64;
         int   thieu = 64 - mode;
         int[] mang  = new int[vao.DoDai + thieu];
         for (int i = 0; i < vao.MangNhiPhan.Length; i++)
         {
             mang[i] = vao.MangNhiPhan[i];
         }
         return(new ChuoiNhiPhan(mang));
     }
     else
     {
         return(vao);
     }
 }
コード例 #14
0
        public ChuoiNhiPhan[] Chia(int SoLuong)
        {
            ChuoiNhiPhan[] KQ = new ChuoiNhiPhan[SoLuong];
            ChuoiNhiPhan   chuoi;
            int            SoBit = DoDai / SoLuong;

            int[] NhiPhan = new int[SoBit];
            for (int i = 0; i < SoLuong; i++)
            {
                NhiPhan = new int[SoBit];
                for (int j = i * SoBit; j < i * SoBit + SoBit; j++)
                {
                    NhiPhan[j - i * SoBit] = MangNhiPhan[j];
                }
                chuoi = new ChuoiNhiPhan(NhiPhan);
                KQ[i] = chuoi;
            }
            return(KQ);
        }
コード例 #15
0
        public static ChuoiNhiPhan ChuyenChuSangNhiPhan(string text)
        {
            StringBuilder binarystring = new StringBuilder(text.Length * 8);

            foreach (char word in text)
            {
                int binary = (int)word;
                int factor = 128;

                for (int i = 0; i < 8; i++)
                {
                    if (binary >= factor)
                    {
                        binary -= factor;
                        binarystring.Append("1");
                    }
                    else
                    {
                        binarystring.Append("0");
                    }
                    factor /= 2;
                }
            }

            binarystring.ToString();
            //foreach (var ch in chuoiVao)
            //{
            //    KQ = KQ.Cong(new ChuoiNhiPhan(ch));
            //}
            int[] MangNhiPhan = new int[binarystring.Length];

            for (int i = 0; i < binarystring.Length; i++)
            {
                MangNhiPhan[i] = int.Parse(binarystring[i].ToString());
            }
            ChuoiNhiPhan KQ = new ChuoiNhiPhan(MangNhiPhan);

            return(KQ);
        }
コード例 #16
0
        public ChuoiNhiPhan XOR(ChuoiNhiPhan Chuoi2)
        {
            if (DoDai != Chuoi2.DoDai)
            {
                return(null);
            }
            ChuoiNhiPhan ChuoiKQ = new ChuoiNhiPhan(DoDai);
            int          x = 0, y = 0;

            for (int i = 0; i < ChuoiKQ.DoDai; i++)
            {
                x = MangNhiPhan[i];
                y = Chuoi2.MangNhiPhan[i];
                if (x != y)
                {
                    ChuoiKQ.MangNhiPhan[i] = 1;
                }
                else
                {
                    ChuoiKQ.MangNhiPhan[i] = 0;
                }
            }
            return(ChuoiKQ);
        }