ҡդӶ ʵŧ 纺촨շ٨絴͵ ᷹ФѺ

͵Դҷҧ Inbox Ѻ

˹ѡ
-
VB 6/VB.Net
ASP/ASP.Net
Ѻ¤
¹ҹ Flash Movie
ռͺش  
 RSS Feeds
 ǹŴ RSS Reader ...   Download  RSS Reader

Forum - www.g2gnet.com
Webmaster - www.g2gnet.com
Visitors - Session views
 5 6 1 6 0 9 0

7 ѹҤ ..2549
44 Users On-Line.
Visitors - Page views
 8 9 7 3 8 3 3
1 Ҿѹ ..2551

Google   
g2gnet.com
Ҵѡ:  

ʹ µáʵ XOR (eXclusive OR) - Ҥ 1

Category »  VB 6/VB.Net
: Webmaster 14/9/2552   : 14:14
(ҹ : 54483) 
硷͹ԡ㹷ءҧ ͧ觿ѧҹ ... ŧ¼¹ 1 ҧ硷͹ԡ ... кҴҹѹ㹷ءѹ ǹ㹢ͧѹ١ҧҨҡػó硷͹ԡ駹 ... Եͧ¹硷͹ԡͧ ҡдѺ Ǫ (ѵԪ) ѹ (ѵ) ҷ 5 ... ͧѺ ... ͧҡ͹ǧ˹͡Ѻ ѹ繢ʹ վ鹰ҹ͹ҧ 觼Ҷ֧ءѹ ... ѧͼѧǹšͧҧԧѧ ҧҹ硷͹ԡ١شҹʹ ... ҧ ͧǧôԨԵ 觨ͧǡѺá (Logic) ͹ (Condition) õѴԹ (Decision) ¡ҧ ǧ AND, OR, NOR, NAND XOR Gate (Gate 㹷 ࡵ ... һе) ... ѧҸѺѹѧ仹
ɮ eXclusive OR (硫٫տ - XOR)
  • XOR 繡áзӷҧáʵ ѡ
    • ԹصͧԹص A, B (㹵ҧԧҹҧ)
    • ҷصͨԡ 0 Թص ͨԡ͹ѹ
    • ҷصͨԡ 1 Թص ͨԡҧѹ

    ҡѹ ... (Ե˹) դ͹ѹ 0 Ѻ դҵҧѹ 1
ҧԧ Truth Table (ҹԴ Truth Today )
    A B XOR
    0 0 0
    0 1 1
    1 0 1
    1 1 0

    True Xor True = False
    True Xor False = True
    False Xor True = True
    False Xor False = False

    1 Xor 1 = 0
    1 Xor 0 = 1
    0 Xor 1 = 1
    0 Xor 0 = 0
ôԹ÷ҧáʵͧ XOR
    ҨҤӵͺ ͵ͧŧŢҹ 10 Ţҹ 2 (Binary) ͹
    ˹ A = 10 ШŢҹ 2 (Binary) = 1 0 1 0
    ˹ B = 8 ШŢҹ 2 (Binary) = 1 0 0 0
    ѧ A Xor B
    1 0 1 0
    1 0 0 0
    ----------
    0 0 1 0 (ҹ 2) 2 (ҹ 10)
    =====
    سҹŧҹŨҡŢҹ 10 Ţҹ 2 Сͺ¤Ѻ
ôԹ÷ҧáʵͧ XOR дѺ٧
    ҷ¹ 65 Xor 20 ӵͺ 85 (ҹ 10) ... ͤöͧдѺ٧ (觤سդ繵ͧŧŢҹ͹)
    ԧ ѹԴԺѵԡдѺԵ ·ͧ ...ҵͧʹѹ
    ¤ 駵ǵ (65) ǡз (20) еͧ١ŧŢҹ 2 ͡ҡ͹ ѧ
    1 0 0 0 0 0 1 ... 65 (ҹ 10)
    X X 1 0 1 0 0 ... 20 (ҹ 10) ... 㴷ҡз XOR Ѻ X ҹ
    ------------------
    1 0 1 0 1 0 1 ... 85 (ҹ 10)
    ========
VB6 㹡õǨͺ ASCII Code

    Dim Dec As Byte
    
    For Dec = 0 To 255
        Debug.Print "Character : " & Chr(Dec) & " : ASCII Code = " & Dec
        If Dec = 255 Then Exit For
    Next
ôԹ÷ҧáʵͧ XOR MS Visual Basic 6.0
    Asc("A") Xor 255
    "A" ASCII Code = 65 (ҹ 10) Ҩ¹ 65 Xor 255
    蹤͵ѡ "A" Ţӹǹ 65 ... еͧŧŢҹ 2 ͹֧з XOR ФѺ ... ͧ
    "A" ( 65 ҹ 10) öШŢҹ 2 1 0 0 0 0 0 1
    255 öШŢҹ 2 1 1 1 1 1 1 1 1 255 (ҹ 10)

    X 1 0 0 0 0 0 1 (65) ... 㴷ҡз XOR Ѻ X ҹ
    1 1 1 1 1 1 1 1 (255)
    --------------------
    1 0 1 1 1 1 1 0 (190)
    =========
    ӵͺ 190 (ҹ 10) ... Ҩҡ˹ѡкԵ 128 + 32 + 16 + 8 + 4 + 2 ...
    BE (Ţҹ 16) çѺ "" 㹵ҧ ASCII Code


    ѺŢҹ 16 йѺҡŢҹ 2 4 Ե (ҡ仫)
    4 Եá (ҡش) ... 1 1 1 0 = 14 (ҹ 10) E (ҹ16)
    4 Ե 1 0 1 1 = 11 (ҹ 10) B (ҹ16)

    Ҷ֧çҡѧ ... سҡѺ֡ͧŢҹѹѺ ... ͧ

    ꡷ ... ҹҵҧʡ鴻Сͺ Ҿ

    Asc("A") = ASCII Code = 65 (ҹ 10)
    ͧǧ纡͹ Asc("A") Xor 255 ... Ţӹǹ 65 XOR 255 ... ӵͺ = 190 (ҹ 10)

    ҡ (Ϳѧ) Chr(Ţӹǹ) ... Chr(190)
    ... Chr(Asc("A") Xor 255)) = "" ... 繵ѡ



    ͡ùҵѡзе ҷӵáʵԸա XOR ¹ŧ ... ¡ҡ (Encryption)
    ҡӵѡз ҷӵáʵԸա XOR աͺ ... ¡ҡöʹ (Decryption)
ǹŴ
ǹŴ Source Code Ѻ MS Visual Basic 6.0 - Service Pack 6
 ǹŴ Visual Basic 6.0 SP5: Run-Time Redistribution Pack
 ǹŴ Microsoft Data Access Object (MDAC) Jet 4.0 Update
 ǹŴ Microsoft Visual Basic Service Pack 6

ʹ µáʵ XOR (eXclusive OR) - Ҥ 2
Ǩͺá鹤Ѻ੾еŢ Шشȹҹ (VB6 Ѻ)
Ǩͺá鹤Ѻ੾еŢ (VB.Net)
ŧŢҹ 10 繰ҹ 2 ... ͧ 觷ҧ㹹 ...
ԸաäԴѺŢӹǹ
鹡кǹ÷ӧҹ

Design Time


Run Time

͸Ժҫ ... Ҵѹ ...

Option Explicit

' ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' American Standard Code for Information Interchange - ASCII (ʡ)
' http://www.ascii.ca/cp874.htm
' http://en.wikipedia.org/wiki/ASCII
' ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

Private Sub Form_Load()

    Me.Move (Screen.Width - Width) \ 2, (Screen.Height - Height) \ 2
    
    txtData.Text = "This is a book"
    txtEncrypt.Text = ""
    txtDecrypt.Text = ""
    
End Sub

' ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' 鹡  ʹ
' ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Private Sub cmdRun_Click()

    If Trim(txtData.Text) = "" Or Len(Trim$(txtData.Text)) = 0 Then Exit Sub
    ' ʴ¡ eXclusive OR - XOR Ѻ
    '   ح  255 ŢӹǹҴ 1 亵
    ' ԴáздѺԵѺ XOR ҹ¹ŧ
    ' 1 1 1 1 1 1 1 1 (ҹ 2) = 255 (ҹ 10)
    txtEncrypt.Text = Encrypt(txtData.Text, 255)
    ' ҹԸաŧŢҹ 10 Ţҹ 2 Сͺ¤Ѻ
    ' http://www.g2gnet.com/News/activenews_view.asp?articleID=129
    
    ' ʹʴ¡ eXclusive OR - XOR ѺǶʹ
    txtDecrypt.Text = Decrypt(txtEncrypt.Text, 255)
    
End Sub

' ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' Ẻѡâͧ XOR - Ե͹ѹ 0 Ե㴷ҧѹ 1
'  MyData ѺҪشͤ
' XORvalue Ѻҵ  ح
' Ф׹ҡѺҡѧ仴
' ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Function Encrypt(MyData As String, XORvalue As Integer) As String
    Dim i As Integer
    
    ' ǹͺӹǹͧŷ
    For i = 1 To Len(MyData)
        Encrypt = Encrypt & Chr(Asc(Mid(MyData, i, 1)) Xor XORvalue)
    Next
End Function

' ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' öʹ (Decrypt) ͹ѺԸա  ...
' ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Function Decrypt(MyData As String, XORvalue As Integer) As String
    Dim i As Integer
    
    ' ǹͺӹǹͧŷʹ
    For i = 1 To Len(MyData)
        Decrypt = Decrypt & Chr(Asc(Mid(MyData, i, 1)) Xor XORvalue)
    Next
End Function
Conclusion:
ѡâ鹾鹰ҹͧ/ʹʢ µáʵẺ eXclusive OR 觼͸Ժ繶֧觻ԺѵԡдѺԵ ͡Ƕ֧ "Ե - Bit" ˹·硷شͧ (سҹ¹ѹ ͧѹ㨹 ... 55555+) ... ѧҾͧ礧繤Ӥѭ ùҹͧкǹ÷ҧҹáʵѹФѺ

͵ - g2gNet Dot Com
Ţ¹ҳԪ硷͹ԡ 0407314800231
CopyLeft © 2004 - 2099 g2gNet.Com All rights reserved.
Email: [email protected] . 08-6862-6560