public static string MaHoaTinyDES(char charcanmahoa, string khoanhapvao) { char kitucanmahoa = charcanmahoa; int sodecima = Convert.ToInt32(charcanmahoa); string chuoinhiphan = Convert.ToString(sodecima, 2); chuoinhiphan = ChuyenThanhDecima8KiTu(chuoinhiphan); string khoa = khoanhapvao; string KL0 = khoa.Substring(0, 4); string KR0 = khoa.Substring(4); string L0 = chuoinhiphan.Substring(0, 4); string R0 = chuoinhiphan.Substring(4); string C = ""; //Vòng 1 for (int i = 1; i <= 3; i++) { string L1 = R0; string expandR0 = GiaiThuatTinyDES.Expand(R0); string KL1; string KR1; if (i == 2) { KL1 = GiaiThuatTinyDES.DichVong(KL0, 2); KR1 = GiaiThuatTinyDES.DichVong(KR0, 2); } else { KL1 = GiaiThuatTinyDES.DichVong(KL0, 1); KR1 = GiaiThuatTinyDES.DichVong(KR0, 1); } string K1 = GiaiThuatTinyDES.Compress(KL1, KR1); string expandR0xorK1 = GiaiThuatTinyDES.Xor2Chuoi(expandR0, K1); string sbox = GiaiThuatTinyDES.Sbox(expandR0xorK1); string F1 = GiaiThuatTinyDES.Pbox(sbox); string R1 = GiaiThuatTinyDES.Xor2Chuoi(L0, F1); L0 = L1; R0 = R1; KL0 = KL1; KR0 = KR1; C = L1 + R1; } int soketqua = Convert.ToInt32(C, 2); char kituketqua = Convert.ToChar(soketqua); return(kituketqua.ToString()); }
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()); }
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); }
public static string MaHoaTinyDESTest(string chuoinhiphan, string khoanhapvao) { string khoa = khoanhapvao; string KL0 = khoa.Substring(0, 4); string KR0 = khoa.Substring(4); string L0 = chuoinhiphan.Substring(0, 4); string R0 = chuoinhiphan.Substring(4); string C = ""; for (int i = 1; i <= 3; i++) { string L1 = R0; string expandR0 = GiaiThuatTinyDES.Expand(R0); string KL1; string KR1; if (i == 2) { KL1 = GiaiThuatTinyDES.DichVong(KL0, 2); KR1 = GiaiThuatTinyDES.DichVong(KR0, 2); } else { KL1 = GiaiThuatTinyDES.DichVong(KL0, 1); KR1 = GiaiThuatTinyDES.DichVong(KR0, 1); } string K1 = GiaiThuatTinyDES.Compress(KL1, KR1); string expandR0xorK1 = GiaiThuatTinyDES.Xor2Chuoi(expandR0, K1); string sbox = GiaiThuatTinyDES.Sbox(expandR0xorK1); string F1 = GiaiThuatTinyDES.Pbox(sbox); string R1 = GiaiThuatTinyDES.Xor2Chuoi(L0, F1); L0 = L1; R0 = R1; KL0 = KL1; KR0 = KR1; C = L1 + R1; } return(C); }