I would like to have a macro user defined function to decrypt a column of string in excel that was encrypted with AES-256. Anyone can help me with this?
I have tried googling and some code but somehow it doesn’t work.
‘ Define the AES key and other constants
Const KEY_SIZE As Integer = 32 ‘ AES-256 key size (32 bytes)
Const BLOCK_SIZE As Integer = 16 ‘ AES block size (16 bytes)
‘ Dummy placeholder functions for AES operations
‘ You’ll need to implement these functions according to AES standards
Function KeyExpansion(key As String) As Variant
‘ Implement key expansion for AES-256 here
End Function
Function InvSubBytes(state As Variant) As Variant
‘ Implement Inverse SubBytes operation here
End Function
Function InvShiftRows(state As Variant) As Variant
‘ Implement Inverse ShiftRows operation here
End Function
Function InvMixColumns(state As Variant) As Variant
‘ Implement Inverse MixColumns operation here
End Function
Function AddRoundKey(state As Variant, roundKey As Variant) As Variant
‘ Implement AddRoundKey operation here
End Function
Function AES256Decrypt(cipherText As String, key As String) As String
Dim state(BLOCK_SIZE – 1) As Byte
Dim roundKeys() As Byte
Dim decryptedText As String
' Convert cipherText and key from hex string to byte array
' Add your conversion code here
' Key expansion
roundKeys = KeyExpansion(key)
' Decryption process
' Implement AES decryption rounds here
' This involves Inverse SubBytes, Inverse ShiftRows, Inverse MixColumns, and AddRoundKey
' Convert the decrypted state back to a string
' Add your conversion code here
AES256Decrypt = decryptedText
End Function
Sub DecryptCell(cellAddress As String, key As String)
Dim ws As Worksheet
Dim encryptedText As String
Dim decryptedText As String
Set ws = ActiveSheet ' Or specify your worksheet
' Get the encrypted value from the cell
encryptedText = ws.Range(cellAddress).Value
' Decrypt the cell value
decryptedText = AES256Decrypt(encryptedText, key)
' Update the cell with the decrypted value
ws.Range(cellAddress).Value = decryptedText
End Sub
Sub TestDecryption()
Dim key As String
key = “A0B1C2D3E4F50617283940566778899A0B1C2D3E4F50617283940566778899A0B1C2D3E4F506172” ‘ Replace with your actual 256-bit key in hexadecimal format
' Decrypt cell A1 on the active sheet
DecryptCell "A1", key
End Sub
Ying Hui is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.