MSSQL 데이터 열 암호화/복호화

ms-help://MS.SQLCC.v9/MS.SQLSVR.v9.ko/udb9/html/38e9bf58-10c6-46ed-83cb-e2d76cda0adc.htm

대칭 키를 사용하여 열을 암호화합니다.
언어 : sql
  1. USE AdventureWorks;
  2. GO
  3.  
  4. --If there is no master key, create one now
  5. IF NOT EXISTS
  6.     (SELECT * FROM sys.symmetric_keys WHERE symmetric_key_id = 101)
  7.     CREATE MASTER KEY ENCRYPTION BY
  8.     PASSWORD = '23987hxJKL95QYV4369#ghf0%94467GRdkjuw54ie5y01478dDkjdahflkujaslekjg5k3fd117r$$#1946kcj$n44ncjhdlj'
  9. GO
  10.  
  11. CREATE CERTIFICATE HumanResources037
  12.    WITH SUBJECT = 'Employee Social Security Numbers';
  13. GO
  14.  
  15. CREATE SYMMETRIC KEY SSN_Key_01
  16.     WITH ALGORITHM = AES_256
  17.     ENCRYPTION BY CERTIFICATE HumanResources037;
  18. GO
  19.  
  20. USE [AdventureWorks];
  21. GO
  22.  
  23. -- Create a column in which to store the encrypted data
  24. ALTER TABLE HumanResources.Employee
  25.     ADD EncryptedNationalIDNumber varbinary(128);
  26. GO
  27.  
  28. -- Open the symmetric key with which to encrypt the data
  29. OPEN SYMMETRIC KEY SSN_Key_01
  30.    DECRYPTION BY CERTIFICATE HumanResources037;
  31.  
  32. -- Encrypt the value in column NationalIDNumber with symmetric
  33. -- key SSN_Key_01. Save the result in column EncryptedNationalIDNumber.
  34. UPDATE HumanResources.Employee
  35. SET EncryptedNationalIDNumber = EncryptByKey(Key_GUID('SSN_Key_01'), NationalIDNumber);
  36. GO
  37.  
  38. -- Verify the encryption.
  39. -- First, open the symmetric key with which to decrypt the data
  40. OPEN SYMMETRIC KEY SSN_Key_01
  41.    DECRYPTION BY CERTIFICATE HumanResources037;
  42. GO
  43.  
  44. -- Now list the original ID, the encrypted ID, and the
  45. -- decrypted ciphertext. If the decryption worked, the original
  46. -- and the decrypted ID will match.
  47. SELECT NationalIDNumber, EncryptedNationalIDNumber
  48.     AS "Encrypted ID Number",
  49.     CONVERT(nvarchar, DecryptByKey(EncryptedNationalIDNumber))
  50.     AS "Decrypted ID Number"
  51.     FROM HumanResources.Employee;
  52. GO


인증 값을 포함하는 대칭 암호화입니다.

언어 : sql
  1. USE AdventureWorks;
  2. --If there is no master key, create one now
  3. IF NOT EXISTS
  4.     (SELECT * FROM sys.symmetric_keys WHERE symmetric_key_id = 101)
  5.     CREATE MASTER KEY ENCRYPTION BY
  6.     PASSWORD = '23987hxJKL95QYV4369#ghf0%94467GRdkjuw54ie5y01478dDkjdahflkujaslekjg5k3fd117r$$#1946kcj$n44ncjhdlj'
  7. GO
  8.  
  9. CREATE CERTIFICATE Sales09
  10.    WITH SUBJECT = 'Customer Credit Card Numbers';
  11. GO
  12.  
  13. CREATE SYMMETRIC KEY CreditCards_Key11
  14.     WITH ALGORITHM = AES_256
  15.     ENCRYPTION BY CERTIFICATE Sales09;
  16. GO
  17.  
  18. -- Create a column in which to store the encrypted data
  19. ALTER TABLE Sales.CreditCard
  20.     ADD CardNumber_Encrypted varbinary(128);
  21. GO
  22.  
  23. -- Open the symmetric key with which to encrypt the data
  24. OPEN SYMMETRIC KEY CreditCards_Key11
  25.    DECRYPTION BY CERTIFICATE Sales09;
  26.  
  27. -- Encrypt the value in column CardNumber with symmetric key CreditCards_Key11.
  28. -- Save the result in column CardNumber_Encrypted. 
  29. UPDATE Sales.CreditCard
  30. SET CardNumber_Encrypted = EncryptByKey(Key_GUID('CreditCards_Key11')
  31.     , CardNumber, 1, HashBytes('SHA1', CONVERT( varbinary
  32.     , CreditCardID)));
  33. GO
  34.  
  35. -- Verify the encryption
  36. -- First, open the symmetric key with which to decrypt the data
  37.  
  38. OPEN SYMMETRIC KEY CreditCards_Key11
  39.    DECRYPTION BY CERTIFICATE Sales09;
  40. GO
  41.  
  42. -- Now list the original card number, the encrypted card number,
  43. -- and the decrypted ciphertext. If the decryption worked,
  44. -- the original number will match the decrypted number.
  45.  
  46. SELECT CardNumber, CardNumber_Encrypted
  47.     AS "Encrypted card number", CONVERT(nvarchar,
  48.     DecryptByKey(CardNumber_Encrypted, 1 ,
  49.     HashBytes('SHA1', CONVERT(varbinary, CreditCardID))))
  50.     AS "Decrypted card number" FROM Sales.CreditCard;
  51. GO


이올린에 북마크하기(0) 이올린에 추천하기(0)
top


http://www.joon.pe.kr/blog/trackback/307


<< Prev   1   ... 39   40   41   42   43   44   45   46   47   ... 336   Next >>