Chilkat HOME ASP Visual Basic VB.NET C# Visual C++ C MFC Delphi FoxPro Java Perl PHP Python Ruby SQL Server VBScript
|
ARC4 Encryption (ARCFOUR)Demonstrates simple ARC4 encryption to match some simple test vectors published by Wikipedia. ARC4 is a fast stream cipher supporting key lengths from 8 to 2048 bits (i.e. from 1 to 256 bytes). It is not a block cipher, so there is no padding, no modes, and no initialization vector.
CREATE PROCEDURE ChilkatSample AS BEGIN DECLARE @hr int DECLARE @crypt int EXEC @hr = sp_OACreate 'Chilkat.Crypt2', @crypt OUT IF @hr <> 0 BEGIN PRINT 'Failed to create ActiveX component' RETURN END DECLARE @success int EXEC sp_OAMethod @crypt, 'UnlockComponent', @success OUT, 'Anything for 30-day trial' IF @success <> 1 BEGIN PRINT 'Crypt component unlock failed' RETURN END -- Set the encryption algorithm to ARC4: EXEC sp_OASetProperty @crypt, 'CryptAlgorithm', 'arc4' -- We want the encrypted output to be a hex-encoded string. EXEC sp_OASetProperty @crypt, 'EncodingMode', 'hex' -- Encrypt some test vectors from Wikipedia: DECLARE @cipherText nvarchar(4000) DECLARE @plainText nvarchar(4000) -- The key length (in bits) is equal to the number of us-ascii -- bytes in our key string * 8. -- ARC4( "Key", "Plaintext" ) == BBF316E8D940AF0AD3 EXEC sp_OASetProperty @crypt, 'KeyLength', 24 EXEC sp_OAMethod @crypt, 'SetEncodedKey', NULL, 'Key', 'ascii' EXEC sp_OAMethod @crypt, 'EncryptStringENC', @cipherText OUT, 'Plaintext' PRINT @cipherText EXEC sp_OAMethod @crypt, 'DecryptStringENC', @plainText OUT, @cipherText PRINT @plainText -- ARC4( "Wiki", "pedia" ) == 1021BF0420 EXEC sp_OASetProperty @crypt, 'KeyLength', 32 EXEC sp_OAMethod @crypt, 'SetEncodedKey', NULL, 'Wiki', 'ascii' EXEC sp_OAMethod @crypt, 'EncryptStringENC', @cipherText OUT, 'pedia' PRINT @cipherText EXEC sp_OAMethod @crypt, 'DecryptStringENC', @plainText OUT, @cipherText PRINT @plainText -- ARC4( "Secret", "Attack at dawn" ) == 45A01F645FC35B383552544B9BF5 EXEC sp_OASetProperty @crypt, 'KeyLength', 48 EXEC sp_OAMethod @crypt, 'SetEncodedKey', NULL, 'Secret', 'ascii' EXEC sp_OAMethod @crypt, 'EncryptStringENC', @cipherText OUT, 'Attack at dawn' PRINT @cipherText EXEC sp_OAMethod @crypt, 'DecryptStringENC', @plainText OUT, @cipherText PRINT @plainText -- Note: The call to SetEncodedKey serves to set the key -- to the us-ascii bytes of the string. END GO |
Need a specific example? Send a request to support@chilkatsoft.com
© 2000-2007 Chilkat Software, Inc. All Rights Reserved.