<%@ Page Language="VB" ContentType="text/html" ResponseEncoding="windows-874" %> http://www.siam2dev.com >> ชุมชนนักพัฒนาซอฟต์แวร์ของไทยแห่งใหม่
เข้าสู่ระบบ ::    
http://www.siam2dev.com >> ชุมชนนักพัฒนาซอฟต์แวร์แห่งใหม่
 
Home   
     สวัสดีครับทุกท่าน ผมพัฒนาเว็บนี้ขึ้นมาโดยมีวัตถุประสงค์ ที่จะเห็นโปรแกรมเมอร์ของไทย ได้มีการคิดค้นและพัฒนาซอฟต์แวร์ ของคนไทย กันมากขึ้น เพื่อเป็นการช่วยชาตของเราให้เจริญยิ่งขึ้นไป (จะได้ไม่ต้องทะเลาะกันอย่างทุกวันนี้ 555 อย่าเครียดครับ ) เนื่องจากเว็บนี้ยังเพิ่งเริ่มต้นนะครับอาจมีเนื้อหาบางงส่วนที่ยังไม่สมบูรณ์จึงขออภัยมา ณ โอกาส นี้ด้วยครับ
 
 
   VB.NET Programming
     การเข้ารหัส(Encrypt) แบบ Columnar Transposition Cipher

     Columnar Transposition Cipher ?
     Columnar Transposition Cipher เป็นอัลกอริทึมที่คล้ายกับ Rail Fence Cipher ซึ่งมีความแตกต่างกันที่วิธีอ่านและเขียน โดยจะเขียนตามแนวแถว และอ่านตามแนวคอลัมน์ในเมตริกซ์ รวมทั้งลำดับคอลัมน์ในการอ่าน อัลกอรึทึมนี้อาจจะต้องเพิ่มการตรวจสอบ input จาก user ในการกรอก key เพราะต้องเป็นตัวเลขที่ไม่ซ้ำ แต่ต่อเนื่องและไม่ต้องเรียงลำดับ ซึ่งในที่นี้จะให้เริ่มที่ 1 ถึง 9
จากรูปซ้าย เมื่อเข้ารหัสแล้วจะได้ผลลัพธ์ ดังนี้ EVLNE ACDTK ESEAQ ROFOJ DEECU WIREE กรณีเมตริกซ์ไม่เต็ม แบบรูปขวา ให้อ่านข้ามช่องว่าง (ในโค้ดจะใส่ค่า Nothing ลงไป) ไปได้เลย ผลลัพธ์คือ EVLNA CDTES EAROF ODEEC WIREE

     - ตัวอย่างโค้ดการเข้ารหัส(Encrypt) แบบ Columnar Transposition Cipher
          
          
Function Encrypt(ByVal PlainText As String, ByVal Key As String) As String
'หาจำนวนคอลัมน์
Dim ColumnNum As Byte = Key.Length

'หาจำนวนแถว
Dim RowNum As Byte = PlainText.Length \ Key.Length
If (PlainText.Length Mod Key.Length) > 0 Then RowNum += 1

Dim CharMatrix(RowNum - 1, ColumnNum - 1) As Char
Dim CharArray(CharMatrix.Length - 1) As Char

'นำตัวอักษรใน PlainText มาเรียงในเมตริกซ์ (Array 2 มิติ)
'เรียงจากซ้ายไปขวาทีละแถว
Dim Count As Byte = 0
For CountRow As Byte = 0 To RowNum - 1
For CountCol As Byte = 0 To ColumnNum - 1
If Count <= PlainText.Length - 1 Then
CharMatrix(CountRow, CountCol) = PlainText(Count)
Count += 1
Else
CharMatrix(CountRow, CountCol) = Nothing
End If
Next CountCol
Next CountRow

'อ่านตัวอักษรในเมตริกซ์ จากบนลงล่าง ทีละคอลัมน์
'มาเรียงเป็นข้อความแถวเดียว
Count = 0
For CountCol As Byte = 1 To Key.Length
'หา column index ตามลำดับก่อนหลัง เก็บไว้ที่ตัวแปร ColumnOrder
Dim ColumnOrder As Byte
For ColumnOrder = 0 To Key.Length - 1
Dim Num As Byte = CByte(Key(ColumnOrder).ToString)
If CountCol = Num Then Exit For
Next ColumnOrder

For CountRow As Byte = 0 To RowNum - 1
If Not CharMatrix(CountRow, ColumnOrder) = Nothing Then
CharArray(Count) = CharMatrix(CountRow, ColumnOrder)
Count += 1
End If
Next CountRow
Next CountCol

'ส่งค่าตัวอักษรที่เป็น array กลับเป็น string
Return CharArray
End Function

     - ตัวอย่างโค้ดการถอดรหัส(Decrypt) แบบ Columnar Transposition Cipher


Function Decrypt(ByVal CipherText As String, ByVal Key As String) As String
'หาจำนวนคอลัมน์
     Dim ColumnNum As Byte = Key.Length

     'หาจำนวน cell ว่าง
     Dim EmptyNum As Byte
     Dim CharTotal As Byte = CipherText.Length
     Dim Fraction As Byte = CharTotal Mod ColumnNum
     EmptyNum = IIf(Fraction = 0, 0, ColumnNum - Fraction)

     'หาจำนวนแถว
     Dim RowNum As Byte = CipherText.Length \ Key.Length
     If (CipherText.Length Mod Key.Length) > 0 Then RowNum += 1

     Dim CharMatrix(RowNum - 1, ColumnNum - 1) As Char
     Dim CharArray(CharMatrix.Length - 1) As Char

     'นำตัวอักษรใน CipherText มาเรียงในเมตริกซ์ (Array 2 มิติ)
     'เรียงจากบนลงล่าง ทีละคอลัมน์
     Dim Count As Byte = 0
     For CountCol As Byte = 1 To Key.Length
          'หา column index ตามลำดับก่อนหลัง เก็บไว้ที่ตัวแปร ColumnOrder
          Dim ColumnOrder As Byte
          For ColumnOrder = 0 To Key.Length - 1
               Dim Num As Byte = CByte(Key(ColumnOrder).ToString)
               If CountCol = Num Then Exit For
          Next ColumnOrder

     'คอมลัมน์ที่ไม่มีช่องว่าง
     If ColumnOrder <= (Key.Length - 1 - EmptyNum) Then
     For CountRow As Byte = 0 To RowNum - 1
          CharMatrix(CountRow, ColumnOrder) = CipherText(Count)
          Count += 1
     Next CountRow
     Else 'คอมลัมน์ที่มีช่องว่าง (Nothing)
     For CountRow As Byte = 0 To RowNum - 2
          CharMatrix(CountRow, ColumnOrder) = CipherText(Count)
          Count += 1
     Next CountRow
     End If
     Next CountCol

     'อ่านตัวอักษรในเมตริกซ์ จากซ้ายไปขวา ทีละแถว
     'มาเรียงเป็นข้อความแถวเดียว
     Count = 0
     For CountRow As Byte = 0 To RowNum - 1
          For CountCol As Byte = 0 To ColumnNum - 1
               If Count <= CipherText.Length - 1 Then
                    CharArray(Count) = CharMatrix(CountRow, CountCol)
                    Count += 1
               End If
          Next CountCol
     Next CountRow

     'ส่งค่าตัวอักษรที่เป็น array กลับเป็น string
     Return CharArray
End Function

แหล่งข้อมูล :
http://teedech.blogspot.com/2007/07/columnar-transposition-cipher-columnar.html
Transposition cipher - wikipedia
Cryptography/Transposition ciphers - wikibooks
Transposition ciphers - Torbj?rn Andersson
Columnar Transposition Ciphers - The Contest Center
Braingle - มีโปรแกรมให้เล่น
 
:: http://www.siam2dev.com ::
e-mail :: xnattapong@hotmail.com , songneam@gmail.com