Đồ án Bảo mật thông tin

I .1 Giới thiệu 3

I.2 Các Hệ Mã Thông Dụng: 3

e. Phương pháp Affine 4

f. Phương pháp Vigenere 5

I.2 LẬP MÃ DES 14

I. 3 THÁM MÃ DES 16

I.3.1. Thám mã hệ DES - 3 vòng 19

II.3.2. Thám mã hệ DES 6-vòng 23

II.3. 3 Các thám mã vi sai khác 27

III. CÀI ĐẶT THÁM MÃ DES 3 VÒNG 27

III.1 Giao Diện . 27

III.2 XỬ LÝ

 

doc122 trang | Chia sẻ: huong.duong | Lượt xem: 1122 | Lượt tải: 0download
Bạn đang xem trước 20 trang tài liệu Đồ án Bảo mật thông tin, để xem tài liệu hoàn chỉnh bạn click vào nút DOWNLOAD ở trên
6E7E8 = E(R5) = E(L6) và E1*E2*E3*E4*E5*E6*E7*E8* = E(R5*) = E(L6*) có thể tính được từ các bản rõ như sau: Input: L0R0, L0*R0*, L6R6 và L6*R6*; với L0’ = 4008000016 và R0’ = 0400000016. Tính C’ = P-1(R6’ Å 0400000016) Tính E = E(L6) và E* = E(L6*) for j Ỵ {2,5,6,7,8} do tính testj( Ej, Ej*, Cj’) Ta cũng sẽ xác định 30 bit khóa trong J2, J5, J6, J7 và J8 như trong thám mã 3-vòng. Bài toán, để xâu xuất x-or giả định cho vòng 6 là chính xác chỉ với xác suất 1/16. Còn 15/16 phần còn lại ta sẽ thường nhận được những xâu vô dụng ngẫu nhiên hơn là các bit khóa. Định nghĩa 3.6: Giả sử L0 Å L0* = L0’ và R0 Å R0*= R0’. Ta nói rằng, cặp bản rõ L0R0 và L0* R0* là đúng (right) ứng với đặc trưng nếu Li Å Li* = Li’ và Ri Å Ri*= Ri’ cho mọi i, 1 £ i £ n. Cặp trái với cặp được định nghĩa gọi là cặp sai (wrong). Ta mong rằng, khoảng 1/16 số cặp của ta là đúng, còn các cặp còn lại là cặp sai ứng với đặc trưng vòng ba của ta. Chiến lược của ta là tính Ej. Ej* và Cj’như đã mô tả ở trên và sau đó xác định testj(Ej, Ej*, Cj’) với j = 2,5,6,7,8. Nếu ta bắt đầu với một cặp đúng, thì thì các bit khóa chính xác cho mỗi Jj sẽ nằm trong tập testj. Nếu cặp là sai, thì trị Cj’ sẽ không đúng, và đó là nguyên do để giả định rằng, mỗi tập testj thực chất là ngẫu nhiên. Ta có thể nhận ra cặp đúng theo phương pháp sau: Nếu ơtestjơ= 0, với bất kỳ jỴ {2,5,6,7,8}, khi đó ta tất yếu có được cặp đúng. Bây giờ cho một cặp sai, ta có thể hy vọng rằng, xác suất để ïtestjï= 0 cho một j cụ thể là xấp xỉ 1/5. Đó là lý do để giả định là, Nj(Ej’, Cj’) = ïtestjï và như đã nhận xét từ trước, xác suất để Nj(Ej’, Cj’) = 0 là xấp xỉ 1/5. Xác suất để cả năm testj đều dương là vào khoảng 0.85 » 0.33, quả vậy xác suất để ít nhất một testj bằng 0 là vào khoảng 0.67. Nên ta có khoảng 2/3 số cặp là sai, nhờ vào một nhận xét đơn giản, được gọi là phép lọc (filtering operation). Tỷ số của các cặp đúng trên các cặp còn lại sau phép lọc là vào khoảng: Ví dụ 3.4: Giả sử ta có cặp bản rõ - bản mã sau: Bản rõ Bản mã 86FA1C2B1F51D3BE C6F21C2B1B51D3BE 1E23ED7F2F553971 296DE2B687AC6340 Chú ý là, L0’ = 4008000016 và R0’ = 0400000016. Xâu nhập và xâu xuất của S-hộp cho vòng 6 được tính như sau: j Ej Ej* Cj’ 2 5 6 7 8 111100 111101 011010 101111 111110 010010 111100 000101 010110 101100 1101 0001 0010 1100 1101 Khi đó các tập testj sẽ là như sau: j testj 2 14, 15,26, 30, 32, 33, 48, 52 5 6 7, 24, 36, 41, 54, 59 7 8 34, 35, 48, 49 Ta thấy rằng, hai tập test5 và test7 là rỗng , nên cặp này là cặp sai và nó bị loại bỏ bằng phép lọc. Bây giờ giả sử ta có cặp sao cho ïtestjï> 0 với j = 2,5,6,7,8 là những tập còn lại sau phép lọc.(Bởi vì ta không biết được là cặp nào đúng, cặp nào sai.) Ta nói rằng, xâu bit J2J5J6J7J8 độ dài 30 là được đề xuất bởi cặp nếu Jj Ỵ testj với j = 2,5,6,7,8. Số các cặp được đề xuất là: Đó là bình thường với số xâu bit được đề xuất là khá lớn. (Chẳng hạn. lớn hơn 80000) Giả sử, ta lập bảng cho tất cả các xâu được đề xuất nhận được từ N cặp, mà không bị loại bởi phép lọc. Với mỗi cặp đúng, thì xâu bit đúng J2J5J6J7J8 sẽ là xâu được đề xuất. Xâu bit đúng sẽ được tính khoảng 3N/16 lần. Xâu bit sai thường xuất hiện ít hơn, bởi vì chúng xuất hiện ngẫu nhiên và có khoảng 230 khả năng. (Là một số rất lớn.) Ta nhận được một bảng cực lớn tất cả các xâu được đề xuất, nên ta sử dụng một thuật toán chỉ đòi hỏi một không gian và thời gian ít nhất. Ta có thể mã hóa bất kỳ một tập testj nào thành một véc tơ Tj có độ dài 64, với tọa độ thứ i của Tj được đặt bằng 1 (0£ i£63), nếu xâu bit độ dài 6 là biểu diễn của i ở trong tập testj; và tọa độ thứ i được đặt bằng 0 trong trường hợp ngược lại ( điều này giống như mảng các bộ đếm mà ta đã sử dụng trong thám mã DES ba vòng). Với mỗi cặp còn lại, ta xây dựng các véc tơ như trên và gọi chúng là Tji, j=2,5,6,7,8; 1 £ i£ N. Với I Í {1, ..., N} ta nói rằng I là chấp nhận được (allowable) nếu với mỗi j Ỵ {2,5,6,7,8} có ít nhất một tọa độ bằng ïIï trong véc tơ Nếu cặp thứ i là cặp đúng cho mỗi iỴI, thì tập I là chấp nhận được. Do đó ta cho rằng tập chấp nhận được có kích thước (xấp xỉ) 3N/16, là tập đề xuất và ta hy vọng là chỉ gồm các bit khóa đúng chứ không có các xâu khác. Điều này làm đơn giản hóa cho việc xây dựng tất cả các tập chấp nhận được I bằng một thuật toán đệ qui. II.3. 3 Các thám mã vi sai khác Phương pháp thám mã vi sai còn có thể áp dụng để thám các hệ DES nhiều vòng hơn. Với hệ DES 8-vòng đòi hỏi 214 bản rõ chọn và các hệ 10-, 12-, 14- và 16-vòng đòi hỏi có tương ứng 224, 231, 239 và 247 bản mã chọn. Nên nói chung là khá phức tạp. Các kỹ thuật thám mã vi sai được Biham và Shamir phát triển. Các phương pháp thám mã DES khác đã được Matsui sử dụng như là thám mã tuyến tính. HỆ MÃ DES 3 VÒNG Chương trình gồm hai phần: Phần Giao Diên (chứa trong thư mục GiaoDien): Có chức năng xử lý giao diện. Phần Xử Lý (chứa trong thư mục XuLy): có chức năng hộ trợ các hàm xử lý. III.1 Giao Diện ( Package GiaoDien). Màn hình chính (Mainform.vb) Form lập mã và giải mã DES(Des.vb) Source code một số hàm chính trong form giai mã Des Imports System.IO Public Class des Inherits System.Windows.Forms.Form khai bao bien Dim str As String Dim s(7) As DataTable Dim ip() As String 'Dim iptru() As String Dim e() As String Dim p() As String Dim pc1() As String Dim pc2() As String Dim daykhoa(15) As String Dim x As String Dim daynhap(29) As String Dim daybanma(29) As String khoi tao Sub khoitao_s0() Dim i As Integer s(0) = New DataTable For i = 0 To 15 Dim col As DataColumn = New DataColumn s(0).Columns.Add(col) Next For i = 0 To 3 Dim row As DataRow = s(0).NewRow s(0).Rows.Add(row) Next s(0).Rows(0).Item(0) = 14 s(0).Rows(0).Item(1) = 4 s(0).Rows(0).Item(2) = 13 s(0).Rows(0).Item(3) = 1 s(0).Rows(0).Item(4) = 2 s(0).Rows(0).Item(5) = 15 s(0).Rows(0).Item(6) = 11 s(0).Rows(0).Item(7) = 8 s(0).Rows(0).Item(8) = 3 s(0).Rows(0).Item(9) = 10 s(0).Rows(0).Item(10) = 6 s(0).Rows(0).Item(11) = 12 s(0).Rows(0).Item(12) = 5 s(0).Rows(0).Item(13) = 9 s(0).Rows(0).Item(14) = 0 s(0).Rows(0).Item(15) = 7 s(0).Rows(1).Item(0) = 0 s(0).Rows(1).Item(1) = 15 s(0).Rows(1).Item(2) = 7 s(0).Rows(1).Item(3) = 4 s(0).Rows(1).Item(4) = 14 s(0).Rows(1).Item(5) = 2 s(0).Rows(1).Item(6) = 13 s(0).Rows(1).Item(7) = 1 s(0).Rows(1).Item(8) = 10 s(0).Rows(1).Item(9) = 6 s(0).Rows(1).Item(10) = 12 s(0).Rows(1).Item(11) = 11 s(0).Rows(1).Item(12) = 9 s(0).Rows(1).Item(13) = 5 s(0).Rows(1).Item(14) = 3 s(0).Rows(1).Item(15) = 8 s(0).Rows(2).Item(0) = 4 s(0).Rows(2).Item(1) = 1 s(0).Rows(2).Item(2) = 14 s(0).Rows(2).Item(3) = 8 s(0).Rows(2).Item(4) = 13 s(0).Rows(2).Item(5) = 6 s(0).Rows(2).Item(6) = 2 s(0).Rows(2).Item(7) = 11 s(0).Rows(2).Item(8) = 15 s(0).Rows(2).Item(9) = 12 s(0).Rows(2).Item(10) = 9 s(0).Rows(2).Item(11) = 7 s(0).Rows(2).Item(12) = 3 s(0).Rows(2).Item(13) = 10 s(0).Rows(2).Item(14) = 5 s(0).Rows(2).Item(15) = 0 s(0).Rows(3).Item(0) = 15 s(0).Rows(3).Item(1) = 12 s(0).Rows(3).Item(2) = 8 s(0).Rows(3).Item(3) = 2 s(0).Rows(3).Item(4) = 4 s(0).Rows(3).Item(5) = 9 s(0).Rows(3).Item(6) = 1 s(0).Rows(3).Item(7) = 7 s(0).Rows(3).Item(8) = 5 s(0).Rows(3).Item(9) = 11 s(0).Rows(3).Item(10) = 3 s(0).Rows(3).Item(11) = 14 s(0).Rows(3).Item(12) = 10 s(0).Rows(3).Item(13) = 0 s(0).Rows(3).Item(14) = 6 s(0).Rows(3).Item(15) = 13 dgs0.DataSource = s(0) End Sub Ham khoi tao s1 Sub khoitao_s1() Dim i As Integer s(1) = New DataTable For i = 0 To 15 Dim col As DataColumn = New DataColumn s(1).Columns.Add(col) Next For i = 0 To 3 Dim row As DataRow = s(1).NewRow s(1).Rows.Add(row) Next s(1).Rows(0).Item(0) = 15 s(1).Rows(0).Item(1) = 1 s(1).Rows(0).Item(2) = 8 s(1).Rows(0).Item(3) = 14 s(1).Rows(0).Item(4) = 6 s(1).Rows(0).Item(5) = 11 s(1).Rows(0).Item(6) = 3 s(1).Rows(0).Item(7) = 4 s(1).Rows(0).Item(8) = 9 s(1).Rows(0).Item(9) = 7 s(1).Rows(0).Item(10) = 2 s(1).Rows(0).Item(11) = 13 s(1).Rows(0).Item(12) = 12 s(1).Rows(0).Item(13) = 0 s(1).Rows(0).Item(14) = 5 s(1).Rows(0).Item(15) = 10 s(1).Rows(1).Item(0) = 3 s(1).Rows(1).Item(1) = 13 s(1).Rows(1).Item(2) = 4 s(1).Rows(1).Item(3) = 7 s(1).Rows(1).Item(4) = 15 s(1).Rows(1).Item(5) = 2 s(1).Rows(1).Item(6) = 8 s(1).Rows(1).Item(7) = 14 s(1).Rows(1).Item(8) = 12 s(1).Rows(1).Item(9) = 0 s(1).Rows(1).Item(10) = 1 s(1).Rows(1).Item(11) = 10 s(1).Rows(1).Item(12) = 6 s(1).Rows(1).Item(13) = 9 s(1).Rows(1).Item(14) = 11 s(1).Rows(1).Item(15) = 5 s(1).Rows(2).Item(0) = 0 s(1).Rows(2).Item(1) = 14 s(1).Rows(2).Item(2) = 7 s(1).Rows(2).Item(3) = 11 s(1).Rows(2).Item(4) = 10 s(1).Rows(2).Item(5) = 4 s(1).Rows(2).Item(6) = 13 s(1).Rows(2).Item(7) = 1 s(1).Rows(2).Item(8) = 5 s(1).Rows(2).Item(9) = 8 s(1).Rows(2).Item(10) = 12 s(1).Rows(2).Item(11) = 6 s(1).Rows(2).Item(12) = 9 s(1).Rows(2).Item(13) = 3 s(1).Rows(2).Item(14) = 2 s(1).Rows(2).Item(15) = 15 s(1).Rows(3).Item(0) = 13 s(1).Rows(3).Item(1) = 8 s(1).Rows(3).Item(2) = 10 s(1).Rows(3).Item(3) = 1 s(1).Rows(3).Item(4) = 3 s(1).Rows(3).Item(5) = 15 s(1).Rows(3).Item(6) = 4 s(1).Rows(3).Item(7) = 2 s(1).Rows(3).Item(8) = 11 s(1).Rows(3).Item(9) = 6 s(1).Rows(3).Item(10) = 7 s(1).Rows(3).Item(11) = 12 s(1).Rows(3).Item(12) = 0 s(1).Rows(3).Item(13) = 5 s(1).Rows(3).Item(14) = 14 s(1).Rows(3).Item(15) = 9 dgs1.DataSource = s(1) End Sub Ham khoi tao s2 Sub khoitao_s2() Dim i As Integer s(2) = New DataTable For i = 0 To 15 Dim col As DataColumn = New DataColumn s(2).Columns.Add(col) Next For i = 0 To 3 Dim row As DataRow = s(2).NewRow s(2).Rows.Add(row) Next s(2).Rows(0).Item(0) = 10 s(2).Rows(0).Item(1) = 0 s(2).Rows(0).Item(2) = 9 s(2).Rows(0).Item(3) = 14 s(2).Rows(0).Item(4) = 6 s(2).Rows(0).Item(5) = 3 s(2).Rows(0).Item(6) = 15 s(2).Rows(0).Item(7) = 5 s(2).Rows(0).Item(8) = 1 s(2).Rows(0).Item(9) = 13 s(2).Rows(0).Item(10) = 12 s(2).Rows(0).Item(11) = 7 s(2).Rows(0).Item(12) = 11 s(2).Rows(0).Item(13) = 4 s(2).Rows(0).Item(14) = 2 s(2).Rows(0).Item(15) = 8 s(2).Rows(1).Item(0) = 13 s(2).Rows(1).Item(1) = 7 s(2).Rows(1).Item(2) = 0 s(2).Rows(1).Item(3) = 9 s(2).Rows(1).Item(4) = 3 s(2).Rows(1).Item(5) = 4 s(2).Rows(1).Item(6) = 6 s(2).Rows(1).Item(7) = 10 s(2).Rows(1).Item(8) = 2 s(2).Rows(1).Item(9) = 8 s(2).Rows(1).Item(10) = 5 s(2).Rows(1).Item(11) = 14 s(2).Rows(1).Item(12) = 12 s(2).Rows(1).Item(13) = 11 s(2).Rows(1).Item(14) = 15 s(2).Rows(1).Item(15) = 1 s(2).Rows(2).Item(0) = 13 s(2).Rows(2).Item(1) = 6 s(2).Rows(2).Item(2) = 4 s(2).Rows(2).Item(3) = 9 s(2).Rows(2).Item(4) = 8 s(2).Rows(2).Item(5) = 15 s(2).Rows(2).Item(6) = 3 s(2).Rows(2).Item(7) = 0 s(2).Rows(2).Item(8) = 11 s(2).Rows(2).Item(9) = 1 s(2).Rows(2).Item(10) = 2 s(2).Rows(2).Item(11) = 12 s(2).Rows(2).Item(12) = 5 s(2).Rows(2).Item(13) = 10 s(2).Rows(2).Item(14) = 14 s(2).Rows(2).Item(15) = 7 s(2).Rows(3).Item(0) = 1 s(2).Rows(3).Item(1) = 10 s(2).Rows(3).Item(2) = 13 s(2).Rows(3).Item(3) = 0 s(2).Rows(3).Item(4) = 6 s(2).Rows(3).Item(5) = 9 s(2).Rows(3).Item(6) = 8 s(2).Rows(3).Item(7) = 7 s(2).Rows(3).Item(8) = 4 s(2).Rows(3).Item(9) = 15 s(2).Rows(3).Item(10) = 14 s(2).Rows(3).Item(11) = 3 s(2).Rows(3).Item(12) = 11 s(2).Rows(3).Item(13) = 5 s(2).Rows(3).Item(14) = 3 s(2).Rows(3).Item(15) = 12 dgs2.DataSource = s(2) End Sub Hàm khởi tạo s3 Sub khoitao_s3() Dim i As Integer s(3) = New DataTable For i = 0 To 15 Dim col As DataColumn = New DataColumn s(3).Columns.Add(col) Next For i = 0 To 3 Dim row As DataRow = s(3).NewRow s(3).Rows.Add(row) Next s(3).Rows(0).Item(0) = 7 s(3).Rows(0).Item(1) = 13 s(3).Rows(0).Item(2) = 14 s(3).Rows(0).Item(3) = 3 s(3).Rows(0).Item(4) = 0 s(3).Rows(0).Item(5) = 6 s(3).Rows(0).Item(6) = 9 s(3).Rows(0).Item(7) = 10 s(3).Rows(0).Item(8) = 1 s(3).Rows(0).Item(9) = 2 s(3).Rows(0).Item(10) = 8 s(3).Rows(0).Item(11) = 5 s(3).Rows(0).Item(12) = 11 s(3).Rows(0).Item(13) = 12 s(3).Rows(0).Item(14) = 4 s(3).Rows(0).Item(15) = 15 s(3).Rows(1).Item(0) = 13 s(3).Rows(1).Item(1) = 8 s(3).Rows(1).Item(2) = 11 s(3).Rows(1).Item(3) = 5 s(3).Rows(1).Item(4) = 6 s(3).Rows(1).Item(5) = 15 s(3).Rows(1).Item(6) = 0 s(3).Rows(1).Item(7) = 3 s(3).Rows(1).Item(8) = 4 s(3).Rows(1).Item(9) = 7 s(3).Rows(1).Item(10) = 2 s(3).Rows(1).Item(11) = 12 s(3).Rows(1).Item(12) = 1 s(3).Rows(1).Item(13) = 10 s(3).Rows(1).Item(14) = 14 s(3).Rows(1).Item(15) = 9 s(3).Rows(2).Item(0) = 10 s(3).Rows(2).Item(1) = 6 s(3).Rows(2).Item(2) = 9 s(3).Rows(2).Item(3) = 0 s(3).Rows(2).Item(4) = 12 s(3).Rows(2).Item(5) = 11 s(3).Rows(2).Item(6) = 7 s(3).Rows(2).Item(7) = 13 s(3).Rows(2).Item(8) = 15 s(3).Rows(2).Item(9) = 1 s(3).Rows(2).Item(10) = 3 s(3).Rows(2).Item(11) = 14 s(3).Rows(2).Item(12) = 5 s(3).Rows(2).Item(13) = 2 s(3).Rows(2).Item(14) = 8 s(3).Rows(2).Item(15) = 4 s(3).Rows(3).Item(0) = 3 s(3).Rows(3).Item(1) = 15 s(3).Rows(3).Item(2) = 0 s(3).Rows(3).Item(3) = 6 s(3).Rows(3).Item(4) = 10 s(3).Rows(3).Item(5) = 1 s(3).Rows(3).Item(6) = 13 s(3).Rows(3).Item(7) = 8 s(3).Rows(3).Item(8) = 9 s(3).Rows(3).Item(9) = 4 s(3).Rows(3).Item(10) = 5 s(3).Rows(3).Item(11) = 11 s(3).Rows(3).Item(12) = 12 s(3).Rows(3).Item(13) = 7 s(3).Rows(3).Item(14) = 2 s(3).Rows(3).Item(15) = 14 dgs3.DataSource = s(3) End Sub Hàm khởi tạo s4 Sub khoitao_s4() Dim i As Integer s(4) = New DataTable For i = 0 To 15 Dim col As DataColumn = New DataColumn s(4).Columns.Add(col) Next For i = 0 To 3 Dim row As DataRow = s(4).NewRow s(4).Rows.Add(row) Next s(4).Rows(0).Item(0) = 2 s(4).Rows(0).Item(1) = 12 s(4).Rows(0).Item(2) = 4 s(4).Rows(0).Item(3) = 1 s(4).Rows(0).Item(4) = 7 s(4).Rows(0).Item(5) = 10 s(4).Rows(0).Item(6) = 11 s(4).Rows(0).Item(7) = 6 s(4).Rows(0).Item(8) = 8 s(4).Rows(0).Item(9) = 5 s(4).Rows(0).Item(10) = 3 s(4).Rows(0).Item(11) = 15 s(4).Rows(0).Item(12) = 13 s(4).Rows(0).Item(13) = 0 s(4).Rows(0).Item(14) = 14 s(4).Rows(0).Item(15) = 9 s(4).Rows(1).Item(0) = 14 s(4).Rows(1).Item(1) = 11 s(4).Rows(1).Item(2) = 2 s(4).Rows(1).Item(3) = 12 s(4).Rows(1).Item(4) = 4 s(4).Rows(1).Item(5) = 7 s(4).Rows(1).Item(6) = 13 s(4).Rows(1).Item(7) = 1 s(4).Rows(1).Item(8) = 5 s(4).Rows(1).Item(9) = 0 s(4).Rows(1).Item(10) = 15 s(4).Rows(1).Item(11) = 10 s(4).Rows(1).Item(12) = 3 s(4).Rows(1).Item(13) = 9 s(4).Rows(1).Item(14) = 8 s(4).Rows(1).Item(15) = 6 s(4).Rows(2).Item(0) = 4 s(4).Rows(2).Item(1) = 2 s(4).Rows(2).Item(2) = 1 s(4).Rows(2).Item(3) = 11 s(4).Rows(2).Item(4) = 10 s(4).Rows(2).Item(5) = 13 s(4).Rows(2).Item(6) = 7 s(4).Rows(2).Item(7) = 8 s(4).Rows(2).Item(8) = 15 s(4).Rows(2).Item(9) = 9 s(4).Rows(2).Item(10) = 12 s(4).Rows(2).Item(11) = 5 s(4).Rows(2).Item(12) = 6 s(4).Rows(2).Item(13) = 3 s(4).Rows(2).Item(14) = 0 s(4).Rows(2).Item(15) = 14 s(4).Rows(3).Item(0) = 11 s(4).Rows(3).Item(1) = 8 s(4).Rows(3).Item(2) = 12 s(4).Rows(3).Item(3) = 7 s(4).Rows(3).Item(4) = 0 s(4).Rows(3).Item(5) = 14 s(4).Rows(3).Item(6) = 2 s(4).Rows(3).Item(7) = 13 s(4).Rows(3).Item(8) = 6 s(4).Rows(3).Item(9) = 15 s(4).Rows(3).Item(10) = 0 s(4).Rows(3).Item(11) = 9 s(4).Rows(3).Item(12) = 10 s(4).Rows(3).Item(13) = 4 s(4).Rows(3).Item(14) = 5 s(4).Rows(3).Item(15) = 3 dgs4.DataSource = s(4) End Sub Hàm khởi tạo S5 Sub khoitao_s5() Dim i As Integer s(5) = New DataTable For i = 0 To 15 Dim col As DataColumn = New DataColumn s(5).Columns.Add(col) Next For i = 0 To 3 Dim row As DataRow = s(5).NewRow s(5).Rows.Add(row) Next s(5).Rows(0).Item(0) = 12 s(5).Rows(0).Item(1) = 1 s(5).Rows(0).Item(2) = 10 s(5).Rows(0).Item(3) = 15 s(5).Rows(0).Item(4) = 9 s(5).Rows(0).Item(5) = 2 s(5).Rows(0).Item(6) = 6 s(5).Rows(0).Item(7) = 8 s(5).Rows(0).Item(8) = 0 s(5).Rows(0).Item(9) = 13 s(5).Rows(0).Item(10) = 3 s(5).Rows(0).Item(11) = 4 s(5).Rows(0).Item(12) = 14 s(5).Rows(0).Item(13) = 7 s(5).Rows(0).Item(14) = 5 s(5).Rows(0).Item(15) = 11 s(5).Rows(1).Item(0) = 10 s(5).Rows(1).Item(1) = 15 s(5).Rows(1).Item(2) = 4 s(5).Rows(1).Item(3) = 2 s(5).Rows(1).Item(4) = 7 s(5).Rows(1).Item(5) = 12 s(5).Rows(1).Item(6) = 9 s(5).Rows(1).Item(7) = 5 s(5).Rows(1).Item(8) = 6 s(5).Rows(1).Item(9) = 1 s(5).Rows(1).Item(10) = 13 s(5).Rows(1).Item(11) = 14 s(5).Rows(1).Item(12) = 0 s(5).Rows(1).Item(13) = 11 s(5).Rows(1).Item(14) = 3 s(5).Rows(1).Item(15) = 8 s(5).Rows(2).Item(0) = 9 s(5).Rows(2).Item(1) = 14 s(5).Rows(2).Item(2) = 15 s(5).Rows(2).Item(3) = 5 s(5).Rows(2).Item(4) = 2 s(5).Rows(2).Item(5) = 8 s(5).Rows(2).Item(6) = 12 s(5).Rows(2).Item(7) = 3 s(5).Rows(2).Item(8) = 7 s(5).Rows(2).Item(9) = 0 s(5).Rows(2).Item(10) = 4 s(5).Rows(2).Item(11) = 10 s(5).Rows(2).Item(12) = 1 s(5).Rows(2).Item(13) = 13 s(5).Rows(2).Item(14) = 11 s(5).Rows(2).Item(15) = 6 s(5).Rows(3).Item(0) = 4 s(5).Rows(3).Item(1) = 3 s(5).Rows(3).Item(2) = 2 s(5).Rows(3).Item(3) = 12 s(5).Rows(3).Item(4) = 9 s(5).Rows(3).Item(5) = 5 s(5).Rows(3).Item(6) = 15 s(5).Rows(3).Item(7) = 10 s(5).Rows(3).Item(8) = 11 s(5).Rows(3).Item(9) = 14 s(5).Rows(3).Item(10) = 1 s(5).Rows(3).Item(11) = 7 s(5).Rows(3).Item(12) = 6 s(5).Rows(3).Item(13) = 0 s(5).Rows(3).Item(14) = 8 s(5).Rows(3).Item(15) = 13 dgs5.DataSource = s(5) End Sub Hàm khởi tạo S6 Sub khoitao_s6() Dim i As Integer s(6) = New DataTable For i = 0 To 15 Dim col As DataColumn = New DataColumn s(6).Columns.Add(col) Next For i = 0 To 3 Dim row As DataRow = s(6).NewRow s(6).Rows.Add(row) Next s(6).Rows(0).Item(0) = 4 s(6).Rows(0).Item(1) = 11 s(6).Rows(0).Item(2) = 2 s(6).Rows(0).Item(3) = 14 s(6).Rows(0).Item(4) = 15 s(6).Rows(0).Item(5) = 0 s(6).Rows(0).Item(6) = 8 s(6).Rows(0).Item(7) = 13 s(6).Rows(0).Item(8) = 3 s(6).Rows(0).Item(9) = 12 s(6).Rows(0).Item(10) = 9 s(6).Rows(0).Item(11) = 7 s(6).Rows(0).Item(12) = 5 s(6).Rows(0).Item(13) = 10 s(6).Rows(0).Item(14) = 6 s(6).Rows(0).Item(15) = 1 s(6).Rows(1).Item(0) = 13 s(6).Rows(1).Item(1) = 0 s(6).Rows(1).Item(2) = 11 s(6).Rows(1).Item(3) = 7 s(6).Rows(1).Item(4) = 4 s(6).Rows(1).Item(5) = 9 s(6).Rows(1).Item(6) = 1 s(6).Rows(1).Item(7) = 10 s(6).Rows(1).Item(8) = 14 s(6).Rows(1).Item(9) = 3 s(6).Rows(1).Item(10) = 5 s(6).Rows(1).Item(11) = 12 s(6).Rows(1).Item(12) = 2 s(6).Rows(1).Item(13) = 15 s(6).Rows(1).Item(14) = 8 s(6).Rows(1).Item(15) = 6 s(6).Rows(2).Item(0) = 1 s(6).Rows(2).Item(1) = 4 s(6).Rows(2).Item(2) = 11 s(6).Rows(2).Item(3) = 13 s(6).Rows(2).Item(4) = 12 s(6).Rows(2).Item(5) = 3 s(6).Rows(2).Item(6) = 7 s(6).Rows(2).Item(7) = 14 s(6).Rows(2).Item(8) = 10 s(6).Rows(2).Item(9) = 15 s(6).Rows(2).Item(10) = 6 s(6).Rows(2).Item(11) = 8 s(6).Rows(2).Item(12) = 0 s(6).Rows(2).Item(13) = 5 s(6).Rows(2).Item(14) = 9 s(6).Rows(2).Item(15) = 2 s(6).Rows(3).Item(0) = 6 s(6).Rows(3).Item(1) = 11 s(6).Rows(3).Item(2) = 13 s(6).Rows(3).Item(3) = 8 s(6).Rows(3).Item(4) = 1 s(6).Rows(3).Item(5) = 4 s(6).Rows(3).Item(6) = 10 s(6).Rows(3).Item(7) = 7 s(6).Rows(3).Item(8) = 9 s(6).Rows(3).Item(9) = 5 s(6).Rows(3).Item(10) = 0 s(6).Rows(3).Item(11) = 15 s(6).Rows(3).Item(12) = 14 s(6).Rows(3).Item(13) = 2 s(6).Rows(3).Item(14) = 3 s(6).Rows(3).Item(15) = 12 dgs6.DataSource = s(6) End Sub Hàm khởi tạo S7 Sub khoitao_s7() Dim i As Integer s(7) = New DataTable For i = 0 To 15 Dim col As DataColumn = New DataColumn s(7).Columns.Add(col) Next For i = 0 To 3 Dim row As DataRow = s(7).NewRow s(7).Rows.Add(row) Next s(7).Rows(0).Item(0) = 13 s(7).Rows(0).Item(1) = 2 s(7).Rows(0).Item(2) = 8 s(7).Rows(0).Item(3) = 4 s(7).Rows(0).Item(4) = 6 s(7).Rows(0).Item(5) = 15 s(7).Rows(0).Item(6) = 11 s(7).Rows(0).Item(7) = 1 s(7).Rows(0).Item(8) = 10 s(7).Rows(0).Item(9) = 9 s(7).Rows(0).Item(10) = 3 s(7).Rows(0).Item(11) = 14 s(7).Rows(0).Item(12) = 5 s(7).Rows(0).Item(13) = 0 s(7).Rows(0).Item(14) = 12 s(7).Rows(0).Item(15) = 7 s(7).Rows(1).Item(0) = 1 s(7).Rows(1).Item(1) = 15 s(7).Rows(1).Item(2) = 13 s(7).Rows(1).Item(3) = 8 s(7).Rows(1).Item(4) = 10 s(7).Rows(1).Item(5) = 3 s(7).Rows(1).Item(6) = 7 s(7).Rows(1).Item(7) = 4 s(7).Rows(1).Item(8) = 12 s(7).Rows(1).Item(9) = 5 s(7).Rows(1).Item(10) = 6 s(7).Rows(1).Item(11) = 11 s(7).Rows(1).Item(12) = 0 s(7).Rows(1).Item(13) = 14 s(7).Rows(1).Item(14) = 9 s(7).Rows(1).Item(15) = 2 s(7).Rows(2).Item(0) = 7 s(7).Rows(2).Item(1) = 11 s(7).Rows(2).Item(2) = 4 s(7).Rows(2).Item(3) = 1 s(7).Rows(2).Item(4) = 9 s(7).Rows(2).Item(5) = 12 s(7).Rows(2).Item(6) = 14 s(7).Rows(2).Item(7) = 2 s(7).Rows(2).Item(8) = 0 s(7).Rows(2).Item(9) = 6 s(7).Rows(2).Item(10) = 10 s(7).Rows(2).Item(11) = 13 s(7).Rows(2).Item(12) = 15 s(7).Rows(2).Item(13) = 3 s(7).Rows(2).Item(14) = 5 s(7).Rows(2).Item(15) = 8 s(7).Rows(3).Item(0) = 2 s(7).Rows(3).Item(1) = 1 s(7).Rows(3).Item(2) = 14 s(7).Rows(3).Item(3) = 7 s(7).Rows(3).Item(4) = 4 s(7).Rows(3).Item(5) = 10 s(7).Rows(3).Item(6) = 8 s(7).Rows(3).Item(7) = 13 s(7).Rows(3).Item(8) = 15 s(7).Rows(3).Item(9) = 12 s(7).Rows(3).Item(10) = 9 s(7).Rows(3).Item(11) = 0 s(7).Rows(3).Item(12) = 3 s(7).Rows(3).Item(13) = 5 s(7).Rows(3).Item(14) = 6 s(7).Rows(3).Item(15) = 11 dgs7.DataSource = s(7) End Sub Khởi tạo giá trị biến Sub khoitao() ip = txtip.Text.Split(" ", ";", ":", ".") 'iptru = txtiptru.Text.Split(" ", " ", ";", ":", ".") e = txte.Text.Split(" ", ";", ":", ".") p = txtp.Text.Split(" ", ";", ":", ".") pc1 = txtpc1.Text.Split(" ", ";", ":", ".") pc2 = txtpc2.Text.Split(" ", ";", ":", ".") khoitao_s0() khoitao_s1() khoitao_s2() khoitao_s3() khoitao_s4() khoitao_s5() khoitao_s6() khoitao_s7() End Sub Cắt bit cuối Function catbitcuoi(ByVal k As String) As String 'dua vao 64 bit tra ra 56 bit Dim i As Integer = 0 Dim j As Integer Dim tam As String While i < 63 For j = i To i + 6 tam += k.Substring(j, 1) Next i = i + 8 End While Return tam End Function Function hvpc1(ByVal k As String) As String Dim tam(63) As Char Dim i As Integer For i = 0 To 63 tam(i) = k.Substring(i, 1) Next tam = catbitcuoi(tam) For i = 0 To 55 tam(i) = k.Substring(Integer.Parse(pc1(i) - 1), 1) Next Return tam End Function Hoá vị pc2 Function hvpc2(ByVal str As String) As String Dim tam(47) As Char Dim i As Integer For i = 0 To 47 tam(i) = str.Substring(Integer.Parse(pc2(i) - 1), 1) Next Return tam End Function Function ls(ByVal s As String, ByVal n As Integer) As String Return s.Substring(n, s.Length - n) + s.Substring(0, n) End Function Hàm tạo dãy khoá Sub taodaykhoa() Dim khoa as String = "0001001100110100010101110111100110011011101111001101111111110001" Dim khoa As String = txtkhoak.Text Dim j As Integer If khoa.Length > 8 Then khoa = txtkhoak.Text.Remove(8, khoa.Length - 8) txtkhoak.Text = khoa End If Dim tam As String For j = 0 To khoa.Length - 1 tam += bi_acsii(Asc(khoa.Substring(j, 1))) Next khoa = tam Dim khoa1 As String = hvpc1(khoa) Dim d(16) As String Dim c(16) As String c(0) = khoa1.Substring(0, 28) c(0) = ls(c(0), 1) d(0) = khoa1.Substring(28, 28) d(0) = ls(d(0), 1) daykhoa(0) = hvpc2(c(0) + d(0)) txtdaykhoa.Text += daykhoa(0) + Chr(9) Dim i As Integer For i = 1 To 15 If i = 2 - 1 Or i = 9 - 1 Or i = 16 - 1 Then c(i) = ls(c(i - 1), 1) d(i) = ls(d(i - 1), 1) Else c(i) = ls(c(i - 1), 2) d(i) = ls(d(i - 1), 2) End If daykhoa(i) = hvpc2(c(i) + d(i)) txtdaykhoa.Text += daykhoa(i) + Chr(9) Next i End Sub Một số hàm xử lý chuỗi nhập Nhập nhị phân

Các file đính kèm theo tài liệu này:

  • docDA0627.doc
Tài liệu liên quan