SQL Server
SQL Server
RSA Encrypt and Decrypt Credit Card Numbers
See more RSA Examples
_LANGUAGE_ sample code to RSA public-key encrypt and decrypt credit card numbers. The RSA key is loaded from an unencrypted PKCS8 file. Chilkat provides many ways of setting the key -- loading from both encrypted and unencrypted PEM, PKCS8, DER, PVK, etc. Keys may be loaded from files or in-memory representations. (The RSA component also provides the ability to generate RSA keys.)Chilkat SQL Server Downloads
-- Important: See this note about string length limitations for strings returned by sp_OAMethod calls.
--
CREATE PROCEDURE ChilkatSample
AS
BEGIN
DECLARE @hr int
-- Important: Do not use nvarchar(max). See the warning about using nvarchar(max).
DECLARE @sTmp0 nvarchar(4000)
DECLARE @success int
SELECT @success = 0
-- This example assumes the Chilkat API to have been previously unlocked.
-- See Global Unlock Sample for sample code.
DECLARE @rsa int
EXEC @hr = sp_OACreate 'Chilkat.Rsa', @rsa OUT
IF @hr <> 0
BEGIN
PRINT 'Failed to create ActiveX component'
RETURN
END
DECLARE @privKey int
EXEC @hr = sp_OACreate 'Chilkat.PrivateKey', @privKey OUT
-- Load an RSA private key from a file:
EXEC sp_OAMethod @privKey, 'LoadAnyFormatFile', @success OUT, 'rsaPrivateKey.key', ''
IF @success = 0
BEGIN
EXEC sp_OAGetProperty @privKey, 'LastErrorText', @sTmp0 OUT
PRINT @sTmp0
EXEC @hr = sp_OADestroy @rsa
EXEC @hr = sp_OADestroy @privKey
RETURN
END
-- Get the public part of the private key.
DECLARE @pubKey int
EXEC @hr = sp_OACreate 'Chilkat.PublicKey', @pubKey OUT
EXEC sp_OAMethod @privKey, 'ToPublicKey', @success OUT, @pubKey
EXEC sp_OAMethod @rsa, 'UsePublicKey', @success OUT, @pubKey
IF @success = 0
BEGIN
EXEC sp_OAGetProperty @rsa, 'LastErrorText', @sTmp0 OUT
PRINT @sTmp0
EXEC @hr = sp_OADestroy @rsa
EXEC @hr = sp_OADestroy @privKey
EXEC @hr = sp_OADestroy @pubKey
RETURN
END
-- Encrypt a VISA credit card number:
-- 1234-5678-0000-9999
DECLARE @ccNumber nvarchar(4000)
SELECT @ccNumber = '1234567800009999'
DECLARE @usePrivateKey int
SELECT @usePrivateKey = 0
EXEC sp_OASetProperty @rsa, 'EncodingMode', 'base64'
DECLARE @encryptedStr nvarchar(4000)
EXEC sp_OAMethod @rsa, 'EncryptStringENC', @encryptedStr OUT, @ccNumber, @usePrivateKey
PRINT 'Encrypted:'
PRINT @encryptedStr
-- Now decrypt:
DECLARE @rsaDecryptor int
EXEC @hr = sp_OACreate 'Chilkat.Rsa', @rsaDecryptor OUT
EXEC sp_OASetProperty @rsaDecryptor, 'EncodingMode', 'base64'
EXEC sp_OAMethod @rsaDecryptor, 'UsePrivateKey', @success OUT, @privKey
SELECT @usePrivateKey = 1
DECLARE @decryptedStr nvarchar(4000)
EXEC sp_OAMethod @rsaDecryptor, 'DecryptStringENC', @decryptedStr OUT, @encryptedStr, @usePrivateKey
PRINT 'Decrypted:'
PRINT @decryptedStr
-- Important: RSA encryption should only be used to encrypt small amounts of data.
-- It is typically used for encrypting symmetric encryption
-- keys such that a symmetric encryption algorithm, such as
-- AES is then used to encrypt/decrypt bulk data
EXEC @hr = sp_OADestroy @rsa
EXEC @hr = sp_OADestroy @privKey
EXEC @hr = sp_OADestroy @pubKey
EXEC @hr = sp_OADestroy @rsaDecryptor
END
GO