Xor Block Cipher Calculation of 2 Hex Values

iCracked

New Member
Joined
Oct 9, 2015
Messages
2
Hello everyone,

I have been trying for some hours now to accomplish my goal. I am trying to use XOR to combine 2 Hex values into 1 hex value of the same length. There exist calculators that accomplish what I need, but they only do it one at a time, and I would like to use excel to perform this function for many values.

I have two hexadecimal values,

Input1:
1747F6001E00DB266F5728FE5257645C

<tbody>
</tbody><colgroup><col></colgroup>

Input2:
1C6262C8DBF510F6554E28EA3BDA58AC

<tbody>
</tbody><colgroup><col></colgroup>

The module I have loaded is:
Code:
Option Explicit
Sub test()
'this sub is only present to demonstrate use of the function!
'it is not required to use the function.
Dim r As Range, retVal, sKey As String
sKey = Application.InputBox("Enter your key", "Key entry", "My Key", , , , , 2)
retVal = MsgBox("This is the key you entered:" & vbNewLine & Chr$(34) & sKey & Chr$(34) & vbNewLine & _
        "Please confirm OK or Cancel to exit", vbOKCancel, "Confirm Key")
If retVal = vbCancel Then Exit Sub
For Each r In Sheets("Sheet1").UsedRange
    If r.Interior.ColorIndex = 6 Then
        r.Value = XorC(r.Value, sKey)
    End If
Next r
End Sub


Function XorC(ByVal sData As String, ByVal sKey As String) As String
    Dim l As Long, i As Long, byIn() As Byte, byOut() As Byte, byKey() As Byte
    Dim bEncOrDec As Boolean
    'confirm valid string and key input:
    If Len(sData) = 0 Or Len(sKey) = 0 Then XorC = "Invalid argument(s) used": Exit Function
    'check whether running encryption or decryption (flagged by presence of "xxx" at start of sData):
    If Left$(sData, 3) = "xxx" Then
        bEncOrDec = False   'decryption
        sData = Mid$(sData, 4)
    Else
        bEncOrDec = True   'encryption
    End If
    'assign strings to byte arrays (unicode)
    byIn = sData
    byOut = sData
    byKey = sKey
    l = LBound(byKey)
    For i = LBound(byIn) To UBound(byIn) - 1 Step 2
        byOut(i) = ((byIn(i) + Not bEncOrDec) Xor byKey(l)) - bEncOrDec 'avoid Chr$(0) by using bEncOrDec flag
        l = l + 2
        If l > UBound(byKey) Then l = LBound(byKey)  'ensure stay within bounds of Key
    Next i
    XorC = byOut
    If bEncOrDec Then XorC = "xxx" & XorC  'add "xxx" onto encrypted text
End Function

But this just outputs: 'NAME!'

I have tried other modules suggested, but the output only ever comes out as [][][]nw[]s[] or something unreadable like that.

The expected result is:
0B2594C8C5F5CBD03A190014698D3CF

If anyone can help me, that would be greatly appreciated. Thanks in advance!!
 
Missed that, thanks.

Code:
Function XorHex(ByVal h1 As String, ByVal h2 As String) As String
  Dim i             As Long

  If Len(h1) > Len(h2) Then h2 = String(Len(h1) - Len(h2), "0") & h2
  If Len(h2) > Len(h1) Then h1 = String(Len(h2) - Len(h1), "0") & h1

  For i = 1 To Len(h1) Step 8
    XorHex = XorHex & Right("0000000" & Hex("&H" & Mid(h1, i, 8) Xor "&H" & Mid(h2, i, 8)), 8)
  Next i
End Function

Row\Col
A​
B​
C​
1​
A0000000000000000100000000000000B1: =XorHex(A1,A2)
2​
A100000000000000
I don't think the answer that code produces is correct for these two values...

A000

A10000001
 
Upvote 0

Excel Facts

When they said...
When they said you are going to "Excel at life", they meant you "will be doing Excel your whole life".
I should quit while I'm behind :)

Code:
Function XorHex(ByVal h1 As String, ByVal h2 As String) As String
  Dim i             As Long

  If Len(h1) > Len(h2) Then h2 = String(Len(h1) - Len(h2), "0") & h2
  If Len(h2) > Len(h1) Then h1 = String(Len(h2) - Len(h1), "0") & h1

  For i = 1 To Len(h1) Step 8
    XorHex = XorHex & Right("0000000" & Hex("&H" & Mid(h1, i, 8) Xor "&H" & Mid(h2, i, 8)), Len(Mid(h2, i, 8)))
  Next i
End Function

Row\Col
A​
B​
C​
1​
A000A10000A001B1: =XorHex(A1,A2)
2​
A10000001
 
Upvote 0
Hi

This last version from shg seems to work fine.

I would just add a statement at the end to remove the leading 0's as they don't usually appear in numbers.


For ex., using MS Windows calculator:

A0100000000 Xor A0200000000 = 300000000
 
Last edited:
Upvote 0
P. S.

I just noticed that the OP left the leading 0 in the example posted, so maybe this could be an option (or don't even bother).
 
Upvote 0
If desired:

Code:
  XorHex = Replace(LTrim(Replace(XorHex, "0", " ")), " ", "0")
 
Upvote 0

Forum statistics

Threads
1,215,366
Messages
6,124,514
Members
449,168
Latest member
CheerfulWalker

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top