• UPDATE POSTED

    I've found the issue, and I'll see if I can get the guys here to post an update to the code shortly.  Basically Blowfish deals with text in multiples of 8 bytes.  This is fairly normal, all modern algorithms encrypt in blocks of 8 bytes (Blowfish/DES, etc.) or blocks of 16 bytes (Rijndael/AES, etc.)

    The problem is that I forgot to add the code into the XP itself to pad out the input string to 8 bytes, so Blowfish isn't encrypting it properly.  As a temporary work-around, you can create a "pad" function to pad your string with spaces to make it a multiple of 8:

    CREATE FUNCTION dbo.udf_PadZeroChar8(@string VARCHAR(2000))

    RETURNS VARCHAR(2000)

    AS

    BEGIN

     SET @string = @string + SUBSTRING(CHAR(0) + CHAR(0) + CHAR(0) + CHAR(0) + CHAR(0) + CHAR(0) + CHAR(0), 1, 8 - LEN(@string) % 8)

     RETURN @string

    END

    CREATE FUNCTION dbo.udf_StripZeroChar(@string VARCHAR(2000))

    RETURNS VARCHAR(2000)

    AS

    BEGIN

     DECLARE @i INT

     SET @i = LEN(@string)

     WHILE @i > 0 AND SUBSTRING(@string, @i, 1) = CHAR(0)

      SET @i = @i - 1

     IF @i > 0

      SET @string = SUBSTRING(@string, 1, @i)

     RETURN @string

    END

    udf_PadZeroChar8 will right-pad your plaintext string with CHAR(0) out to the next highest multiple of 8 characters.  udf_StripZeroChar will remove right-padded CHAR(0) characters from your string after a decryption.  To use your example, you can use these like this:

    DECLARE @plain_text VARCHAR(500) -- Our plain text

    DECLARE @enc_text VARCHAR(500)   -- Our encrypted text

    DECLARE @dec_text VARCHAR(500)   -- Our decrypted text

    DECLARE @key VARCHAR(500)         -- Our encryption key

    --SET @plain_text = 'Now is the time for all good men to come to the aid of their countrymen.'

    SET @plain_text='This is my test of encrypted message.'

    EXEC master..xp_generatekey 448, @key OUTPUT

    SET @plain_text = dbo.udf_PadZeroChar8(@plain_text)

    EXEC master..xp_blowfishencrypt @plain_text, @key, @enc_text OUTPUT

    EXEC master..xp_blowfishdecrypt @enc_text, @key, @dec_text OUTPUT

    SET @dec_text = dbo.udf_StripZeroChar(@dec_text)

    SET @plain_text = dbo.udf_StripZeroChar(@plain_text)

    PRINT 'Test 3: Longer String/Longer Key'

    PRINT '--Plain Text: ''' + @plain_text + ''''

    PRINT '--Key: ''' + @key + ''''

    PRINT '--Encrypted Text: ''' + @enc_text + ''''

    PRINT '--Decrypted Text: ''' + @dec_text + ''''

    IF @plain_text = @dec_text

     PRINT '--Result:  PLAIN TEXT IS EQUAL TO DECRYPTED TEXT'

    ELSE

     PRINT '--Result:  PLAIN TEXT IS NOT EQUAL TO DECRYPTED TEXT'

    PRINT '-----------------------------------------'

    Like I said, this is a temporary work-around until I can get the update posted.  I apologize for the inconvenience.