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); }
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); }
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); }
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); }
public Khoa(string khoa) { //KhoaChinh = new ChuoiHexa(khoa); //if (!KhoaChinh.KiemTra()) //{ // return; //} string hex = ChuyenChuSangHe16(khoa); KhoaChinhNhiPhan = He16SangNhiPhan(hex); }
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); }
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)); }
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); }
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); } } }
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 }); }
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); }
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); }
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); } }
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); }
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); }
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); }