简体   繁体   中英

Find Credit Card Number in XML?

I have a Credit Card Number in an XML Message and I want to use a regex to find the Credit Card Number and encrypt it. All numbers are 16 characters long and they are not always in the same place in the xml, so basically I think I just need to find a string that is 16 characters and decrypt it, but I am not sure how.

Example:

<Message>
<PaymentInfo>
<CCNumber>333333333333333</CCNumber>
</PaymentInfo>
</Message>

Another way may be:

<Message>
<CardInfo>
<CreditNumber>333333333333333</CreditNumber>
</CardInfo>
</Message>

Would this solution work, it is in VB though because the original code was VB.

 Public Shared Function EncryptXmlLog(xml As String) As String
 'Get possible credit card numbers (13 - 16 characters)

 Dim creditCardNumbers As MatchCollection = Matches(xml, "\b\d{13,16}\b")

 For Each creditCardNumber As RegularExpressions.Match In creditCardNumbers
     xml = xml.Replace(creditCardNumber.Value, Encrypt(creditCardNumber.Value))
 Next

 Return xml
 End Function

(\\d{16}|\\d{15}|\\d{13}) will match any digit sets of 16, 15, or 13

However I must know why you don't just commonize your XML doc before parsing. If there are multiple docs, you should have a conditional parser of some kind.

Try:

   var match = Regex.Match(xmlStrin, @">(\d{16})<"); 
   var number = match.Groups[1].Value;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM