Mã hoá trong SQL Server 2005

MAK

Mã hoá là một phương pháp quan trọng nhằm bảo mật dữ liệu. Những dữ liệu nhạy cảm như số CMT, số thẻ tín dụng, mật khẩu… cần phải được bảo vệ trước vô vàn mối nguy hiểm tấn công hiện nay. Trong SQL Server 2000 bạn có thể tự tạo các hàm của riêng mình hoặc sử dụng các DLL ngoài để mã hoá dữ liệu. Trong SQL Server 2005, các hàm và phương thức này được mặc định cho phép sẵn.

SQL Server 2005 cung cấp các kỹ thuật sau để mã hoá dữ liệu

  • Mã hoá bằng mật khẩu
  • Mã hoá khoá đối xứng
  • Mã hoá khoá không đối xứng
  • Mã hoá chứng nhận

Trong phần đầu của loạt bài này, chúng tôi sẽ giải thích cách sử dụng kỹ thuật mã hoá bằng mật khẩu và phương pháp giải mã nó.

SQL Server 2005 cung cấp 2 hàm cho việc mã hoá: một cho việc mã hoá và một cho việc giải mã.

“Mã hoá bằng mật khẩu” là phương pháp mã hoá dữ liệu cơ bản thông qua mật khẩu. Dữ liệu có thể được giải mã nếu nhập đúng mật khẩu đã sử dụng khi mã hoá. Chúng ta sẽ thử một ví dụ về việc mã hoá và giải mã dữ liệu bằng kỹ thuật mã hoá thông qua mật khẩu.

select EncryptedData = EncryptByPassPhrase('MAK', '123456789' )

Kết quả

EncryptedData
0x0100000000214F5A73054F3AB954DD23571154019F3EFC031ABFCCD258FD22ED69A48002

Giờ chúng ta sẽ thực thi 3 lần hàm Encryptbypassphrase trên theo ví dụ sau

declare @count int
declare @SocialSecurityNumber varchar(500)
declare @password varchar(12)
set @count =1
while @count<=3
begin
set @SocialSecurityNumber = '123456789'
set @Password = 'MAK'
select EncryptedData = EncryptByPassPhrase(@password, @SocialSecurityNumber )
set @count=@count+1
end

Kết quả

EncryptedData
0x01000000CBB7EE45B5C1460D6996B149CE16B76C7F7CD598DC56364D106B05D47B930093

(1 row(s) affected)

EncryptedData
0x010000005E884D30C8FF7E4723D4E70A03B0B07F877667BAF1DA9BE1E116434842D11B99

(1 row(s) affected)

EncryptedData
0x01000000C508FB0C4FC7734B47B414D2602A71A338417DD685229173684D319334A084CD

Lưu ý:
“123456789” ở đây có thể là số thẻ tín dụng và “MAK” là mật khẩu

Kết quả của Encryptbypassphrase sau mỗi lần thực thi hàm là khác nhau. Tuy nhiên, khi bạn giải mã dữ liệu thì nó vẫn ra kết quả như ban đầu trước khi mã hoá.

Giờ chúng ta sẽ thử giải mã dữ liệu đã được mã hoá ở trên với hàm DecryptByPassPhrase

select convert(varchar(100),DecryptByPassPhrase('MAK', 0x01000000CBB7EE45B5C1460D6996B149CE16B76C7F7CD598DC56364D106B05D47B930093))

select convert(varchar(100),DecryptByPassPhrase('MAK', 0x010000005E884D30C8FF7E4723D4E70A03B0B07F877667BAF1DA9BE1E116434842D11B99))

select convert(varchar(100),DecryptByPassPhrase('MAK', 0x01000000C508FB0C4FC7734B47B414D2602A71A338417DD685229173684D319334A084CD))

Kết quả

123456789

(1 row(s) affected)


123456789

(1 row(s) affected)


123456789

(1 row(s) affected)

Thử giải mã dữ liệu đã được mã hoá với một mật khẩu khác. Thực thi theo câu lệnh sau

select convert(varchar(100),DecryptByPassPhrase('test', 0x01000000C508FB0C4FC7734B47B414D2602A71A338417DD685229173684D319334A084CD))

Kết quả

NULL

(1 row(s) affected)

Kết quả cho bạn thấy SQL Server trả lại giá trị NULL nếu mật khẩu sai.

Giờ chúng ta sẽ thử tạo một bảng chứa số thẻ tín dụng và số CMT, sau đó sẽ mã hoá dữ liệu này thông qua phương pháp mã hoá mật khẩu.

USE [master]
GO
/****** Object: Database [admin] Script Date: 11/25/2007 10:50:47 ******/
IF EXISTS (SELECT name FROM sys.databases WHERE name = N'Customer DB')
DROP DATABASE [Customer DB]
go

create database [Customer DB]
go
use [Customer DB]
go

create table [Customer data]
([customer id] int,
[Credit Card Number] bigint,
[Social Security Number] bigint)
go

insert into [Customer data] values (1, 1234567812345678, 123451234)
insert into [Customer data] values (2, 1234567812345378, 323451234)
insert into [Customer data] values (3, 1234567812335678, 133451234)
insert into [Customer data] values (4, 1234567813345678, 123351234)
insert into [Customer data] values (5, 1234563812345678, 123431234)
go

Tạo hai cột để lưu dữ liệu đã được mã hoá

use [Customer DB]
go
alter table [Customer Data] add
[Encrypted Credit Card Number] varbinary(MAX)
go
alter table [Customer Data] add
[Encrypted Social Security Number] varbinary(MAX)
go

Cập nhật dữ liệu đã được mã hoá vào hai cột vừa tạo
use [Customer DB]
go
update [Customer Data] set [Encrypted Credit Card Number] =
EncryptByPassPhrase('Credit Card', convert(varchar(100),[Credit Card Number]) )
go
update [Customer Data] set [Encrypted Social Security Number] =
EncryptByPassPhrase('Social Security', convert(varchar(100),[Social Security Number]) )
Go

Truy vẫn bảng bằng các lệnh sau (hình 1)

use [Customer DB]
go
select * from [customer data]
go

Kết quả


Hình 1

Xoá bỏ cột chứa dữ liệu chưa được mã hoá

use [Customer DB]
go
alter table [Customer Data] drop column [Credit Card Number]
go
alter table [Customer Data] drop column [Social Security Number]
go

Truy vấn bảng theo các lệnh sau (hình 2)

use [Customer DB]
go
select * from [customer data]
go

Kết quả


Hình 2

Giải mã dữ liệu trên bảng thông qua hàm Decryptbypassphrase như sau (hình 3)

use [Customer DB]
go
select
[customer id],
convert(bigint,convert(varchar(100),decryptbypassphrase('Credit Card',[Encrypted Credit Card Number]) )) as
[Credit Card Number],
convert(bigint,convert(varchar(100),decryptbypassphrase('Social Security',[Encrypted Social Security Number] ) )) as
[Social Security Number] from [customer data]
Go

Kết quả

customer id,Credit Card Number,Social Security Number
1, 1234567812345678, 123451234
2, 1234567812345378, 323451234
3, 1234567812335678, 133451234
4, 1234567813345678, 123351234
5, 1234563812345678, 123431234


Hình 3

Kết luận

Mã hoá dữ liệu thực sự rất quan trọng. Thông qua bài này chúng tôi đã giới thiệu đến các bạn một trong 4 kỹ thuật mã hoá sẵn có trong SQL Server 2005 – kỹ thuật mã hoá bằng mật khẩu – và phương pháp giải mã nó. Trong bài sau, chúng ta sẽ bàn luận về phương pháp hack/khôi phục dữ liệu đã được mã hoá bằng mật khẩu này.

Thứ Ba, 11/03/2008 11:16
4,210 👨 18.101
0 Bình luận
Sắp xếp theo
    ❖ Tổng hợp