Đề tài Hệ mã DES

MỤC LỤC

I .1 Giới thiệu 2

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

e. Phương pháp Affine 2

f. Phương pháp Vigenere 2

I.2 LẬP MÃ DES 2

I. 3 THÁM MÃ DES 2

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

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

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

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

III.1 Giao Diện . 2

III.2 XỬ LÝ

 

 

doc110 trang | Chia sẻ: maiphuongdc | Lượt xem: 1626 | Lượt tải: 1download
Bạn đang xem trước 20 trang tài liệu Đề tài Hệ mã DES, để xem tài liệu hoàn chỉnh bạn click vào nút DOWNLOAD ở trên
õ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 tố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 tố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 Hố 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 khố 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 Sub binarynhap() x = txtchuoinhap.Text Dim y As String Dim i As Integer Dim j As Integer Dim sokitudu As Integer = x.Length Mod 8 If sokitudu > 0 Then Dim sokituthem As Integer = 8 - sokitudu For i = 1 To sokituthem x += " " Next End If Dim sodaynhap As Integer = x.Length \ 8 ReDim daynhap(sodaynhap - 1) For i = 0 To sodaynhap - 1 daynhap(i) = x.Substring(i * 8, 8) y = "" For j = 0 To daynhap(i).Length - 1 y += bi_acsii(Asc(daynhap(i).Substring(j, 1))) Next daynhap(i) = y Next End Sub Function bi_acsii(ByVal int As Integer) As String Dim tam(7) As Char Dim i As Integer For i = 0 To 7 tam(i) = (int Mod 2).ToString int \= 2 Next Array.Reverse(tam) Return tam End Function Một số hàm mó hố Hàm hốn vị ip Function hvip(ByVal x As String) As String Dim tam(63) As Char Dim i As Integer For i = 0 To 63 tam(i) = x.Substring(ip(i) - 1, 1) Next Return tam End Function Hàm hốn vị e Function hve(ByVal r As String) As String Dim tam(47) As Char Dim i As Integer For i = 0 To 47 tam(i) = r.Substring(e(i) - 1, 1) Next Return tam End Function Function hvp(ByVal c As String) As String Dim tam(31) As Char Dim i As Integer For i = 0 To 31 tam(i) = c.Substring(p(i) - 1, 1) Next Return tam End Function Hàm hốn vị ip trừ Function hviptru(ByVal c As String) As String Dim tam(63) As Char Dim i As Integer For i = 0 To 63 tam(ip(i) - 1) = c.Substring(i, 1) Next Return tam End Function Mó hố Function mahoa() As String binarynhap() Dim k As Integer Dim y As String For k = 0 To daynhap.Length - 1 'x += "0000000100100011010001010110011110001001101010101100110111101111" x = daynhap(k) Dim x0 As String = hvip(x) Dim l(15) As String Dim r(15) As String Dim i, j As Integer Dim l0 As String = x0.Substring(0, 32) Dim r0 As String = x0.Substring(32, 32) l(0) = r0 For i = 0 To 31 r(0) += (l0.Substring(i, 1) Xor f(r0, daykhoa(0)).Substring(i, 1)).ToString Next For i = 1 To 15 l(i) = r(i - 1) Dim a As String = f(r(i - 1), daykhoa(i)) For j = 0 To 31 r(i) += (l

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

  • docHỆ MÃ DES.doc
  • docbia.doc
  • rarchương trinh.rar
Tài liệu liên quan