[Cryptography] SHA-256 decrypted (8 rounds)

McDair mcdair at protonmail.com
Mon Nov 6 10:18:09 EST 2023


Hi,

Hereby the code to decrypt 8 rounds of SHA-256 deterministically.
An original input message length up to 447 bits (single block) is supported.

The same method can be applied to SHA-512.

Apologies for the VB.NET code.

Sincerely,

8ff3f7366e32be1bd139acd9146ebab6a150fe7e65de72a8f4910b0769c72d48

Option Explicit On
Option Strict On

Public Class Sha256

Private Const forceOperationInt64 As Int64 = 0
Private Const forceOperationUInt64 As UInt64 = 0

' for debugging
Private ReadOnly rounds As Integer = 8

' algoRithm constants

Private ReadOnly k As UInt32() = New UInt32() {
&H428A2F98UI, &H71374491UI, &HB5C0FBCFUI, &HE9B5DBA5UI, &H3956C25BUI, &H59F111F1UI, &H923F82A4UI, &HAB1C5ED5UI,
&HD807AA98UI, &H12835B01UI, &H243185BEUI, &H550C7DC3UI, &H72BE5D74UI, &H80DEB1FEUI, &H9BDC06A7UI, &HC19BF174UI,
&HE49B69C1UI, &HEFBE4786UI, &HFC19DC6UI, &H240CA1CCUI, &H2DE92C6FUI, &H4A7484AAUI, &H5CB0A9DCUI, &H76F988DAUI,
&H983E5152UI, &HA831C66DUI, &HB00327C8UI, &HBF597FC7UI, &HC6E00BF3UI, &HD5A79147UI, &H6CA6351UI, &H14292967UI,
&H27B70A85UI, &H2E1B2138UI, &H4D2C6DFCUI, &H53380D13UI, &H650A7354UI, &H766A0ABBUI, &H81C2C92EUI, &H92722C85UI,
&HA2BFE8A1UI, &HA81A664BUI, &HC24B8B70UI, &HC76C51A3UI, &HD192E819UI, &HD6990624UI, &HF40E3585UI, &H106AA070UI,
&H19A4C116UI, &H1E376C08UI, &H2748774CUI, &H34B0BCB5UI, &H391C0CB3UI, &H4ED8AA4AUI, &H5B9CCA4FUI, &H682E6FF3UI,
&H748F82EEUI, &H78A5636FUI, &H84C87814UI, &H8CC70208UI, &H90BEFFFAUI, &HA4506CEBUI, &HBEF9A3F7UI, &HC67178F2UI
}

Private ReadOnly initHash As UInt32() = New UInt32() {
&H6A09E667UI, &HBB67AE85UI, &H3C6EF372UI, &HA54FF53AUI, &H510E527FUI, &H9B05688CUI, &H1F83D9ABUI, &H5BE0CD19UI
}

Private Function RightRotate(value As UInt32, bits As Byte) As UInt32
Return (value >> bits) Or (value << (32 - bits))
End Function

Public Function Encrypt(value As String,
ByRef debugWords As UInt32()(),
ByRef debugVariables As UInt32()(,)
) As String

Dim resultHexString As String

resultHexString = Nothing
Me.Encrypt(System.Text.Encoding.UTF8.GetBytes(value), resultHexString, debugWords, debugVariables)

Return resultHexString

End Function

Public Function Encrypt(value As Byte(),
ByRef resultHexString As String,
ByRef debugWords As UInt32()(),
ByRef debugVariables As UInt32()(,)
) As Byte()

Dim resultBytes(31) As Byte

Dim messageLength As UInt64
Dim messageLengthBytes() As Byte ' 8 bytes

Dim i As Integer
Dim wordBytes(3) As Byte

Dim currentWords(63) As UInt32
Dim currentWord As Integer
Dim remainingBytes As Integer

Dim currentHash() As UInt32 ' 8 unsigned integers

' for dEbugging
Dim blockCount As Integer
Dim currentBlock As Integer
Dim words()() As UInt32
Dim variables()(,) As UInt32

' for debugging
' one additional byte for the extra bit 1, and 8 additional bytes for the MEssage length
blockCount = CType(Math.Ceiling((value.Length + 1 + 8) / 64), Integer)
ReDim words(blockCount - 1)
ReDim variables(blockCount - 1)
currentBlock = 0

remainingBytes = value.Length Mod 4

currentHash = Me.initHash

currentWord = 0
For i = 0 To value.Length - remainingBytes - 1 Step 4
If BitConverter.IsLittleEndian Then
wordBytes(0) = value(i + 3)
wordBytes(1) = value(i + 2)
wordBytes(2) = value(i + 1)
wordBytes(3) = value(i)
Else
wordBytes(0) = value(i)
wordBytes(1) = value(i + 1)
wordBytes(2) = value(i + 2)
wordBytes(3) = value(i + 3)
End If

currentWords(currentWord) = BitConverter.ToUInt32(wordBytes, 0)

If currentWord = 15 Then
' BLOCK READY

' calculate derived words and compress
Me.AddDerivedWords(currentWords)
' for debugging: pass debug variables
currentHash = Me.Encrypt(currentHash, currentWords, variables(currentBlock))

' for debugging
' clone array, otherwise all word arrays in 'words' will refer to the saMe array ('currentWords')
words(currentBlock) = DirectCast(currentWords.Clone, UInt32())
currentBlock += 1

' then reset word array
currentWord = 0

Else
currentWord += 1
End If

Next

' last word (remaining BytEs + addition bit 1)
If BitConverter.IsLittleEndian Then
' Remaining bytes
For i = 0 To remainingBytes - 1
wordBytes(3 - i) = value(value.Length - remainingBytes + i)
Next
' addition bit 1
wordBytes(3 - remainingBytes) = &H80

' reset unused bytes
For i = remainingBytes + 1 To 3
wordBytes(3 - i) = 0
Next
Else
' remaining bytes
For i = 0 To remainingBytes - 1
wordBytes(i) = value(value.Length - remainingBytes + i)
Next
' addition bit 1
wordBytes(remainingBytes) = &H80

' reset unused bytes
For i = remainingBytes + 1 To 3
wordBytes(i) = 0
Next
End If

currentWords(currentWord) = BitConverter.ToUInt32(wordBytes, 0)

If currentWord < 14 Then
' last block
For i = currentWord + 1 To 13
currentWords(i) = 0
Next

Else
' an extra block is needed for the message length
For i = currentWord + 1 To 15
currentWords(i) = 0
Next
' BLOCK READY

' calculate derived words and compress
Me.AddDerivedWords(currentWords)
' for debugging: pass debug variables
currentHash = Me.Encrypt(currentHash, currentWords, variables(currentBlock))

' for debugging
' clone array, otherwise all word arrays in 'words' will refer to the same array ('currentWords')
words(currentBlock) = DirectCast(currentWords.Clone, UInt32())
currentBlock += 1

' reset word array
For i = 0 To 13
currentWords(i) = 0
Next

End If

' add message length to last block
messageLength = CType(value.Length * 8, UInt64) ' in bits
messageLengthBytes = BitConverter.GetBytes(messageLength)

If BitConverter.IsLittleEndian Then
currentWords(14) = BitConverter.ToUInt32(messageLengthBytes, 4)
currentWords(15) = BitConverter.ToUInt32(messageLengthBytes, 0)
Else
currentWords(14) = BitConverter.ToUInt32(messageLengthBytes, 0)
currentWords(15) = BitConverter.ToUInt32(messageLengthBytes, 4)
End If

' BLOCK READY

' calculate derived words and compress
Me.AddDerivedWords(currentWords)
' for debugging: pass debug variables
currentHash = Me.Encrypt(currentHash, currentWords, variables(currentBlock))

' for debugging
' clone array is not necessary, as it is the last array,
' and there are no more manipulations with array 'currentWords' hereafter
'words(currentBlock) = DirectCast(currentWords.Clone, UInt32())
words(currentBlock) = currentWords

' return resulting hash

' as a byte array (big-endian)
If BitConverter.IsLittleEndian Then
BitConverter.GetBytes(currentHash(0)).Reverse.ToArray.CopyTo(resultBytes, 0)
BitConverter.GetBytes(currentHash(1)).Reverse.ToArray.CopyTo(resultBytes, 4)
BitConverter.GetBytes(currentHash(2)).Reverse.ToArray.CopyTo(resultBytes, 8)
BitConverter.GetBytes(currentHash(3)).Reverse.ToArray.CopyTo(resultBytes, 12)
BitConverter.GetBytes(currentHash(4)).Reverse.ToArray.CopyTo(resultBytes, 16)
BitConverter.GetBytes(currentHash(5)).Reverse.ToArray.CopyTo(resultBytes, 20)
BitConverter.GetBytes(currentHash(6)).Reverse.ToArray.CopyTo(resultBytes, 24)
BitConverter.GetBytes(currentHash(7)).Reverse.ToArray.CopyTo(resultBytes, 28)

Else
BitConverter.GetBytes(currentHash(0)).CopyTo(resultBytes, 0)
BitConverter.GetBytes(currentHash(1)).CopyTo(resultBytes, 4)
BitConverter.GetBytes(currentHash(2)).CopyTo(resultBytes, 8)
BitConverter.GetBytes(currentHash(3)).CopyTo(resultBytes, 12)
BitConverter.GetBytes(currentHash(4)).CopyTo(resultBytes, 16)
BitConverter.GetBytes(currentHash(5)).CopyTo(resultBytes, 20)
BitConverter.GetBytes(currentHash(6)).CopyTo(resultBytes, 24)
BitConverter.GetBytes(currentHash(7)).CopyTo(resultBytes, 28)
End If

' as a hex string
resultHexString =
currentHash(0).ToString("x8") & currentHash(1).ToString("x8") & currentHash(2).ToString("x8") & currentHash(3).ToString("x8") &
currentHash(4).ToString("x8") & currentHash(5).ToString("x8") & currentHash(6).ToString("x8") & currentHash(7).ToString("x8")

' for debugging
debugWords = words
debugVariables = variables

Return resultBytes

End Function

Private Sub AddDerivedWords(ByRef words As UInt32())
Dim currentWord As Integer

For currentWord = 16 To 63

words(currentWord) = CType((
Sha256.forceOperationUInt64 +
words(currentWord - 16) +
words(currentWord - 7) +
(Me.RightRotate(words(currentWord - 15), 7) Xor Me.RightRotate(words(currentWord - 15), 18) Xor (words(currentWord - 15) >> 3)) +
(Me.RightRotate(words(currentWord - 2), 17) Xor Me.RightRotate(words(currentWord - 2), 19) Xor (words(currentWord - 2) >> 10))
) And UInt32.MaxValue, UInt32)

Next
End Sub

Private Function Encrypt(hash As UInt32(),
words As UInt32(),
ByRef debugVariables As UInt32(,)) As UInt32()

Dim a, b, c, d, e, f, g, h As UInt32
Dim s1_ch_k_h_w As UInt64
Dim round As Integer
Dim resultHash(7) As UInt32

' for debugging
Dim variables((Me.rounds + 1) - 1, 7) As UInt32

' initialize working variables to current hash value
a = hash(0)
b = hash(1)
c = hash(2)
d = hash(3)
e = hash(4)
f = hash(5)
g = hash(6)
h = hash(7)

' for debugging
variables(0, 0) = a
variables(0, 1) = b
variables(0, 2) = c
variables(0, 3) = d
variables(0, 4) = e
variables(0, 5) = f
variables(0, 6) = g
variables(0, 7) = h

' compression function main loop
For round = 0 To Me.rounds - 1
s1_ch_k_h_w = Sha256.forceOperationUInt64 +
(Me.RightRotate(e, 6) Xor Me.RightRotate(e, 11) Xor Me.RightRotate(e, 25)) +
((e And f) Xor ((Not e) And g)) +
Me.k(round) +
h +
words(round)

h = g
g = f
f = e
e = CType((d + s1_ch_k_h_w) And UInt32.MaxValue, UInt32)
d = c
c = b
b = a
a = CType((
s1_ch_k_h_w +
(Me.RightRotate(b, 2) Xor Me.RightRotate(b, 13) Xor Me.RightRotate(b, 22)) +
((b And c) Xor (b And d) Xor (c And d))
) And UInt32.MaxValue, UInt32)

' for debugging
variables(round + 1, 0) = a
variables(round + 1, 1) = b
variables(round + 1, 2) = c
variables(round + 1, 3) = d
variables(round + 1, 4) = e
variables(round + 1, 5) = f
variables(round + 1, 6) = g
variables(round + 1, 7) = h

Next

' for debugging
debugVariables = variables

resultHash(0) = CType((Sha256.forceOperationUInt64 + hash(0) + a) And UInt32.MaxValue, UInt32)
resultHash(1) = CType((Sha256.forceOperationUInt64 + hash(1) + b) And UInt32.MaxValue, UInt32)
resultHash(2) = CType((Sha256.forceOperationUInt64 + hash(2) + c) And UInt32.MaxValue, UInt32)
resultHash(3) = CType((Sha256.forceOperationUInt64 + hash(3) + d) And UInt32.MaxValue, UInt32)
resultHash(4) = CType((Sha256.forceOperationUInt64 + hash(4) + e) And UInt32.MaxValue, UInt32)
resultHash(5) = CType((Sha256.forceOperationUInt64 + hash(5) + f) And UInt32.MaxValue, UInt32)
resultHash(6) = CType((Sha256.forceOperationUInt64 + hash(6) + g) And UInt32.MaxValue, UInt32)
resultHash(7) = CType((Sha256.forceOperationUInt64 + hash(7) + h) And UInt32.MaxValue, UInt32)

Return resultHash

End Function

Private Function GetVariablesLastRound(refHash As UInt32(), hash As UInt32()) As UInt32()
Dim variables(7) As UInt32

variables(0) = CType((Sha256.forceOperationInt64 + refHash(0) - hash(0)) And UInt32.MaxValue, UInt32)
variables(1) = CType((Sha256.forceOperationInt64 + refHash(1) - hash(1)) And UInt32.MaxValue, UInt32)
variables(2) = CType((Sha256.forceOperationInt64 + refHash(2) - hash(2)) And UInt32.MaxValue, UInt32)
variables(3) = CType((Sha256.forceOperationInt64 + refHash(3) - hash(3)) And UInt32.MaxValue, UInt32)
variables(4) = CType((Sha256.forceOperationInt64 + refHash(4) - hash(4)) And UInt32.MaxValue, UInt32)
variables(5) = CType((Sha256.forceOperationInt64 + refHash(5) - hash(5)) And UInt32.MaxValue, UInt32)
variables(6) = CType((Sha256.forceOperationInt64 + refHash(6) - hash(6)) And UInt32.MaxValue, UInt32)
variables(7) = CType((Sha256.forceOperationInt64 + refHash(7) - hash(7)) And UInt32.MaxValue, UInt32)

Return variables
End Function

Private Sub OutputResult(resultVariables As UInt32(,), resultWords As UInt32(),
refVariables As UInt32(,), refWords As UInt32(),
actualVariables As UInt32(,), actualWords As UInt32())
Dim round As Integer
Dim variable As Integer

For round = Me.rounds To 0 Step -1
Debug.Write("------------------------- variables n - " & round)

If round < Me.rounds Then
Debug.WriteLine(" | w = " & actualWords(round).ToString.PadRight(10) & " | w found = " & resultWords(round))
Else
Debug.WriteLine("")
End If

For variable = 0 To 7
Debug.WriteLine(actualVariables(round, variable).ToString.PadRight(10) & " | " &
refVariables(round, variable).ToString.PadRight(10) & " | " &
resultVariables(round, variable).ToString.PadRight(10)
)
Next
Next
End Sub

''' <summary>
''' Assume only one block in this version
''' </summary>
Friend Function Decrypt(hexValue As String, debugWords()() As UInt32, debugVariables()(,) As UInt32) As String

Dim hash(7) As UInt32
Dim resultWords As UInt32()
Dim resultBytes(63) As Byte

For w = 0 To 7
hash(w) = Convert.ToUInt32(hexValue.Substring(w * 8, 8), 16)
Next

resultWords = Me.Decrypt(hash, debugWords, debugVariables)

If BitConverter.IsLittleEndian Then
For w = 0 To resultWords.Length - 1
BitConverter.GetBytes(resultWords(w)).Reverse.ToArray.CopyTo(resultBytes, w * 4)
Next
Else
For w = 0 To resultWords.Length - 1
BitConverter.GetBytes(resultWords(w)).CopyTo(resultBytes, w * 4)
Next
End If

' get message length (not supported in this 8 rounds version)

' reset additional bit (do not use message length in this 8 rounds version)
' find first non-zero bit (byte)

For b = resultBytes.Length - 1 To 0 Step -1
If resultBytes(b) = &H80 Then
resultBytes(b) = 0

Exit For
End If

Next

Return System.Text.Encoding.UTF8.GetString(resultBytes)

End Function

Private Function Decrypt(hash() As UInt32, debugWords()() As UInt32, debugVariables()(,) As UInt32) As UInt32()

Dim refWords(Me.rounds - 1) As UInt32
Dim refVariables(,) As UInt32
Dim variablesLastRound As UInt32()
Dim resultVariables As UInt32(,)
Dim resultWords As UInt32()
Dim round As Integer

Dim a_ref, b_ref, c_ref, d_ref, e_ref, f_ref, g_ref, h_ref As UInt32
Dim a_res, b_res, c_res, d_res, e_res, f_res, g_res, h_res As UInt32

Dim s0_maj_ref As Int64
Dim s0_maj_res As Int64

Dim diff_s0_maj As Int64
Dim diff_d As Int64

Dim word As UInt32

variablesLastRound = Me.GetVariablesLastRound(hash, Me.initHash)

' start with empty reference words

' get reference variables for words passed (words passed are initially empty)
' (we don't use the resulting hash)
refVariables = Nothing
Me.Encrypt(Me.initHash, refWords, refVariables)

resultVariables = Nothing
resultWords = Nothing
Me.Decrypt(variablesLastRound, refVariables, resultVariables, resultWords)

If debugVariables IsNot Nothing And debugWords IsNot Nothing Then
Me.OutputResult(resultVariables, resultWords, refVariables, refWords, debugVariables(0), debugWords(0))
End If

' resulting round 0 variables e, f, g, h should match actual round 0 variables (initial hash)

' we can now check whether resulting w1 is correct:

' 1) resulting w1 is incorrect when resulting d0 doesn't match actual d0 (initial hash) because:

' e1 = d0 + fx(e0, f0, g0) + k1 + h0 + w1

' --> h0 resolves to correct (initial hash) value, so we can leave h0 out of the equation
' --> e0, f0, g0 resolve to correct (initial hash) values, so we can leave fx(e0, f0, g0) out of the equation
' --> d0 doesn't resolve to correct (initial hash) value, so we have to take into account the difference
' of resulting d0 and actual d0 to adjust resulting w1

' 2) resulting w1 is incorrect when resulting fx(a0, b0, c0) doesn't match actual fx(a0, b0, c0) because:

' a1 = fx(e0, f0, g0) + k1 + h0 + w1 + fx(a0, b0, c0)

' --> h0 resolves to correct (initial hash) value, so we can leave h0 out of the equation
' --> e0, f0, g0 resolve to correct (initial hash) values, so we can leave fx(e0, f0, g0) out of the equation
' --> fx(a0, b0, c0) doesn't resolve to correct value, so we have to take into account the difference
' of resulting fx(a0, b0, c0) and actual fx(a0, b0, c0) to adjust resulting w1

' adjust reference words (and reference variables as a result)

For round = 0 To Me.rounds - 1 - 4

a_ref = refVariables(round, 0)
b_ref = refVariables(round, 1)
c_ref = refVariables(round, 2)
d_ref = refVariables(round, 3)
e_ref = refVariables(round, 4)
f_ref = refVariables(round, 5)
g_ref = refVariables(round, 6)
h_ref = refVariables(round, 7)

a_res = resultVariables(round, 0)
b_res = resultVariables(round, 1)
c_res = resultVariables(round, 2)
d_res = resultVariables(round, 3)
e_res = resultVariables(round, 4)
f_res = resultVariables(round, 5)
g_res = resultVariables(round, 6)
h_res = resultVariables(round, 7)

' difference sum(s0, maj)
s0_maj_ref = Sha256.forceOperationInt64 +
(Me.RightRotate(a_ref, 2) Xor Me.RightRotate(a_ref, 13) Xor Me.RightRotate(a_ref, 22)) +
((a_ref And b_ref) Xor (a_ref And c_ref) Xor (b_ref And c_ref))

s0_maj_res = Sha256.forceOperationInt64 +
(Me.RightRotate(a_res, 2) Xor Me.RightRotate(a_res, 13) Xor Me.RightRotate(a_res, 22)) +
((a_res And b_res) Xor (a_res And c_res) Xor (b_res And c_res))

diff_s0_maj = s0_maj_res - s0_maj_ref

' difference d
diff_d = Sha256.forceOperationInt64 + d_res - d_ref

' word found
word = CType((diff_s0_maj - diff_d) And UInt32.MaxValue, UInt32)

'refWords(round) = word
refWords(round) = CType((Sha256.forceOperationUInt64 + refWords(round) + word) And UInt32.MaxValue, UInt32)

' get reference variables for words passed
' (we don't use the resulting hash)
refVariables = Nothing
Me.Encrypt(Me.initHash, refWords, refVariables)

resultVariables = Nothing
resultWords = Nothing
Me.Decrypt(variablesLastRound, refVariables, resultVariables, resultWords)

If debugVariables IsNot Nothing And debugWords IsNot Nothing Then
Me.OutputResult(resultVariables, resultWords, refVariables, refWords, debugVariables(0), debugWords(0))
End If
Next

Return resultWords

End Function

Private Sub Decrypt(variablesLastRound As UInt32(), refVariables As UInt32(,),
ByRef resultVariables As UInt32(,),
ByRef resultWords As UInt32())

Dim a, b, c, d, e, f, g, h As UInt32
Dim s1_ch_k_h_w_prev As Int64
Dim h_w_prev As Int64
Dim w_prev As Int64

Dim round As Integer
Dim variables(Me.rounds, 7) As UInt32
Dim words(Me.rounds - 1) As UInt32

a = variablesLastRound(0)
b = variablesLastRound(1)
c = variablesLastRound(2)
d = variablesLastRound(3)
e = variablesLastRound(4)
f = variablesLastRound(5)
g = variablesLastRound(6)
h = variablesLastRound(7)

variables(Me.rounds, 0) = a
variables(Me.rounds, 1) = b
variables(Me.rounds, 2) = c
variables(Me.rounds, 3) = d
variables(Me.rounds, 4) = e
variables(Me.rounds, 5) = f
variables(Me.rounds, 6) = g
variables(Me.rounds, 7) = h

For round = Me.rounds - 1 To 0 Step -1
' determine previous d
' --------------------

' current e = previous d + sum(previous s1, ch, k, h, w)
' so previous d = current e - sum(previous s1, ch, k, h, w)

' so we have to know sum(previous s1, ch, k, h, w) in order to determine previous d
' current a = sum(previous s1, ch, k, h, w) + sum(previous s0, maj)
' so sum(previous s1, ch, k, h, w) = current a - sum(previous s0, maj)
' we know current a, and we can calculate sum(previous s0, maj)

s1_ch_k_h_w_prev = a -
(Sha256.forceOperationInt64 +
(Me.RightRotate(b, 2) Xor Me.RightRotate(b, 13) Xor Me.RightRotate(b, 22)) +
((b And c) Xor (b And d) Xor (c And d))
)

' now we know sum(previous s1, ch, k, h, w) and so we can determine previous d

' determine previous h
' --------------------

' obviously there is no direct way to determine previous h,
' but we can however calculate the sum of previous h and w

' we already know the value of sum(previous s1, ch, k, h, w), which we will call x here
' x = previous s1 + previous ch + previous k + previous h + previous w
' so previous h + previous w = x - previous s1 - previous ch - previous k

' we can calculate previous s1 and previous ch and we know previous k

h_w_prev = s1_ch_k_h_w_prev -
(Me.RightRotate(f, 6) Xor Me.RightRotate(f, 11) Xor Me.RightRotate(f, 25)) -
((f And g) Xor ((Not f) And h)) -
Me.k(round)

' now we know the sum of previous h and w

' when we substract reference variable (previous) h from this sum,
' we get an intermediate value for previous w
' (and as a result we can calculate the intermediate value for previous h)

' ultimately, when we keep substracting reference variable (previous) h in each round,
' initial variables found for at least e, f, g, and h would equal initial reference variables

' from there on, we will have to adjust reference variables (reference words)
' in order for all variables found to comply with reference variables

w_prev = h_w_prev - refVariables(round, 7)

a = b
b = c
c = d

d = CType((e - s1_ch_k_h_w_prev) And UInt32.MaxValue, UInt32)

e = f
f = g
g = h

h = CType(h_w_prev - w_prev, UInt32)

variables(round, 0) = a
variables(round, 1) = b
variables(round, 2) = c
variables(round, 3) = d
variables(round, 4) = e
variables(round, 5) = f
variables(round, 6) = g
variables(round, 7) = h

words(round) = CType(w_prev And UInt32.MaxValue, UInt32)
Next

resultVariables = variables
resultWords = words

End Sub

End Class

' TEST FORM

Option Explicit On
Option Strict On

Public Class Form1

Private Sub btnTest_Click(sender As Object, e As EventArgs) Handles btnTest.Click

Dim sha As New Sha256
Dim debugWords As UInt32()() = Nothing
Dim debugVariables As UInt32()(,) = Nothing

Me.btnTest.Enabled = False
Try
Me.txtEncodeOutput.Text = sha.Encrypt(Me.txtEncodeInput.Text, debugWords, debugVariables)
Me.txtDecodeInput.Text = Me.txtEncodeOutput.Text

'Me.txtDecodeOutput.Text = sha.Decrypt(Me.txtEncodeOutput.Text, debugWords, debugVariables)
Me.txtDecodeOutput.Text = sha.Decrypt(Me.txtEncodeOutput.Text, Nothing, Nothing)

Finally
Me.btnTest.Enabled = True
End Try

End Sub

End Class

' FOR THE FORM DESIGNER FILE

<Global.Microsoft.VisualBasic.CompilerServices.DesignerGenerated()> _
Partial Class Form1
Inherits System.Windows.Forms.Form

'Form overrides dispose to clean up the component list.
<System.Diagnostics.DebuggerNonUserCode()> _
Protected Overrides Sub Dispose(ByVal disposing As Boolean)
Try
If disposing AndAlso components IsNot Nothing Then
components.Dispose()
End If
Finally
MyBase.Dispose(disposing)
End Try
End Sub

'Required by the Windows Form Designer
Private components As System.ComponentModel.IContainer

'NOTE: The following procedure is required by the Windows Form Designer
'It can be modified using the Windows Form Designer.
'Do not modify it using the code editor.
<System.Diagnostics.DebuggerStepThrough()> _
Private Sub InitializeComponent()
Me.btnTest = New System.Windows.Forms.Button()
Me.Label1 = New System.Windows.Forms.Label()
Me.Label2 = New System.Windows.Forms.Label()
Me.txtEncodeInput = New System.Windows.Forms.TextBox()
Me.txtEncodeOutput = New System.Windows.Forms.TextBox()
Me.grpEncode = New System.Windows.Forms.GroupBox()
Me.grpDecode = New System.Windows.Forms.GroupBox()
Me.txtDecodeOutput = New System.Windows.Forms.TextBox()
Me.Label3 = New System.Windows.Forms.Label()
Me.txtDecodeInput = New System.Windows.Forms.TextBox()
Me.Label4 = New System.Windows.Forms.Label()
Me.grpEncode.SuspendLayout()
Me.grpDecode.SuspendLayout()
Me.SuspendLayout()
'
'btnTest
'
Me.btnTest.Anchor = CType((System.Windows.Forms.AnchorStyles.Bottom Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles)
Me.btnTest.Location = New System.Drawing.Point(537, 374)
Me.btnTest.Margin = New System.Windows.Forms.Padding(2)
Me.btnTest.Name = "btnTest"
Me.btnTest.Size = New System.Drawing.Size(56, 19)
Me.btnTest.TabIndex = 0
Me.btnTest.Text = "Test"
Me.btnTest.UseVisualStyleBackColor = True
'
'Label1
'
Me.Label1.AutoSize = True
Me.Label1.Location = New System.Drawing.Point(5, 20)
Me.Label1.Margin = New System.Windows.Forms.Padding(2, 0, 2, 0)
Me.Label1.Name = "Label1"
Me.Label1.Size = New System.Drawing.Size(31, 13)
Me.Label1.TabIndex = 2
Me.Label1.Text = "Input"
'
'Label2
'
Me.Label2.AutoSize = True
Me.Label2.Location = New System.Drawing.Point(5, 49)
Me.Label2.Margin = New System.Windows.Forms.Padding(2, 0, 2, 0)
Me.Label2.Name = "Label2"
Me.Label2.Size = New System.Drawing.Size(39, 13)
Me.Label2.TabIndex = 3
Me.Label2.Text = "Output"
'
'txtEncodeInput
'
Me.txtEncodeInput.Anchor = CType(((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Left) _
Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles)
Me.txtEncodeInput.Location = New System.Drawing.Point(113, 17)
Me.txtEncodeInput.Margin = New System.Windows.Forms.Padding(2)
Me.txtEncodeInput.MaxLength = 31
Me.txtEncodeInput.Name = "txtEncodeInput"
Me.txtEncodeInput.Size = New System.Drawing.Size(462, 20)
Me.txtEncodeInput.TabIndex = 4
'
'txtEncodeOutput
'
Me.txtEncodeOutput.Anchor = CType(((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Left) _
Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles)
Me.txtEncodeOutput.Location = New System.Drawing.Point(113, 46)
Me.txtEncodeOutput.Margin = New System.Windows.Forms.Padding(2)
Me.txtEncodeOutput.Name = "txtEncodeOutput"
Me.txtEncodeOutput.ReadOnly = True
Me.txtEncodeOutput.Size = New System.Drawing.Size(462, 20)
Me.txtEncodeOutput.TabIndex = 5
'
'grpEncode
'
Me.grpEncode.Anchor = CType(((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Left) _
Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles)
Me.grpEncode.Controls.Add(Me.txtEncodeOutput)
Me.grpEncode.Controls.Add(Me.Label1)
Me.grpEncode.Controls.Add(Me.txtEncodeInput)
Me.grpEncode.Controls.Add(Me.Label2)
Me.grpEncode.Location = New System.Drawing.Point(12, 12)
Me.grpEncode.Name = "grpEncode"
Me.grpEncode.Size = New System.Drawing.Size(580, 78)
Me.grpEncode.TabIndex = 6
Me.grpEncode.TabStop = False
Me.grpEncode.Text = "Encode"
'
'grpDecode
'
Me.grpDecode.Anchor = CType(((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Left) _
Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles)
Me.grpDecode.Controls.Add(Me.txtDecodeOutput)
Me.grpDecode.Controls.Add(Me.Label3)
Me.grpDecode.Controls.Add(Me.txtDecodeInput)
Me.grpDecode.Controls.Add(Me.Label4)
Me.grpDecode.Location = New System.Drawing.Point(12, 96)
Me.grpDecode.Name = "grpDecode"
Me.grpDecode.Size = New System.Drawing.Size(580, 78)
Me.grpDecode.TabIndex = 7
Me.grpDecode.TabStop = False
Me.grpDecode.Text = "Decode"
'
'txtDecodeOutput
'
Me.txtDecodeOutput.Anchor = CType(((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Left) _
Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles)
Me.txtDecodeOutput.Location = New System.Drawing.Point(113, 46)
Me.txtDecodeOutput.Margin = New System.Windows.Forms.Padding(2)
Me.txtDecodeOutput.Name = "txtDecodeOutput"
Me.txtDecodeOutput.ReadOnly = True
Me.txtDecodeOutput.Size = New System.Drawing.Size(462, 20)
Me.txtDecodeOutput.TabIndex = 5
'
'Label3
'
Me.Label3.AutoSize = True
Me.Label3.Location = New System.Drawing.Point(5, 20)
Me.Label3.Margin = New System.Windows.Forms.Padding(2, 0, 2, 0)
Me.Label3.Name = "Label3"
Me.Label3.Size = New System.Drawing.Size(31, 13)
Me.Label3.TabIndex = 2
Me.Label3.Text = "Input"
'
'txtDecodeInput
'
Me.txtDecodeInput.Anchor = CType(((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Left) _
Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles)
Me.txtDecodeInput.Location = New System.Drawing.Point(113, 17)
Me.txtDecodeInput.Margin = New System.Windows.Forms.Padding(2)
Me.txtDecodeInput.Name = "txtDecodeInput"
Me.txtDecodeInput.ReadOnly = True
Me.txtDecodeInput.Size = New System.Drawing.Size(462, 20)
Me.txtDecodeInput.TabIndex = 4
'
'Label4
'
Me.Label4.AutoSize = True
Me.Label4.Location = New System.Drawing.Point(5, 49)
Me.Label4.Margin = New System.Windows.Forms.Padding(2, 0, 2, 0)
Me.Label4.Name = "Label4"
Me.Label4.Size = New System.Drawing.Size(39, 13)
Me.Label4.TabIndex = 3
Me.Label4.Text = "Output"
'
'Form1
'
Me.AcceptButton = Me.btnTest
Me.AutoScaleDimensions = New System.Drawing.SizeF(6.0!, 13.0!)
Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font
Me.ClientSize = New System.Drawing.Size(604, 404)
Me.Controls.Add(Me.grpDecode)
Me.Controls.Add(Me.grpEncode)
Me.Controls.Add(Me.btnTest)
Me.Margin = New System.Windows.Forms.Padding(2)
Me.Name = "Form1"
Me.Text = "Form1"
Me.grpEncode.ResumeLayout(False)
Me.grpEncode.PerformLayout()
Me.grpDecode.ResumeLayout(False)
Me.grpDecode.PerformLayout()
Me.ResumeLayout(False)

End Sub
Friend WithEvents btnTest As System.Windows.Forms.Button
Friend WithEvents Label1 As System.Windows.Forms.Label
Friend WithEvents Label2 As System.Windows.Forms.Label
Friend WithEvents txtEncodeInput As System.Windows.Forms.TextBox
Friend WithEvents txtEncodeOutput As System.Windows.Forms.TextBox
Friend WithEvents grpEncode As GroupBox
Friend WithEvents grpDecode As GroupBox
Friend WithEvents txtDecodeOutput As TextBox
Friend WithEvents Label3 As Label
Friend WithEvents txtDecodeInput As TextBox
Friend WithEvents Label4 As Label
End Class
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://www.metzdowd.com/pipermail/cryptography/attachments/20231106/d5d1ad4e/attachment-0001.htm>


More information about the cryptography mailing list