0

Im trying to read a key and identifier from a .txt-file with the following content:

Wiederherstellungsschlüssel für die BitLocker-Laufwerkverschlüsselung 

Um zu überprüfen, ob es sich um den richtigen Wiederherstellungsschlüssel handelt, vergleichen Sie den Beginn des folgenden Bezeichners mit dem auf dem PC angezeigten Bezeichnerwert.

Bezeichner:

    F85E6660-52A8-43D9-B66Z-F61246666666

Falls der obige Bezeichner mit dem auf dem PC angezeigten Bezeichner übereinstimmt, sollten Sie den folgenden Schlüssel zum Entsperren des Laufwerks verwenden.

Wiederherstellungsschlüssel:

    222222-333333-444444-555555-666666-777777-888888-999999

Falls der obige Bezeichner nicht mit dem auf dem PC angezeigten Bezeichner übereinstimmt, handelt es sich nicht um den richtigen Schlüssel zum Entsperren des Laufwerks.
Versuchen Sie es mit einem anderen Wiederherstellungsschlüssel, oder suchen Sie unter "https://go.microsoft.com/fwlink/?LinkID=260589" nach weiteren Informationen.

While using the regex:

identifier_pattern = r"[A-Z0-9]{8}-[A-Z0-9]{4}-[A-Z0-9]{4}-[A-Z0-9]{4}-[A-Z0-9]{12}"
key_pattern = r"[0-9]{6}-[0-9]{6}-[0-9]{6}-[0-9]{6}-[0-9]{6}-[0-9]{6}-[0-9]{6}-[0-9]{6}"

It works when I save the text in multi-line or single-line strings but not when I read the file. I tried read() and iterating the input line by line or deleting linebreaks and tabs but it doesn't work either.

#Example use
import re

identifier_pattern = r"[A-Z0-9]{8}-[A-Z0-9]{4}-[A-Z0-9]{4}-[A-Z0-9]{4}-[A-Z0-9]{12}"

single_line = " F85E6660-52A8-43D9-B66Z-F61246666666"
# Works
print(re.search(identifier_pattern,single_line))

# Doesn't work
with open("testKey.txt") as file:
    for line in file:
        line_clear = line.replace('\r',' ').replace('\n' , ' ').replace('\t' , ' ')
        match = re.search(identifier_pattern,line_clear)
        print("match: ", match , "line: ",line_clear)
# Doesn't work
def extract_bitlocker_info(file_path):
    with open(file_path, 'r') as file:
        content = file.read()

    identifier_pattern = r"[A-Z0-9]{8}-[A-Z0-9]{4}-[A-Z0-9]{4}-[A-Z0-9]{4}-[A-Z0-9]{12}"
    identifier = re.search(identifier_pattern, content)

    recovery_key_pattern = r"[0-9]{6}-[0-9]{6}-[0-9]{6}-[0-9]{6}-[0-9]{6}-[0-9]{6}-[0-9]{6}-[0-9]{6}"
    recovery_key = re.search(recovery_key_pattern, content)

    return identifier, recovery_key

file_path = "testKey.txt"
identifier, recovery_key = extract_bitlocker_info(file_path)

print("Identifier:", identifier)
print("Recovery Key:", recovery_key)

What is the reason that the regular expressions do not work with input from .txt-files?

EDIT: By "Does not work" I mean that re.search() returns None.

3
  • 1
    Please clarify what you mean by "does not work". What behavior do you observe? What is printed? Do you actually print the output of the extract_bitlocker_info function? Are you sure there is a TAB before the pattern? Try with recovery_key_pattern = r"\b\d{6}(?:-\d{6}){7}\b" Commented Oct 10, 2024 at 17:35
  • 3
    Running your code I do get a match for the F8... line. Please be clear what "doesn't work". Commented Oct 10, 2024 at 17:39
  • 1
    Just debug and see the exact value that line_clear has. Should be easy.
    – trincot
    Commented Oct 10, 2024 at 17:45

2 Answers 2

1

Not sure why you're replacing various characters in the input as they don't form part of either of the patterns you're interested in.

Just read the entire file content then carry out discrete searches for the two patterns of interest.

import re

IDENTIFIER = re.compile(r"[A-Z0-9]{8}-[A-Z0-9]{4}-[A-Z0-9]{4}-[A-Z0-9]{4}-[A-Z0-9]{12}")
KEY = re.compile(r"[0-9]{6}-[0-9]{6}-[0-9]{6}-[0-9]{6}-[0-9]{6}-[0-9]{6}-[0-9]{6}-[0-9]{6}")

def search(p: re.Pattern, b:str) -> str|None:
    m = p.search(b)
    return m.group(0) if m else None

with open("testKey.txt") as data:
    buffer = data.read()
    for p in IDENTIFIER, KEY:
        print(search(p, buffer))

Output:

F85E6660-52A8-43D9-B66Z-F61246666666
222222-333333-444444-555555-666666-777777-888888-999999
2
  • Thank you for your reply, @SIGHUP. Which IDE are you using? I am using VS Code. What version of python are you using? Mine is 3.12.14. Unfortunately, my output is None for both. Commented Oct 11, 2024 at 13:26
  • @Simplicissimus Python 3.13.0, VSCode 1.94.1 on macOS 15.0.1. The file I constructed was a copy'n'paste of the data shown in your question Commented Oct 11, 2024 at 13:32
-1

There was a \t in the regex which is not present in the string I guess.

import re

file = """
Wiederherstellungsschlüssel für die BitLocker-Laufwerkverschlüsselung 

Um zu überprüfen, ob es sich um den richtigen Wiederherstellungsschlüssel handelt, vergleichen Sie den Beginn des folgenden Bezeichners mit dem auf dem PC angezeigten Bezeichnerwert.

Bezeichner:

    X12Y3456-78B9-01C2-D34E-F56789012345

Falls der obige Bezeichner mit dem auf dem PC angezeigten Bezeichner übereinstimmt, sollten Sie den folgenden Schlüssel zum Entsperren des Laufwerks verwenden.

Wiederherstellungsschlüssel:

    222222-333333-444444-555555-666666-777777-888888-999999

Falls der obige Bezeichner nicht mit dem auf dem PC angezeigten Bezeichner übereinstimmt, handelt es sich nicht um den richtigen Schlüssel zum Entsperren des Laufwerks.
Versuchen Sie es mit einem anderen Wiederherstellungsschlüssel, oder suchen Sie unter "https://go.microsoft.com/fwlink/?LinkID=260589" nach weiteren Informationen.
"""

identifier_pattern = r"[A-Z0-9]{8}-[A-Z0-9]{4}-[A-Z0-9]{4}-[A-Z0-9]{4}-[A-Z0-9]{12}"
recovery_key_pattern = r"[0-9]{6}-[0-9]{6}-[0-9]{6}-[0-9]{6}-[0-9]{6}-[0-9]{6}-[0-9]{6}-[0-9]{6}"
print(re.findall(identifier_pattern, file)[0])
print(re.findall(recovery_key_pattern, file)[0])
7
  • The tab the recovery_key_pattern was a mistake, all functions may use the same regex. But your answer is using a multi-line string and not an input-file. So it doesn't answer my question at all. Commented Oct 10, 2024 at 18:05
  • The only thing you need to do now is to load your file into a variable called file. How am I supposed to load a file if you don't provide a file? :D
    – Ohumeronen
    Commented Oct 10, 2024 at 18:08
  • The file content is literally the first thing of the question. I already did just that, that's why I'm confused why it isn't working like with a regular string. I am using: with open(file_path, 'r') as file: content = file.read() Commented Oct 10, 2024 at 18:18
  • You do not call the function extract_bitlocker_info so this point is never reached.
    – Ohumeronen
    Commented Oct 10, 2024 at 18:20
  • 1
    I saved the content to a file and ran your code. I get the correct results. One match for Identifier and one match for Recovery Key. Sorry I cannot reproduce the error. Maybe specific to the python version. Mine is Python 3.10.9.
    – Ohumeronen
    Commented Oct 10, 2024 at 18:38

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.