【问题标题】:Read key file located in C:\ProgramData\Microsoft\Crypto\RSA\MachineKeys读取位于 C:\ProgramData\Microsoft\Crypto\RSA\MachineKeys 中的密钥文件
【发布时间】:2019-11-25 16:17:28
【问题描述】:

我们创建的一个实用程序在 C:\ProgramData\Microsoft\Crypto\RSA\MachineKeys 中生成了太多文件。为了安全地删除这些文件,我想打开每个文件并检查密钥。如何在 C# 中打开这些密钥文件?我查看了here 的代码。该代码仅返回公钥。我可以从这些关键文件中获得更多信息吗?

【问题讨论】:

  • 您需要找到密钥产生的证书。您不想将公钥和私钥存储在不受信任的位置,因此黑客无法同时获得公钥和私钥。密钥是 Program Data 文件夹中的文件,您可能需要管理员权限才能打开文件。
  • 我有密钥产生的证书和密码。这个问题的根本原因是我每次使用它时都会导入证书,而不是将其导入证书存储区。我改变了实用程序的工作方式。现在它不再生成新的密钥文件了。
  • 你是对的。每次创建新文档时,都应从证书(私钥)中生成一个新的公钥。所以你必须导入证书才能得到新的公钥。
  • 我要如何导入这些文件?没有扩展名。格式是什么?
  • 你有证书。为什么要导入文件?

标签: c# cryptography rsa


【解决方案1】:

我最终使用以下 powershell 从证书存储中获取有效密钥列表。由于我使用的是 IIS 服务,因此我还将 c2319c42033a5ca7f44e731bfd3fa2b5 添加到列表中。我删除了不在此列表中的所有密钥文件。

 $MachineCertStores = Get-ChildItem Cert:\LocalMachine
$UserCertStores = Get-ChildItem Cert:\CurrentUser

Foreach ($Store in $MachineCertStores)
{
    $path = "Cert:\LocalMachine\" + $($store.Name)
    $keys = Get-ChildItem $path
    Foreach ($Key in $Keys)
        {
        $UniqueKeyName = $key.PrivateKey.CspKeyContainerInfo.UniqueKeyContainerName
        if ([string]::IsNullOrWhitespace($UniqueKeyName)){
        }else{
            write-host $UniqueKeyName
            $file = Get-Content "validkey.txt"
            $containsWord = $file | %{$_ -match $UniqueKeyName.substring(0,32)}
            If($containsWord -contains $true)
            {
            }else{
                $UniqueKeyName.substring(0,32) | Out-File 'validkey.txt' -Append
            }
        }
    }
}

Foreach ($Store in $UserCertStores)
{
    $path = "Cert:\CurrentUser\" + $($store.Name)
    $keys = Get-ChildItem $path
    Foreach ($Key in $Keys)
        {
        $UniqueKeyName = $key.PrivateKey.CspKeyContainerInfo.UniqueKeyContainerName
        if ([string]::IsNullOrWhitespace($UniqueKeyName)){
        }else{
            write-host $UniqueKeyName
            $file = Get-Content "validkey.txt"
            $containsWord = $file | %{$_ -match $UniqueKeyName.substring(0,32)}
            If($containsWord -contains $true)
            {
            }else{
                $UniqueKeyName.substring(0,32) | Out-File 'validkey.txt' -Append
            }
        }
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-09-20
    • 2013-12-26
    • 2015-06-21
    • 2015-02-16
    相关资源
    最近更新 更多