Home >Database >Mysql Tutorial >How to encrypt table in sql

How to encrypt table in sql

清浅
清浅Original
2019-05-11 13:08:594618browse

How to encrypt SQL tables: first test the environment; then create the database master key; then create the certificate and a symmetric secret key; and finally encrypt the data.

How to encrypt table in sql

sql server How to set up table encryption

SQL Server supports database-level encryption (TDE) and column-level encryption Data encryption, database-level encryption is performed in the database, which is transparent to the program, and no additional operations are required during the development process. Compared with database-level encryption, the column-level data encryption process is a bit troublesome, and the program needs to do some additional operations. The following figure is the encryption hierarchy of SQL Server. It can be seen that the encryption of SQL Server is hierarchical. The encryption of the upper level protects the encryption of its sub-levels. This article explains a method using the four levels of ①②③④ in the figure to encrypt/decrypt. data process.

How to encrypt table in sql

(1) Test environment description

The test uses SQL Servre 2012 R2 and creates the following data table:

--Create the user table for testing

CREATE TABLE TBLUser 
(    
    Name             nvarchar(30),     
    Password         varbinary(1000),    
)
GO

How to encrypt table in sql

(2) Create the database master key

The database master key is in Under the service master key, encryption is performed by the service master key. This is a database-level key that can be used to provide encryption for creating database-level certificates or asymmetric keys. Each database can only have one database master key, which is created through T-SQL statements. The specific code is as follows:

CREATE MASTER KEY ENCRYPTION BY PASSWORD ='passW@ord'
GO

Image 025.png

(3) Create certificate

Create a symmetric key certificate for encryption, the specific code is as follows :

CREATE CERTIFICATE TestCert with SUBJECT = 'Test Certificate'
GO

Image 026.png

(4) Create a symmetric key

As can be seen from the SQL Server encryption hierarchy, the symmetric key It can be created by password, or other symmetric keys, asymmetric keys, and certificates. This article uses the certificate to create a symmetric secret key for testing. The specific code is as follows:

CREATE SYMMETRIC KEY TestSymmetric WITH ALGORITHM = AES_256
 ENCRYPTION BY CERTIFICATE TestCert 
GO

Image 027.png

(5) Encrypt the data

First Open the certificate symmetric secret key, then use the ENCRYPTBYKEY function to encrypt the data, and then close the symmetric secret key. The specific code is as follows:

OPEN SYMMETRIC KEY TestSymmetric DECRYPTION BY CERTIFICATE TestCert;
INSERT INTO TBLUser values('张三', ENCRYPTBYKEY(Key_Guid(N'TestSymmetric'), '123456'));
CLOSE SYMMETRIC KEY TestSymmetric;
GO

Image 028.png

(6) View the encrypted data

Directly run SQL to query the encrypted data, you can I saw that the content of the password was a string of unreadable hexadecimal characters.

SELECT * FROM TBLUser
GO

Image 029.png

# (7) Decrypt data

First open the certificate symmetric secret key, then use the DecryptByKey function to decrypt the data, and close it after completion Symmetric key. The specific code is as follows:

OPEN SYMMETRIC KEY TestSymmetric DECRYPTION BY CERTIFICATE TestCert;
SELECT Name, CAST(DecryptByKey(password) as varchar(100)) Password FROM TBLUser;
CLOSE SYMMETRIC KEY TestSymmetric;
GO

Image 030.png

The above is the detailed content of How to encrypt table in sql. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Related articles

See more