쓸만한 주저리

capicom을 이용한 암/복호화

봄돌73 2013. 12. 2. 12:24
http://msdn.microsoft.com/en-us/library/ms995332.aspx


What Is CAPICOM?

CAPICOM is a Microsoft® ActiveX® control that provides a COM interface to Microsoft CryptoAPI. It exposes a select set of CryptoAPI functions to enable application developers to easily incorporate digital signing and encryption functionality into their applications. Because it uses COM, application developers can access this functionality in a number of programming environments such as Microsoft® Visual Basic®, Visual Basic Script, Active Server Pages, Microsoft® JScript®, C++, and others. CAPICOM is packaged as an ActiveX control, allowing Web developers to utilize it in Web based applications as well.


Where To Get It

CAPICOM is available today for download from the MSDN Web site. CAPICOM is delivered with the Platform SDK and is redistributable free of charge. The Platform SDK contains CAPICOM samples, API documentation, and the redistributable CAPICOM ActiveX control. The Platform SDK can be downloaded from the following URL:

http://www.microsoft.com/en-us/download/details.aspx?id=25281


예제1

출처 : http://yungun.net/64


function MD5(strString)
   dim cp
   set cp = Server.CreateObject("CAPICOM.HashedData")
   cp.Algorithm = 3
   cp.Hash Ustr2Bstr(strString) 'Unicode string -> Byte String Conversion
   MD5 = cp.Value
   set cp = nothing
end function

function UStr2Bstr(UStr)
  'Unicode string to Byte string conversion
  Dim lngLoop
  Dim strChar
  dim strResult
  strResult = ""
  For lngLoop = 1 to Len(UStr)
    strChar = Mid(UStr, lngLoop, 1)
    strResult = strResult & ChrB(AscB(strChar))
  Next
  UStr2Bstr = strResult
end function


원하는 알고리즘에 따라 아래 상수를 적용하면 됩니다.

Const CAPICOM_HASH_ALGORITHM_SHA1 = 0
Const CAPICOM_HASH_ALGORITHM_MD2 = 1
Const CAPICOM_HASH_ALGORITHM_MD4 = 2
Const CAPICOM_HASH_ALGORITHM_MD5 = 3
Const CAPICOM_HASH_ALGORITHM_SHA256 = 4
Const CAPICOM_HASH_ALGORITHM_SHA384 = 5
Const CAPICOM_HASH_ALGORITHM_SHA512 = 6


참고로, Base64 encoding 이나 decoding 해야하는 경우

set cp = server.createobject("CAPICOM.Utilities")
strEncoded = cp.Base64Encode(strString)
strDecoded = cp.Base64Decode(strString)
set cp = nothing



예제 2

출처 : http://blog.naver.com/PostView.nhn?blogId=tick0000&logNo=50092768556

<%

Const CAPICOM_ENCRYPTION_ALGORITHM_3DES = 3
Const gcKEY = "ABCDEFG"


Public Function Encrypt(Message)
    Dim ed, key
    key = gcKEY

    Set ed = CreateObject("CAPICOM.EncryptedData")
    ed.Content = Message
    ed.SetSecret key
    Encrypt = ed.Encrypt
    Set ed = Nothing
End Function

 

Public Function Decrypt(EncMessage)
    Dim ed, key

    key = gcKEY
    Set ed = CreateObject("CAPICOM.EncryptedData")

    ed.SetSecret key
    ed.Algorithm.Name = CAPICOM_ENCRYPTION_ALGORITHM_3DES
    ed.Decrypt(EncMessage)
    Decrypt = ed.Content

    Set ed = Nothing
End Function
...

%>