弱弱的- -!问题一下 我在我的SQL里,发里,字母 z 经过软件加密以后值为 6a9037n7ylaqouitzspoos86kw3n48我通过SQL2005 的函数,类似select right(sys.fn_VarBinToHexStr(hashbytes('MD5', 'z')),30)算出来对不上,问问大家这是怎么算出来的啊,还有能反算吗?
望 大家赐教,谢谢!

解决方案 »

  1.   

     ----------------------------------------------------------------
    create function sys.fn_varbintohexstr 
    (
        @pbinin varbinary(max) 
    )
    returns nvarchar(max)
    as
    begin
        return sys.fn_varbintohexsubstring(1,@pbinin,1,0)
    end
    ----------------------------------------------------------------
    create function sys.fn_varbintohexsubstring (
    @fsetprefix bit = 1 -- append '0x' to the output
    ,@pbinin varbinary(max) -- input binary stream
    ,@startoffset int = 1  -- starting offset 
    ,@cbytesin int = 0  -- length of input to consider, 0 means total length
    )
    returns nvarchar(max)
    as
    begin
    declare @pstrout nvarchar(max)
    ,@i int
    ,@firstnibble int
    ,@secondnibble int
    ,@tempint int
    ,@hexstring char(16) --
    -- initialize and validate
    --
    if (@pbinin IS NOT NULL)
    begin
    select @i = 0
    ,@cbytesin = case when (@cbytesin > 0) then @cbytesin else DATALENGTH(@pbinin) end
    ,@pstrout =  case when (@fsetprefix = 1) then N'0x' else N'' end
    ,@hexstring = '0123456789abcdef' if ( ((@cbytesin * 2) + 2 > 4000) or ((@cbytesin * 2) + 2 < 1) )
    return NULL if ( ( @startoffset > DATALENGTH(@pbinin) ) or ( @startoffset < 1 ) )
    return NULL --
    -- adjust the length to process based on start offset and
    -- total length
    --
    if ((DATALENGTH(@pbinin) - @startoffset + 1) < @cbytesin)
    select @cbytesin = DATALENGTH(@pbinin) - @startoffset + 1

    --
    -- do for each byte
    --
    while (@i < @cbytesin)
    begin
    --
    -- Each byte has two nibbles
    -- which we convert to character
    --
    select @tempint = cast(substring(@pbinin, @i + @startoffset, 1) as int)
    select @firstnibble = @tempint / 16
    select @secondnibble = @tempint % 16 --
    -- we need to do an explicit cast with substring 
    -- for proper string conversion. 
    --
    select @pstrout = @pstrout +
    cast(substring(@hexstring, (@firstnibble+1), 1) as nvarchar) +
    cast(substring(@hexstring, (@secondnibble+1), 1) as nvarchar) select @i = @i + 1
    end
    end

    -- All done
    return @pstrout
    end
      

  2.   

    select right(sys.fn_VarBinToHexStr(hashbytes('MD5', 'z')),32)