Do Not Put Secret Strings in Plaintext Files ― Calling Windows DPAPI from PowerShell

セキュリティカテゴリを表すパンダのイラスト Security

About This Article
This article was created using an automated generation flow leveraging generative AI. It reviews the .NET ProtectedData specifications and organizes tips on calling the Windows DPAPI from PowerShell to protect secret strings.

Verification Status: 📘 Official API Verified · PowerShell Sample Implemented · Windows PowerShell 5.1 Real Machine Unverified

Do Not Put Secret Strings in Plaintext Files ― Calling Windows DPAPI from PowerShell

You want to avoid writing API keys or passwords directly into configuration files. However, for small automations on a personal PC, setting up a heavy secret management infrastructure is overkill. In such cases, Windows provides a mechanism called DPAPI.

From PowerShell, you can call it using .NET’s System.Security.Cryptography.ProtectedData.

Protecting with the CurrentUser Scope

# CurrentUser specifies protection bound to the "current Windows user".

$scope = [System.Security.Cryptography.DataProtectionScope]::CurrentUser

# Because DPAPI accepts a byte array, convert the string to UTF-8.


# A dummy string for explanation purposes is used here.

$bytes = [Text.Encoding]::UTF8.GetBytes("dummy-secret")

# ProtectedData.Protect() calls the Windows DPAPI.


# The second argument $null specifies that no additional entropy is used.

$protected = [System.Security.Cryptography.ProtectedData]::Protect(
    $bytes,
    $null,
    $scope
)

# After encryption, it is a byte array rather than a string, so save it as binary.

[IO.File]::WriteAllBytes(".secret.bin", $protected)

This example uses a scope bound to the current Windows user. The saved secret.bin is not a plaintext string.

However, This Does Not Make Secret Management “Foolproof”

CurrentUser can be decrypted within the context of the same Windows user. In other words, it is not an all-purpose mechanism that can protect against scenarios where the device or user account itself is compromised.

Additionally, if you only back up the encrypted file, you may lose the ability to decrypt it if you lose the user profile or the information required by DPAPI.

It is easiest to start using this for purposes such as:

  • Auxiliary scripts on a personal PC

  • Small-scale automation where you want to avoid plaintext storage

  • Educational material for understanding Windows secret protection mechanisms

and so on.

Do Not Write Secrets to the Command Line

Passing them as arguments like -Secret "本物のパスワード" may cause them to remain in history and logs. In the GitHub sample, input is handled via Read-Host -AsSecureString, and conversion memory is cleared as much as possible.

flowchart LR
    A[秘密文字列] --> B[ProtectedData.Protect]
    B --> C[暗号化バイト列]
    C --> D[ファイル保存]
    D --> E[同じユーザーでUnprotect]

GitHub Sample

Official Information & Primary Sources

ライセンス:本記事のテキスト/コードは特記なき限り CC BY 4.0 です。引用の際は出典URL(本ページ)を明記してください。
利用ポリシー もご参照ください。

コメント

Copied title and URL