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!!
 

Excel Facts

Which Excel functions can ignore hidden rows?
The SUBTOTAL and AGGREGATE functions ignore hidden rows. AGGREGATE can also exclude error cells and more.
I believe I have found what I need to do, and that is that I need to use the bitwise function of XOR in Excel.

The problem is, BITXOR only works on decimal numbers, and not hexadecimal.

Still need help with that part.

Thanks again in advance.
 
Upvote 0
Hi, this seems to give the same results as this on-line calculator https://xor.pw/?

Code:
Function fXorHex(h1 As String, h2 As String) As String
Dim i As Long
For i = 1 To Len(h1) Step 8
    fXorHex = fXorHex & Hex("&H" & Mid(h1, i, 8) Xor "&H" & Mid(h2, i, 8))
Next i
End Function



Excel 2012
A
11747F6001E00DB266F5728FE5257645C
21C6262C8DBF510F6554E28EA3BDA58AC
3B2594C8C5F5CBD03A190014698D3CF0
Sheet1
Cell Formulas
RangeFormula
A3=fXorHex(A1,A2)
 
  • Like
Reactions: shg
Upvote 0
Hi FormR

You may lose zeroes in each loop, if the result of xor in the loop has 0's on the left.

For ex., try with

A000000000000000
A100000000000000

You should get a 1 followed by 14 0's, but instead you get

10000000

In the second loop you concatenate with hex("00000000" xor "00000000")=hex("00000000")=0, the hex will gobble all the 0's on the left.

Please check.
 
Upvote 0
Hi PGC,

I have to hold my hands up and say I'm not an expert in this area, I think I must have got lucky with the random checks I did.

Would this be an appropriate correction, or am I over simplifying the flaw?

Code:
Function fXorHex(h1 As String, h2 As String) As String
Dim i As Long
For i = 1 To Len(h1) Step 8
    fXorHex = fXorHex & Right("00000000" & Hex("&H" & Mid(h1, i, 8) Xor "&H" & Mid(h2, i, 8)), Len(Mid(h1, i, 8)))
Next i
End Function
 
Upvote 0
Hi

I did not test but it seems OK if both hex numbers have the same number of digits.

Remark:

Usually, like in the base 10 numbers, you may assume 0's on the left if necessary.

For ex., with hex numbers (A3 xor 45C) you would assume (0A3 xor 45C). I don't if that's necessary in this case.
If it does then I guess you'll have to amend the code for hex numbers with different number of digits.
 
Upvote 0
I think PGC means like this:

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 & Hex("&H" & Mid(h1, i, 8) Xor "&H" & Mid(h2, i, 8))
  Next i
End Function

Row\Col
A​
B​
C​
1​
A34FFB1: =XorHex(A1,A2)
2​
45C
 
Upvote 0
I think PGC means like this:

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 & Hex("&H" & Mid(h1, i, 8) Xor "&H" & Mid(h2, i, 8))
  Next i
End Function

Row\Col
A​
B​
C​
1​
A34FFB1: =XorHex(A1,A2)
2​
45C
But that code appears to fail for PGC's values posted in Message #4.

While a little "long winded" (especially for me :eek:), the following works for all examples posted so far (although a get a leading 0 in the answer for the values from Message #4). There are three functions, but you only call the HEXOR function (it, in turn, calls the other two as it needs them)...
Code:
Function HEXOR(Hex1 As String, Hex2 As String) As String
  Dim x As Long, MaxLen As Long, Bin1 As String, Bin2 As String, BinOut As String
  Bin1 = HexToBin(Hex1)
  Bin2 = HexToBin(Hex2)
  MaxLen = Application.Max(Len(Bin1), Len(Bin2))
  Bin1 = Format$(Bin1, String(MaxLen, "0"))
  Bin2 = Format$(Bin2, String(MaxLen, "0"))
  For x = MaxLen To 1 Step -1
    BinOut = CStr(CByte(Mid(Bin1, x, 1)) Xor CByte(Mid(Bin2, x, 1))) & BinOut
  Next
  HEXOR = BinToHex(BinOut)
End Function

Function HexToBin(HexString As String, Optional _
                  WithBlanks As Boolean) As Variant
  Dim x As Integer
  Const BinValues = "00000001001000110100010101100111" & _
                    "10001001101010111100110111101111"
  For x = 1 To Len(HexString)
    HexToBin = HexToBin & Mid$(BinValues, 4 * Val( _
               "&h" & Mid$(HexString, x, 1)) + 1, 4)
    If WithBlanks Then HexToBin = HexToBin & " "
  Next
End Function

Function BinToHex(ByVal BinaryString As String) As String
  Dim x As Integer
  Const BinValues = "*0000*0001*0010*0011*0100*0101*0110*0111" & _
                    "*1000*1001*1010*1011*1100*1101*1110*1111*"
  Const HexValues = "0123456789ABCDEF"
  If BinaryString Like "*[!01]*" Then
    BinToHex = "Error - Argument not a binary string"
  Else
    BinaryString = String$((4 - Len(BinaryString) _
                   Mod 4) Mod 4, "0") & BinaryString
    For x = 1 To Len(BinaryString) - 3 Step 4
      BinToHex = BinToHex & Mid$(HexValues, (4 + InStr(BinValues, _
                 "*" & Mid$(BinaryString, x, 4) & "*")) \ 5, 1)
    Next
  End If
End Function
 
Upvote 0
But that code appears to fail for PGC's values posted in Message #4.

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

although a get a leading 0 in the answer for the values from Message #4
I would argue that the result should be the same length as the longer input.

BTW: All the good parts of the code credit FormR.
 
Last edited:
Upvote 0

Forum statistics

Threads
1,215,350
Messages
6,124,431
Members
449,158
Latest member
burk0007

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