---
slug: "find-bitlocker-file-like-grep-r-in-python"
title: "BitLocker の復号化キーのファイルは utf-16のため grep -R で検索できないのでスクリプトで検索する"
description: "OrangePi Zero 2W に Web サイネージ用の Chromium kiosk を構築する手順。OS インストール、Wayland 設定、自動起動まで。"
url: "https://www.ytyng.com/blog/find-bitlocker-file-like-grep-r-in-python"
publish_date: "2023-05-14T03:09:02Z"
created: "2023-05-14T03:09:02Z"
updated: "2026-05-11T13:21:48.924Z"
categories: []
keywords: ""
featured_image_url: "https://media.ytyng.com/resize/20250705/22583bad7d814848842d7490db02c15c.png.webp?width=768"
has_video: true
has_music: true
video_urls: ["https://media.ytyng.net/ytyng-blog/282/featured-video-1.mp4", "https://media.ytyng.net/ytyng-blog/282/featured-video-2.mp4", "https://media.ytyng.net/ytyng-blog/282/featured-video-3.mp4"]
music_urls: ["https://media.ytyng.net/ytyng-blog/282/featured-music-282-3.mp3", "https://media.ytyng.net/ytyng-blog/282/featured-music-282-4.mp3"]
lang: "ja"
---

# BitLocker の復号化キーのファイルは utf-16のため grep -R で検索できないのでスクリプトで検索する

Windows のストレージを BitLocker で暗号化する時、そのキーファイルをファイルとして保存できる。

ファイルが複数ある場合、内容の一致するファイルを検索したくて

```shell
grep -R 'AABBCC' .
```
とかしても、ファイルエンコーディングが UTF-16 なのでマッチしない。

マッチさせるテクニックは、下記 StackOverflow に書いてある
https://stackoverflow.com/questions/3752913/grepping-binary-files-and-utf16

これによると、おそらく
```shell
grep -Ra 'A.A.B.B.C.C.' .
```
とするとで実現可能そうだが、私は今回はこの方法を行わず Python スクリプトを書いて行った。
python では、 decode('utf16', errors='ignore') して str にすると内容を読める。


```python
import glob
import os

bitlocker_key_save_dir = '<my-mounted-bitlocker-key-save-dir>'

needle = 'AABBCC'


def main():
    # bitlocker_key_save_dir 以下のファイルをサブディレクトリも含めてすべて取得
    for file_path in glob.glob(
        f'{bitlocker_key_save_dir}/**/*', recursive=True
    ):
        # file_path がファイルでなければスキップ
        if not os.path.isfile(file_path):
            continue
        print(f'\r{file_path}', end='', flush=True)
        # 内容を print
        content_bytes = open(file_path, 'rb').read()
        content = content_bytes.decode('utf16', errors='ignore')
        if needle in content:
            print('\n')
            print(content)


if __name__ == '__main__':
    main()
```
