Skip to Content
C# 12 核心要点
book

C# 12 核心要点

by Joseph Albahari
May 2025
Intermediate to advanced
1086 pages
14h 54m
Chinese
O'Reilly Media, Inc.
Content preview from C# 12 核心要点

第 20 章 密码学 密码学

本作品已使用人工智能进行翻译。欢迎您提供反馈和意见:translation-feedback@oreilly.com

在本章中,我们将讨论 .NET 中的主要加密 API:

  • Windows 数据保护 API (DPAPI)

  • 散列

  • 对称加密

  • 公钥加密和签名

本章所涉及的类型定义在以下命名空间中:

System.Security;
System.Security.Cryptography;

概述

表 20-1总结了 .NET 中的加密选项。在接下来的章节中,我们将逐一探讨。

表 20-1. .NET 中的加密和散列选项
选项 管理关键 速度 实力 说明
File.Encrypt 0 快速 取决于用户密码 通过文件系统支持透明地保护文件。密钥由登录用户的凭据隐式生成。仅限 Windows。
Windows 数据保护 0 快速 取决于用户密码 使用隐式导出密钥加密和解密字节数组。
散列 0 快速 单向(不可逆)转换。用于存储密码、比较文件和检查数据是否损坏。
对称加密 1 快速 用于通用加密/解密。使用相同的密钥进行加密和解密。可用于保护传输中信息的安全。
公钥加密 2 加密和解密使用不同的密钥。用于在信息传输中交换对称密钥,以及对文件进行数字签名。

.NET还在System.Security.Cryptography.Xml 中为创建和验证基于XML的签名提供了更专业的支持,并在System.Security.Cryptography.X509Certificates 中提供了处理数字证书的类型。

Windows 数据保护

备注

Windows 数据保护功能仅适用于 Windows,在其他操作系统上则会出现PlatformNotSupportedException

"文件和目录操作 "一节中我们介绍了如何使用File.Encrypt 请求操作系统对文件进行透明加密:

File.WriteAllText ("myfile.txt", "");
File.Encrypt ("myfile.txt");
File.AppendAllText ("myfile.txt", "sensitive data");

在这种情况下,加密使用的密钥来自登录用户的密码。通过 Windows 数据保护 API (DPAPI),您可以使用相同的隐式派生密钥对字节数组进行加密。DPAPI 通过 ProtectedData类--一种具有两个静态方法的简单类型:

public static byte[] Protect
  (byte[] userData, byte[] optionalEntropy, DataProtectionScope scope);

public static byte[] Unprotect
  (byte[] encryptedData, byte[] optionalEntropy, DataProtectionScope scope);

optionalEntropy 中包含的内容都会添加到密钥中,从而提高密钥的安全性。DataProtectionScope 枚举参数允许两个选项:Curren⁠t​UserLocalMachine 。使用CurrentUser 时,密钥来自登录用户的凭据;使用LocalMachine 时,使用所有用户通用的全机器密钥。这意味着使用CurrentUser 范围,一个用户加密的数据无法被另一个用户解密。LocalMachine ...

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.

Read now

Unlock full access

More than 5,000 organizations count on O’Reilly

AirBnbBlueOriginElectronic ArtsHomeDepotNasdaqRakutenTata Consultancy Services

QuotationMarkO’Reilly covers everything we've got, with content to help us build a world-class technology community, upgrade the capabilities and competencies of our teams, and improve overall team performance as well as their engagement.
Julian F.
Head of Cybersecurity
QuotationMarkI wanted to learn C and C++, but it didn't click for me until I picked up an O'Reilly book. When I went on the O’Reilly platform, I was astonished to find all the books there, plus live events and sandboxes so you could play around with the technology.
Addison B.
Field Engineer
QuotationMarkI’ve been on the O’Reilly platform for more than eight years. I use a couple of learning platforms, but I'm on O'Reilly more than anybody else. When you're there, you start learning. I'm never disappointed.
Amir M.
Data Platform Tech Lead
QuotationMarkI'm always learning. So when I got on to O'Reilly, I was like a kid in a candy store. There are playlists. There are answers. There's on-demand training. It's worth its weight in gold, in terms of what it allows me to do.
Mark W.
Embedded Software Engineer

You might also like

Programming C# 12

Programming C# 12

Ian Griffiths
C# 12 in a Nutshell

C# 12 in a Nutshell

Joseph Albahari
C# 6 for Programmers, Sixth Edition

C# 6 for Programmers, Sixth Edition

Paul Deitel, Harvey Deitel
Head First C#, 4th Edition

Head First C#, 4th Edition

Andrew Stellman, Jennifer Greene

Publisher Resources

ISBN: 9798341657038