|
| |
Code samples available on this page:
Symmetric Key String Encryption/Decryption
'--- Create a DynCrypto object (ASP script) ---
Set DynCrypto = server.CreateObject("DynCrypto.Crypto")
'--- define the password ---
Password = "My Password"
'--- Encrypt ---
Data = "Please encrypt this string"
CipherText = DynCrypto.SymEncrypt(password,Data)
'--- Decrypt ---
ClearText = DynCrypto.SymDecrypt(password,CipherText)
|
Asymmetric Key String Encryption/Decryption
'--- Create a DynCrypto object (ASP script) ---
Set DynCrypto = server.CreateObject("DynCrypto.Crypto")
'--- define the password ---
PrivateKey = "My Password"
'--- create the public key
PublicKey = DynCrypto.AsymPublicKey(PrivateKey)
'--- Encrypt ---
Data = "Please encrypt this string"
CipherText = DynCrypto.DynCrypto.AsymEncrypt(PublicKey,Data)
'--- Decrypt ---
ClearText = DynCrypto.AsymDecrypt(PrivateKey,CipherText)
|
Generate a Hash value from a String
'--- Create a DynCrypto object (ASP script) ---
Set DynCrypto = server.CreateObject("DynCrypto.Crypto")
'-- Generate a Hash Value
Data = "Please encrypt this string"
HashValue = DynCrypto.Hash(Data)
|
Asymmetric Key File Encryption/Decryption
'--- We Create an DynCrypto object
Set DynCrypto = CreateObject("DynCrypto.Crypto")
PrivateKey = "my pass"
PublicKey = DynCrypto.AsymPublicKey(PrivateKey)
CurDir = "L:\dynasp\html\test\dyncrypto\15\"
'CurDir = "C:\TEMP\"
ClearFile = CurDir & "CLEAR.TXT" '--- file to encrypt
'--- We encrypt the file.
'--- The full file name for the crypted file is returned in variable "CipherFile"
CipherDir = CurDir & "OUT" '--- Save the crypted file in this directory
Result = DynCrypto.AsymEncryptFile(PublicKey,ClearFile,CipherDir,CipherFile)
if Result <> 0 then
'--- Error. Result contain the Win32 error code.
WScript.echo "Encryption OK"
end if
'--- We decrypt the crypted file.
'--- The full file name for the clear file is returned in variable "ClearFileOut"
ClearDir = CurDir & "OUT" '--- Save the decrypted file in this directory
Result = DynCrypto.AsymDecryptFile(PrivateKey,CipherFile,ClearDir,ClearFileOut,HashCheck)
if (Not HashCheck) or Result <> 0 then
'--- Error. Invalid private key, cipher data was corrupted
else
'--- Success. File was decrypted. Private key is valid
end if
|
Symmetric Key File Encryption/Decryption
The code below assume that C:\TEMP and C:\TEMP\OUT are valid directories, and
that file CLEAR.TXT exists and is the file to encrypt.
Password = "my pass"
CurDir = "C:\TEMP\"
ClearFile = CurDir & "CLEAR.TXT" '--- file to encrypt
'--- We Create an DynCrypto object
Set DynCrypto = CreateObject("DynCrypto.Crypto")
'--- We encrypt the file.
'--- The full file name for the crypted file is returned in variable "CipherFile"
CipherDir = CurDir & "OUT" '--- Save the crypted file in this directory
Result = DynCrypto.SymEncryptFile(Password,ClearFile ,CipherDir,CipherFile)
if Result <> 0 then
'--- Error. Result contain the Win32 error code.
end if
'--- We decrypt the crypted file.
'--- The full file name for the clear file is returned in variable "ClearFileOut"
ClearDir = CurDir & "OUT" '--- Save the decrypted file in this directory
Result = DynCrypto.SymDecryptFile(Password,CipherFile,ClearDir,ClearFileOut,HashCheck)
if (Not HashCheck) or Result <> 0 then
'--- Error. Invalid private key, cipher data was corrupted
else
'--- Success. File was decrypted. Private key is valid
end if
|
Generate a Hash value from a File
The sample code below creates a Hash value from file C:\TEMP\CLEAR.TXT. The result is
stored in a String variable:
'--- We Create an DynCrypto object
Set DynCrypto = CreateObject("DynCrypto.Crypto")
FileName = "C:\TEMP\CLEAR.TXT"
HashValue = DynCrypto.HashFile(FileName)
|
The sample code below creates a Hash value from file C:\TEMP\CLEAR.TXT. The result is
stored in an Array of Bytes:
'--- We Create an DynCrypto object
Set DynCrypto = CreateObject("DynCrypto.Crypto")
FileName = "C:\TEMP\CLEAR.TXT"
HashValue = DynCrypto.HashFile(FileName,1)
'--- Example, to walk through each byte of the array
if IsArray(HashValue) then
cData = ""
for i=0 to ubound(HashValue)
cData = cData & HashValue(i) & "."
next
'--- cData is now a string in which are listed 20 bytes representing the Hash Value
'--- (ex: 12.50.26.78.26.98.78.41.23.69.64.85.94.12.12.69.87.58.29.64)
end if
|
Symmetric Key Encryption in an SQL Server Stored Procedure
The sample code below will create a stored procedure called SymEncrypt for
symmetric key encryption.
This procedure accepts two parameters: the password and the string to be
encrypted.
Requires DynCrypto version 1.5.2 or newer
CREATE PROCEDURE SymEncrypt
@Password varchar(255) = '',
@ClearData varchar(255) = ''
AS
DECLARE @object int
DECLARE @hr int
DECLARE @CipherData varchar(255)
DECLARE @source varchar(255)
DECLARE @description varchar(255)
DECLARE @Tmp varchar(255)
--- Create a DynCrypto object
EXEC @hr = sp_OACreate 'DynCrypto.Crypto', @object OUT
IF @hr <> 0
BEGIN
--- Report error
EXEC @hr = sp_OAGetErrorInfo @object, @source OUT, @description OUT
IF @hr = 0
BEGIN
SELECT @Tmp = '(' + @source + ') ' + @description
PRINT @Tmp
END
RETURN
END
--- Call SymEncrypt
EXEC @hr = sp_OAGetProperty @object, 'SymEncrypt', @CipherData OUT, @Password, @ClearData
IF @hr <> 0
BEGIN
--- Report error
EXEC @hr = sp_OAGetErrorInfo @object, @source OUT, @description OUT
IF @hr = 0
BEGIN
SELECT @Tmp = '(' + @source + ') ' + @description
PRINT @Tmp
END
RETURN
END
SELECT @Tmp = 'Encrypted Data: ' + @CipherData
PRINT @Tmp
--- Destroy the DynCrypto object
EXEC @hr = sp_OADestroy @object
IF @hr <> 0
BEGIN
--- Report error
EXEC @hr = sp_OAGetErrorInfo @object, @source OUT, @description OUT
IF @hr = 0
BEGIN
SELECT @Tmp = '(' + @source + ') ' + @description
PRINT @Tmp
END
RETURN
END
RETURN
GO
|
Symmetric Key Encryption SQL Server User
Defined Function
The two sample codes below will create SQL Server (2000 and newer) User Defined
Functions called SymEncrypt and SymDecrypt for
symmetric key encryption.
These functions accepts two parameters: the password and the string to be
encrypted/decrypted. (Requires DynCrypto version 1.5.2 or newer)
CREATE FUNCTION SymEncrypt (@Password varchar(255), @ClearData varchar(255))
RETURNS varchar(255) AS
BEGIN
DECLARE @object int
DECLARE @hr int
DECLARE @CipherData varchar(255)
DECLARE @source varchar(255)
DECLARE @description varchar(255)
DECLARE @Tmp varchar(255)
EXEC @hr = sp_OACreate 'DynCrypto.Crypto', @object OUT
IF @hr <> 0
BEGIN
--- Report error
EXEC @hr = sp_OAGetErrorInfo @object, @source OUT, @description OUT
IF @hr = 0
BEGIN
SET @Tmp = '(' + @source + ') ' + @description
END
RETURN @Tmp
END
--- Call SymEncrypt
EXEC @hr = sp_OAGetProperty @object, 'SymEncrypt', @CipherData OUT, @Password, @ClearData
IF @hr <> 0
BEGIN
--- Report error
EXEC @hr = sp_OAGetErrorInfo @object, @source OUT, @description OUT
IF @hr = 0
BEGIN
SET @Tmp = '(' + @source + ') ' + @description
END
RETURN @Tmp
END
--- Destroy the DynCrypto object
EXEC @hr = sp_OADestroy @object
IF @hr <> 0
BEGIN
--- Report error
EXEC @hr = sp_OAGetErrorInfo @object, @source OUT, @description OUT
IF @hr = 0
BEGIN
SET @Tmp = '(' + @source + ') ' + @description
END
RETURN @Tmp
END
RETURN @CipherData
END
|
CREATE FUNCTION SymDecrypt (@Password varchar(255), @CipherData varchar(255))
RETURNS varchar(255) AS
BEGIN
DECLARE @object int
DECLARE @hr int
DECLARE @ClearData varchar(255)
DECLARE @source varchar(255)
DECLARE @description varchar(255)
DECLARE @Tmp varchar(255)
EXEC @hr = sp_OACreate 'DynCrypto.Crypto', @object OUT
IF @hr <> 0
BEGIN
--- Report error
EXEC @hr = sp_OAGetErrorInfo @object, @source OUT, @description OUT
IF @hr = 0
BEGIN
SET @Tmp = '(' + @source + ') ' + @description
END
RETURN @Tmp
END
--- Call SymDecrypt
EXEC @hr = sp_OAGetProperty @object, 'SymDecrypt', @ClearData OUT, @Password, @CipherData
IF @hr <> 0
BEGIN
--- Report error
EXEC @hr = sp_OAGetErrorInfo @object, @source OUT, @description OUT
IF @hr = 0
BEGIN
SET @Tmp = '(' + @source + ') ' + @description + '2'
END
RETURN @Tmp
END
--- Destroy the DynCrypto object
EXEC @hr = sp_OADestroy @object
IF @hr <> 0
BEGIN
--- Report error
EXEC @hr = sp_OAGetErrorInfo @object, @source OUT, @description OUT
IF @hr = 0
BEGIN
SET @Tmp = '(' + @source + ') ' + @description
END
RETURN @Tmp
END
RETURN @ClearData
END
|
Use example SQL statements:
| UPDATE MyTable SET CipherData=SymEncrypt('MyPassword','Some clear text
data') WHERE ...
|
| SELECT SymDecrypt('MyPassword', CipherData) AS ClearData FROM MyTable
|
|