tranchemontaigne
Technical User
I am trying to create a simple character offset encryption function to encrypt specific data fields within a query results output.
Though my method appears to work for encrypting data (provided the field is not null), but unfortunately the decrypt function is not working correctly. When viewing the code through the debugger, the watch window shows Windows ANSI character code being correctly translated into alphabet characters, yet the query displays only character codes (not aplhabet letters).
Here's my code, running in MS Access 2000.
Option Compare Database
Option Explicit
Public Function Encrypt(strString As String) As String
'/////////////////////////////////////////////////////////////////////
'// DESCRIPTION:
'// Function takes a character string and encrypts it by
'// offsetting each character of the string by an integer value.
'//
'// In the event that the character string is equal to "OTH000"
'// the string is not encrypted.
'//
'/////////////////////////////////////////////////////////////////////
'// INPUTS:
'// strString -String expression to encrypt
'//
'/////////////////////////////////////////////////////////////////////
'// OUTPUT:
'// Encrypt -Encrypted string
'//
'/////////////////////////////////////////////////////////////////////
'declare variables
Dim intStrLength As Integer
Dim intOffset As Integer
Dim intCounter As Integer
Dim strLetter As String
Dim intLetterPlain As Integer
Dim intLetterEncrypted As Integer
Dim strLetterEncrypted As String
Dim strEncryptedString As String
'set character offset value
intOffset = 9
'calculate length of string to encrypt
intStrLength = Len(strString)
'test to see if strString is "OTH000"
If strEncryptedString = "OTH000" Then
strString = "OTH000"
'NONE OF THESE 2 TESTS FOR NULL VALUES WORK
'test#1 to see if strString is null
'elseIf strString = "" Then
' intStrLength = 0
'End If
'If intStrLength > 0 Then
' intStrLength = intStrLength
'Else
' intStrLength = 0
'End If
'test#2 to see if strString is null
'elseIf intStrLength < 1 Then
' strEncryptedString = "2"
Else
'encrypt string one letter at a time
'build encrypted string by concatenating one letter at a time
'stop at end of string
For intCounter = 1 To intStrLength
strLetter = Mid(strString, intCounter, 1)
intLetterPlain = Asc(strLetter)
intLetterEncrypted = intLetterPlain + intOffset
strLetterEncrypted = Chr(intLetterEncrypted)
strEncryptedString = strEncryptedString & strLetterEncrypted
Next intCounter
End If
'return encrypted string
Encrypt = strEncryptedString
End Function
Public Function Decrypt(strEncryptedString As String) As String
'/////////////////////////////////////////////////////////////////////
'// DESCRIPTION:
'// Function takes a the chatacter string previously encrypted by
'// the Encrypt() function, and returns the original plain text
'// character string values.
'//
'// In the event that the character string is equal to "OTH000"
'// the string is not decrypted.
'//
'/////////////////////////////////////////////////////////////////////
'// INPUTS:
'// strEncryptedString -Encrypted string expression
'//
'/////////////////////////////////////////////////////////////////////
'// OUTPUT:
'// Decrypt -Decrypted string
'//
'/////////////////////////////////////////////////////////////////////
'declare variables
Dim intStrLength As Integer
Dim intOffset As Integer
Dim intCounter As Integer
Dim strLetter As String
Dim intLetterPlain As Integer
Dim strLetterPlain 'As String
Dim intLetterEncrypted As Integer
Dim strLetterEncrypted As String
Dim strString As String
'set character offset value
intOffset = 9
'calculate length of string to encrypt
intStrLength = Len(strEncryptedString)
'test to see if strString is "OTH000"
If strEncryptedString = "OTH000" Then
strString = "OTH000"
'NONE OF THESE 2 TESTS FOR NULL VALUES WORK
'test#1 to see if strString is null
'elseIf strString = "" Then
' intStrLength = 0
'End If
'If intStrLength > 0 Then
' intStrLength = intStrLength
'Else
' intStrLength = 0
'End If
'test#2 to see if strString is null
'elseIf intStrLength < 1 Then
' strEncryptedString = "2"
Else
'encrypt string one letter at a time
'build encrypted string by concatenating one letter at a time
'stop at end of string
For intCounter = 1 To 2 'intStrLength
strLetterEncrypted = Mid(strEncryptedString, intCounter, 1)
intLetterEncrypted = Asc(strLetterEncrypted)
intLetterPlain = intLetterEncrypted - intOffset
strLetterPlain = Chr(intLetterPlain)
strString = strString & intLetterPlain
Next intCounter
End If
'return encrypted string
Decrypt = strString
End Function
As a secondary question, when I use these functions in a query, I receive "Error#" in the query output whenever the field I am trying to encrypt contains a null value within a record. I can work around this by building an
IIF( [test field] is not null, encrypt([test field]),
null)
expression that tests for null, and only calls the encrypt function if the field value is not null, but ideally I would like to have the function know how to handle a null value natively. I would like to set a default argument value within the function prototype, but do not know how to do this with VBA. Alternatively, is it possible for overload functions in Access VBA, so that the same function name could be used, but the compiler would know wiich version to call based upon the function argument supplied?
Thanks in advance for your help.
Though my method appears to work for encrypting data (provided the field is not null), but unfortunately the decrypt function is not working correctly. When viewing the code through the debugger, the watch window shows Windows ANSI character code being correctly translated into alphabet characters, yet the query displays only character codes (not aplhabet letters).
Here's my code, running in MS Access 2000.
Option Compare Database
Option Explicit
Public Function Encrypt(strString As String) As String
'/////////////////////////////////////////////////////////////////////
'// DESCRIPTION:
'// Function takes a character string and encrypts it by
'// offsetting each character of the string by an integer value.
'//
'// In the event that the character string is equal to "OTH000"
'// the string is not encrypted.
'//
'/////////////////////////////////////////////////////////////////////
'// INPUTS:
'// strString -String expression to encrypt
'//
'/////////////////////////////////////////////////////////////////////
'// OUTPUT:
'// Encrypt -Encrypted string
'//
'/////////////////////////////////////////////////////////////////////
'declare variables
Dim intStrLength As Integer
Dim intOffset As Integer
Dim intCounter As Integer
Dim strLetter As String
Dim intLetterPlain As Integer
Dim intLetterEncrypted As Integer
Dim strLetterEncrypted As String
Dim strEncryptedString As String
'set character offset value
intOffset = 9
'calculate length of string to encrypt
intStrLength = Len(strString)
'test to see if strString is "OTH000"
If strEncryptedString = "OTH000" Then
strString = "OTH000"
'NONE OF THESE 2 TESTS FOR NULL VALUES WORK
'test#1 to see if strString is null
'elseIf strString = "" Then
' intStrLength = 0
'End If
'If intStrLength > 0 Then
' intStrLength = intStrLength
'Else
' intStrLength = 0
'End If
'test#2 to see if strString is null
'elseIf intStrLength < 1 Then
' strEncryptedString = "2"
Else
'encrypt string one letter at a time
'build encrypted string by concatenating one letter at a time
'stop at end of string
For intCounter = 1 To intStrLength
strLetter = Mid(strString, intCounter, 1)
intLetterPlain = Asc(strLetter)
intLetterEncrypted = intLetterPlain + intOffset
strLetterEncrypted = Chr(intLetterEncrypted)
strEncryptedString = strEncryptedString & strLetterEncrypted
Next intCounter
End If
'return encrypted string
Encrypt = strEncryptedString
End Function
Public Function Decrypt(strEncryptedString As String) As String
'/////////////////////////////////////////////////////////////////////
'// DESCRIPTION:
'// Function takes a the chatacter string previously encrypted by
'// the Encrypt() function, and returns the original plain text
'// character string values.
'//
'// In the event that the character string is equal to "OTH000"
'// the string is not decrypted.
'//
'/////////////////////////////////////////////////////////////////////
'// INPUTS:
'// strEncryptedString -Encrypted string expression
'//
'/////////////////////////////////////////////////////////////////////
'// OUTPUT:
'// Decrypt -Decrypted string
'//
'/////////////////////////////////////////////////////////////////////
'declare variables
Dim intStrLength As Integer
Dim intOffset As Integer
Dim intCounter As Integer
Dim strLetter As String
Dim intLetterPlain As Integer
Dim strLetterPlain 'As String
Dim intLetterEncrypted As Integer
Dim strLetterEncrypted As String
Dim strString As String
'set character offset value
intOffset = 9
'calculate length of string to encrypt
intStrLength = Len(strEncryptedString)
'test to see if strString is "OTH000"
If strEncryptedString = "OTH000" Then
strString = "OTH000"
'NONE OF THESE 2 TESTS FOR NULL VALUES WORK
'test#1 to see if strString is null
'elseIf strString = "" Then
' intStrLength = 0
'End If
'If intStrLength > 0 Then
' intStrLength = intStrLength
'Else
' intStrLength = 0
'End If
'test#2 to see if strString is null
'elseIf intStrLength < 1 Then
' strEncryptedString = "2"
Else
'encrypt string one letter at a time
'build encrypted string by concatenating one letter at a time
'stop at end of string
For intCounter = 1 To 2 'intStrLength
strLetterEncrypted = Mid(strEncryptedString, intCounter, 1)
intLetterEncrypted = Asc(strLetterEncrypted)
intLetterPlain = intLetterEncrypted - intOffset
strLetterPlain = Chr(intLetterPlain)
strString = strString & intLetterPlain
Next intCounter
End If
'return encrypted string
Decrypt = strString
End Function
As a secondary question, when I use these functions in a query, I receive "Error#" in the query output whenever the field I am trying to encrypt contains a null value within a record. I can work around this by building an
IIF( [test field] is not null, encrypt([test field]),
null)
expression that tests for null, and only calls the encrypt function if the field value is not null, but ideally I would like to have the function know how to handle a null value natively. I would like to set a default argument value within the function prototype, but do not know how to do this with VBA. Alternatively, is it possible for overload functions in Access VBA, so that the same function name could be used, but the compiler would know wiich version to call based upon the function argument supplied?
Thanks in advance for your help.