欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  数据库

UTF8编码的Base64解密 MSSQL实现

程序员文章站 2022-05-23 16:41:30
...

加密解密UTF8编码的BASE64串 无 GOCREATE FUNCTION [dbo].[c_GetUTF8Code] ( @char Nchar )RETURNS intAS--UTF8转码BEGIN Declare @Code int Select @Code=Cast(Unicode(@char) as int) Declare @Utf8Code int Set @Utf8Code=0 if(@Code128) begin --0-127 --

加密解密UTF8编码的BASE64串 UTF8编码的Base64解密 MSSQL实现
GO

CREATE FUNCTION [dbo].[c_GetUTF8Code]  
   (  
       @char Nchar
   )
RETURNS int
AS

--UTF8转码
BEGIN
 Declare @Code int
 Select @Code=Cast(Unicode(@char) as int)
 Declare @Utf8Code int
 Set @Utf8Code=0
 if(@Code127 and @Code2047 and @Code127 and @CodeInt2047)
  begin
   --+3位
   if(@RestTransferLenth=0)
   begin
    Set @CodeTransfer=@CodeInt
    Set @RestTransfer=0
    Set @RestTransferLenth=0
   end
   else if(@RestTransferLenth=1)
   begin
    Set @CodeTransfer=0x10000*@RestTransfer+@CodeInt/0x100
    Set @RestTransfer=@CodeInt%0x100
    Set @RestTransferLenth=1
   end
   else if(@RestTransferLenth=2)
   begin
    --剩余部分十六进制右移两位与新数据前两位之和
    Set @CodeTransfer=0x100*@RestTransfer+@CodeInt/0x10000
    Set @RestTransfer=@CodeInt%0x10000
    Set @RestTransferLenth=2
   end
  end
  ---累积到3位,执行加密转换
  if(@CodeTransfer>0x100000)
  begin
   SET @block_val = CAST(@CodeTransfer AS BINARY(3))  
   SET @output = @output 
   + SUBSTRING(@map , @block_val/262144  +1,1)
   + SUBSTRING(@map ,(@block_val/4096&63)+1,1)
   + SUBSTRING(@map ,(@block_val/64  &63)+1,1)
   + SUBSTRING(@map ,(@block_val&63)     +1,1)
  end
  SET @block_start=@block_start+1  
 END  
 IF @RestTransferLenth>0  
  BEGIN  
  
  SET @block_val=Cast(@RestTransfer*(Case @RestTransferLenth When 1 Then 65536 Else 256 end) as BINARY(3))
  SET @output=@output  
   +SUBSTRING(@map , @block_val/262144+1,    1)  
   +SUBSTRING(@map ,(@block_val/4096  &63)+1,1)  
   +CASE WHEN @RestTransferLenth =1
   THEN REPLACE(SUBSTRING(@map ,(@block_val/64&63)+1,1),'A','=')  
   ELSE SUBSTRING(@map ,(@block_val/64&63)+1,1)
    END
   +CASE WHEN @RestTransferLenth=1  
   THEN '='  
   ELSE REPLACE(SUBSTRING(@map ,(@block_val&63)+1,1),'A','=')
    END  
  END
 RETURN @output  
END

 

 

 






GO

CREATE FUNCTION [dbo].[base64_utf8decode]  
   (  
       @encoded_text varchar(max)  
   )  
   RETURNS varchar(max)  
   AS BEGIN 

--BASE64加密
DECLARE @output varchar(max)
DECLARE @block_start int
DECLARE @encoded_length int
DECLARE @decoded_length int 
DECLARE @mapr binary(122)  
SET @output = ''  
SET @mapr =  
  0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF    --    1-33  
  +0xFFFFFFFFFFFFFFFFFFFF3EFFFFFF3F3435363738393A3B3C3DFFFFFF00FFFFFF    --    33-64  
  +0x000102030405060708090A0B0C0D0E0F10111213141516171819FFFFFFFFFFFF    --    65-96  
  +0x1A1B1C1D1E1F202122232425262728292A2B2C2D2E2F30313233--    97-122  
SET @encoded_length=LEN(@encoded_text)  
SET @decoded_length=@encoded_length/4*3
SET @block_start=1  
Declare @Code int
Set @Code=0
Declare @CodeLength int--累计连接数,1,2,3
Set @CodeLength =0
WHILE @block_start