473,396 Members | 1,996 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Solved: Masking credit card numbers in a mixed field

I am working on a project to look in a couple of fields for 16 consecutive numbers, I am masking credit card numbers I can get this to work with the following code, for fields where it begins or ends with 16 consecutive numbers. The problem I am having is a field with alpha on either side, or a numeric string longer then 16 digits. I guess what I need is something to look for 16 digits in a field, and replace place 7-12 with *’s. VBA doesn’t like wild cards in any of the string functions. Any help would be great.

Andrew

Expand|Select|Wrap|Line Numbers
  1. Public Function fnMaskCCNumbers(Item As String, fld As String)
  2.  
  3. Dim str As String
  4. Dim rst As Recordset
  5. Dim str1 As String
  6. Dim str2 As String
  7. Dim strlen As Integer
  8.  
  9. Set rst = CurrentDb.OpenRecordset(Item, dbOpenDynaset)
  10.  
  11.  
  12. rst.MoveFirst
  13.  
  14. Do Until rst.EOF = True
  15.  
  16. If rst(fld) Like "*################" Or rst(fld) Like "################*" Or rst(fld) Like "*################" Then
  17.  
  18. str = rst(fld)
  19.     'starts with CC rst(fld) Like "################*"
  20.     If IsNumeric(Left(str, 16)) = True Then
  21.     str1 = Left(str, 6) & Chr(42) & Chr(42) & Chr(42) & Chr(42) & Chr(42) & Chr(42) & Mid(str, 12, 37)
  22.     Debug.Print str1
  23.     rst.Edit
  24.     rst(fld) = str1
  25.     rst.Update
  26.  
  27.     'ends with CC rst(fld) Like "*################"
  28.     ElseIf IsNumeric(Right(str, 16)) = True Then
  29.     strlen = Len(str)
  30.     str2 = Left(str, (strlen - 10)) & Chr(42) & Chr(42) & Chr(42) & Chr(42) & Chr(42) & Chr(42) & Right(str, 4)
  31.     Debug.Print str2
  32.     rst.Edit
  33.     rst(fld) = str2
  34.     rst.Update
  35.  
  36.  
  37. End If
  38. End If
  39. rst.MoveNext
  40. Loop
  41.  
  42. rst.Close
  43.  
  44.  
  45.  
  46. End Function
  47.  
Apr 11 '11 #1
3 4188
gershwyn
122 100+
I'm assuming that the number is surrounded by spaces on either side. This function loops through, checking the characters between each pair of spaces in the string.
Expand|Select|Wrap|Line Numbers
  1. Public Function MaskDigits(StringValue As String)
  2.   startPos = 0
  3.   Do While startPos < Len(StringValue)
  4.     endPos = InStr(startPos + 1, StringValue, " ")
  5.     If endPos = 0 Then endPos = Len(StringValue) + 1
  6.     If (endPos - startPos - 1) = 16 Then
  7.       If IsNumeric(Mid(StringValue, startPos + 1, 16)) Then
  8.         StringValue = Left(StringValue, startPos + 6) & String(6, Chr(42)) & Right(StringValue, Len(StringValue) - startPos - 12)
  9.         Exit Do
  10.       End If
  11.     End If
  12.     startPos = endPos
  13.   Loop
  14.   MaskDigits = StringValue
  15. End Function
This has the potential to give a false positive, on the off chance your data contains 16 consecutive characters that can be interpreted as a number, but are not entirely made of digits (e.g., "123,456,789.1234") If that is a real possibility, you could add another check to make sure you're dealing with 16 straight digits.
Apr 11 '11 #2
jimatqsi
1,271 Expert 1GB
Andrew,
Here is a recursive routine to do what you want. I've included a little test routine that I used to check it out.

Expand|Select|Wrap|Line Numbers
  1. Public Function Find16Digits(ByVal strField As String, intLevel) As String
  2. ' Incoming parameter strField is a string of unknown length
  3. ' and how deep we are nested in Find16Digits calls
  4.  
  5.    On Error GoTo Find16Digits_Error
  6.  
  7.     Dim intRetry As Integer
  8.  
  9.     Find16Digits = ""
  10.  
  11.     Do While intRetry >= 0
  12.         If Len(strField) < 16 - intLevel Then ' keep trying until there is not enough left to try
  13.             Find16Digits = ""
  14.             Exit Function ' cannot possibly work without 16 chars
  15.         End If
  16.  
  17.         If IsNumeric(Left(strField, 1)) Then ' check 1st char to see if it is a number
  18.             If intLevel >= 15 Then
  19.                 Find16Digits = strField   ' the field we started with is okay
  20.             Else
  21.                 Find16Digits = Find16Digits(Mid(strField, 2), intLevel + 1) ' recursive call for all chars to the right, bumping up intLevel
  22.                 If Find16Digits <> "" Then Find16Digits = strField
  23.             End If
  24.         Else  ' as soon as we find a non-numeric, return with bad news
  25.             Find16Digits = ""
  26.         End If
  27.  
  28.         If Len(Find16Digits) < (16 - intLevel) Then ' if we failed
  29.             If intLevel = 0 Then   ' If we're at the lowest level, STRIP OFF 1 CHAR AND TRY AGAIN
  30.                 intRetry = intRetry + 1
  31.                 strField = Mid(strField, 2) ' eliminate the first character of the field, try again
  32.             Else
  33.                 ' If we're not at the lowest level, go back and try again
  34.                 Exit Function
  35.             End If
  36.         Else
  37.             Find16Digits = Left(Find16Digits, 16) ' cut off any extra chars
  38.             intRetry = -1
  39.         End If
  40.  
  41.     Loop
  42.  
  43.    On Error GoTo 0
  44.    Exit Function
  45.  
  46. Find16Digits_Error:
  47.  
  48.     MsgBox "Error " & Err.Number & " (" & Err.Description & ") in procedure Find16Digits of VBA Document Form_ComponentBuilder_frm"
  49.     Resume Next
  50.  
  51. End Function
  52.  
  53. Public Sub GetCCNumber()
  54.  
  55.     Dim strTestCC As String
  56.     Dim strResult As String
  57.  
  58.    On Error GoTo GetCCNumber_Error
  59.  
  60.     strTestCC = "1234567890123456"
  61.     strResult = Find16Digits(strTestCC, 0)
  62.     MsgBox "The CC Number in " & strTestCC & " is " & strResult
  63.  
  64.     strTestCC = "CR154A1234567890123456"
  65.     strResult = Find16Digits(strTestCC, 0)
  66.     MsgBox "The CC Number in " & strTestCC & " is " & strResult
  67.  
  68.     strTestCC = "1234569923456aa44a123456789012345689aa"
  69.     strResult = Find16Digits(strTestCC, 0)
  70.     MsgBox "The CC Number in " & strTestCC & " is " & strResult
  71.  
  72.    On Error GoTo 0
  73.    Exit Sub
  74.  
  75. GetCCNumber_Error:
  76.  
  77.     MsgBox "Error " & Err.Number & " (" & Err.Description & ") in procedure GetCCNumber of VBA Document Form_ComponentBuilder_frm"
  78.     Resume Next
  79.  
  80. End Sub
  81.  
My routine returns the 16 character string. You could present 16 asterisks and the right-most 4 characters in your UI.

Jim
Apr 11 '11 #3
Thanks for the reply's I did get it to work with a REGex Function, It was very frustrating. It works realativly fast, as I was able to mask the numbers in a few history talbes, with ~300K records each, in only a few mins. Here is what I used. Not to bad for 50 some lines of code, I just passed in my recordset, and the targeted field.

Expand|Select|Wrap|Line Numbers
  1. Public Function fnMaskCCNumbers(Item As String, fld As String)
  2.  
  3. Dim str As String
  4. Dim rst As Recordset
  5. Dim str2 As String
  6. Dim str3 As String
  7. Dim str4 As String
  8.  
  9. Set rst = CurrentDb.OpenRecordset(Item, dbOpenDynaset)
  10.  
  11. rst.MoveFirst
  12.  
  13. Do Until rst.EOF = True
  14. 'If rst(fld) Like "*################*" Or rst(fld) Like "################*" Or rst(fld) Like "*################" And rst(fld)not Like "*#################*" Then
  15.  
  16. If rst(fld) Like "*################*" And Not rst(fld) Like "*#################*" Then
  17.  
  18. str = rst(fld)
  19.  
  20. str2 = RE16(str)
  21.  
  22. 'Debug.Print str2
  23. str3 = Left(str2, 6) & Chr(42) & Chr(42) & Chr(42) & Chr(42) & Chr(42) & Chr(42) & Right(str2, 4)
  24. 'Debug.Print str3
  25. str4 = Replace(rst(fld), str2, str3)
  26. 'Debug.Print str4
  27.  
  28.     rst.Edit
  29.     rst(fld) = str4
  30.     rst.Update
  31.  
  32. End If
  33.  
  34. rst.MoveNext
  35. Loop
  36.  
  37. rst.Close
  38.  
  39. End Function
  40.  
  41. Public Function RE16(strData As String) As String
  42.     Dim RE As Object, REMatches As Object
  43.  
  44.     Set RE = CreateObject("vbscript.regexp")
  45.     With RE
  46.         .MultiLine = False
  47.         .Global = False
  48.         .IgnoreCase = True
  49.         .Pattern = "[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]"
  50.     End With
  51.  
  52.     Set REMatches = RE.Execute(strData)
  53.     RE16 = REMatches(0)
  54.  
  55. End Function
  56.  
Apr 12 '11 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: revjjjames | last post by:
Hello, can someone please recommended a online credit card processor that is PHP friendly? I am seeking one that could be built into a hidden-frame, and process a transaction seamlessly this way. ...
1
by: Amy B | last post by:
Hi, A co-worker is creating a vb.net web application which uses the CryptKeeper.dll to encrypt credit card numbers into a sql database. It is my job to pull these encrypted numbers and use the...
7
by: gj | last post by:
I have an application in Access 97 I will be rewriting in the latest version of Access in 6 months. In the meantime, does anyone know of an ActiveX control I can add into an Access 97 form to...
4
by: gl | last post by:
I have just started a project that's going to do very heavy credit card processing through asp.net and i had some questions. I've never really done any cc processing through code and I wasn't sure...
5
by: John | last post by:
Hi, I've always had the opinion that you don't store credit card numbers on a hosted website database. But it has occurred to me, that perhaps I am over reacting, and encrypted CC info may be...
11
by: Paul Furman | last post by:
I'm setting up credit card payment through authorize.net and they have the option to send a POST string back to my site once complete. I'm not sure how to proceed. They don't have much to read...
1
by: Salimunnisaa | last post by:
Dear members, I have encrypted credit card numbers using Rinjdael algorithm and stored them in database. How will I decrypt them and how will mention the key and the Initialization Vector...
6
by: samatair | last post by:
I need to create a form which accepts credit card numbers and mail the details with the credit card number to my client. I came to know that using SSL would make the form HTTPS and make it secure....
4
code green
by: code green | last post by:
Data Protection etc.... shouldn't be there. How do I find credit card numbers buried amongst any length of text. Googling finds credit card validation claims, which are too complicated. I need...
1
by: janakiram dabbara | last post by:
hi, i have a scenario,i need to genarate random random credit card numbers like first i want to enter only 1st 4 digits,last 4 digits,5th to 8th and 9th to 12 and remaining numbers are...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.