. The DPAPI encryption programming under the net environment

Source: Internet
Author: User

The local encryption protection mechanism of Windows provides a simple calling interface, the generation and protection of keys are handled by the system, and the programming interface is called DPAPI. The boundary of this encryption protection mechanism is the user login account or the local computer system, using the encryption process set by the operating system to protect the data and decrypt the restored data, the user does not care about the source and management of the key. Using DPAPI to encrypt local sensitive information can save your application from processing the challenge of generating and storing encryption keys.

The protection of DPAPI encryption is similar to that of EFS, beyond the protection boundary. The difference with EFS is that within the same user account or computer protection range, you can also create finer-grained cryptographic segmentation boundaries using an optional number of random numbers. The process that holds the same security token as the encryption, with the same number of optional chaos, is able to access the data. Data that is encrypted within the local computer system, and only when the system is started, the locally running process can decrypt the data. This prevents remote processes from acquiring locally sensitive data, and prevents booting to other operating system instances on a local multi-boot basis, obtaining protected data directly from the hard disk.

The optional disorder is not a strong protection, and malicious code that penetrates the local computer can scan the file for a stored protection word, or it can intercept a password-derived protection word via a keyboard hook. Local computer-wide data protection can be opened by all programs that can run on this computer, and if an attacker is able to gain physical control of the machine, it may be possible to forcibly insert the boot-up process by modifying the registry, unencrypted files, etc., as well as gaining access to these encrypted data. Therefore, it is still necessary to implement effective encryption in a prudent manner to keep the computer from being attacked by malicious code.

In the face of its design security scenario, DPAPI can be considered a strong protection mechanism. Windows uses a well-designed key management mechanism to protect encrypted data.

. NET data protection class is the managed encapsulation of DPAPI. Since this is a service provided by the operating system, the application does not need to add additional libraries.. NET DPAPI has two classes, which are protecteddata classes and Protectedmemory classes, respectively, for general data protection and memory area protection.

The ProtectedData class encrypts a byte array. This feature is in the Windows 2000, XP-only system. The encryption method is to call the static protect function, passing clear text and optional random numbers. The decryption method is to invoke the static unprotect method, passing the ciphertext and the optional random number set when encrypting. An application can specify that encrypted data can be decrypted only by the same user account, or by any account on the local computer. This range is specified by a parameter of the DataProtectionScope enumeration type, as described below:

CurrentUser: The protected data is associated with the current user. Only threads running in the context of the current user can cancel data protection.

LocalMachine: The protected data is associated with the computer context. Any process running on the computer can cancel data protection. This enumeration value is typically used for server-specific applications that run on servers (untrusted users cannot access the server).

Example code for data Encryption protection:

Protecteddata.protect (data, s_aditionalentropy, Dataprotectionscope.currentuser);

Code examples of data decryption to protect:

Protecteddata.unprotect (data, s_aditionalentropy, Dataprotectionscope.currentuser);

Protectedmemory is a more advanced local encryption protection feature introduced later in Vista. It is able to establish a cryptographic isolation of memory between processes, which prevents attackers from accessing sensitive information such as passwords through memory search mechanisms such as virtual memory exchange files. Encrypts an in-memory byte array using the Protectedmemory class. This encryption process replaces plaintext with ciphertext in memory, and the result is that the plaintext cannot be found even when searching for memory. Protectedmemory encrypted memory data cannot be encrypted after the computer restarts if it is saved to the hard disk. The memory that is encrypted by the current process can be specified to be decrypted only by the current process, can be decrypted by all processes, or can be decrypted from the same user context, which is specified by the parameters of the MemoryProtectionScope enumeration type. Specific as follows:

Sameprocess: Only code that runs in the same process as the cryptographic code can be decrypted.

Crossprocess: Any code in any process can be decrypted using the Unprotect method.

Samelogon: Only code that runs in the same user context as encryption can be decrypted.

Example code for memory encryption protection:

Protectedmemory.protect (Secret, Memoryprotectionscope.samelogon);

Code examples of memory decryption to protect:

Protectedmemory.unprotect (Secret, Memoryprotectionscope.samelogon);

. The DPAPI encryption programming under the net environment

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.