首頁 資料庫 mysql教程 UTF8编码的Base64解密 MSSQL实现

UTF8编码的Base64解密 MSSQL实现

Jun 07, 2016 pm 02:57 PM
base64 mssql utf8 實現 編碼 解密

加密解密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(@Code<128)
 begin
  --0-127
  --0000-007F
  --0xxxxxxx
  --01100010 Unocide
  --01100010 UTF-8
  Set @Utf8Code=@Code 
 end
 else if(@Code>127 and @Code<2048)
 begin
  --128-2047
  --0080-07FF
  --110xxx xx10xx xxxx
  --110  7      F    F
  Declare @C1 int
  Declare @C2 int
  Declare @C3 int
  Select @C1=@Code/0x100 
  Select @C2=(@Code%0x100)/0x10
  Select @C3=@Code%0x10
  Select @Utf8Code=0xC080+0x400*@C1+0x100*(@C2/4)+0x10*(@C2%4)+@C3
 end
 else if(@Code>2047 and @Code<65536)
 begin
  --2047-65535
  --0110 0010 0001 0001
  --1110 xxxx 10xx xxxx 10xx xxxx
  --1110 0110 1000 1000 1001 0001
  Declare @C11 int
  Declare @C12 int
  Declare @C13 int
  Declare @C14 int
  Select @C11=@Code/0x1000
  Select @C12=(@Code%0x1000)/0x100
  Select @C13=(@Code%0x100)/0x10
  Select @C14=@Code%0x10
  Select @Utf8Code=0xE08080+0x10000*@C11+0x400*@C12+0x100*(@C13/4)+0x10*(@C13%4)+@C14 
 end
 return @Utf8Code
End

 

 

GO

CREATE FUNCTION [dbo].[base64_utf8encode]  
(  
 @plain_text varchar(max)  
)  
RETURNS varchar(max)  
AS BEGIN

--Base64解密
 DECLARE @output varchar(max)
 DECLARE @block_start integer
 DECLARE @map char(64)  
 SET @map='ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/'  
 SET @output=''
 SET @block_start=0
 Declare @plain_textLength int
 Set @plain_textLength=Len(@plain_text)
 Declare @RestTransfer int--转码数累积
 Declare @RestTransferLenth int
 Set @RestTransfer=0
 Set @RestTransferLenth=0
 Declare @CodeInt int
 Declare @block_val BINARY(3)
 WHILE @block_start<@plain_textLength
 BEGIN  
  Set @CodeInt=0
  SELECT @CodeInt= [dbo].[c_GetUTF8Code](SubString(@plain_text,@block_start+1,1))
  Declare @CodeTransfer int
  Set @CodeTransfer=0
  --0-127 1位
  --128-2047 2位
  --2047-65535 3位
  if(@CodeInt<128)
  begin
   --+1位
   if(@RestTransferLenth=0 or @RestTransferLenth=1)
   begin
    Set @RestTransfer=@RestTransfer*0x100+@CodeInt
    Set @RestTransferLenth=@RestTransferLenth+1
   end
   else if(@RestTransferLenth=2)
   begin
    Set @CodeTransfer=@RestTransfer*0x100+@CodeInt
    Set @RestTransfer=0
    Set @RestTransferLenth=0
   end
  end
  else if(@CodeInt>127 and @CodeInt<2048)
  begin
   --+2位
   if(@RestTransferLenth=0)
   begin
    Set @RestTransfer=@CodeInt
    Set @RestTransferLenth=2
   end
   else if(@RestTransferLenth=1)
   begin
    Set @CodeTransfer=0x10000*@RestTransfer+@CodeInt
    Set @RestTransfer=0
    Set @RestTransferLenth=0
   end
   else if(@RestTransferLenth=2)
   begin
    Set @CodeTransfer=0x100*@RestTransfer+@CodeInt/0x100
    Set @RestTransfer=@CodeInt%0x100
    Set @RestTransferLenth=1
   end
  end
  else if(@CodeInt>2047)
  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<@encoded_length
BEGIN
 Declare @Integer Integer
 Set @Integer=substring(@mapr,Unicode(substring(@encoded_text,@block_start  ,1)),1)*262144  
     + substring(@mapr,Unicode(substring(@encoded_text,@block_start+1,1)),1)*4096  
     + substring(@mapr,Unicode(substring(@encoded_text,@block_start+2,1)),1)*64  
     + substring(@mapr,Unicode(substring(@encoded_text,@block_start+3,1)),1)
 Declare @C1 int
 Declare @C2 int
 Declare @C3 int
 --0xFF FF FF
 Set @C1=@Integer/0x10000
 Set @C2=(@Integer/0x100)%0x100
 Set @C3=@Integer%0x100
 -------------------------------------@C1
 if(@C1<0x80)
 begin
  if(@CodeLength=2)
  begin
   --128-2047
   --0080-07FF
   --110x xx xx 10xx xxxx
   Set @Code=((@Code%0x2000)/0x100)*0x10+@Code%0x40
   SET @output=@output+NCHAR(@Code)
   --print @Code 
   Set @Code=0
   Set @CodeLength=0
  end
  SET @output=@output+CAST(Cast(@C1 AS BINARY(1))AS VARCHAR(1))
 end
 else
 begin
  --码字连接
  Set @Code=@Code*0x100+@C1
  SET @CodeLength=@CodeLength+1
  if(@CodeLength=3)
  begin
   --0110 0010 0001 0001
   --1110 xxxx 10xx xxxx 10xx xxxx
   --1110 0110 1000 1000 1001 0001
   Set @Code=((@Code%0x100000)/0x10000)*0x1000+((@Code%0x4000)/0x100)*0x40+@Code%0x40
   SET @output=@output+NCHAR(@Code)
   Set @Code=0
   Set @CodeLength=0
  end
 end
 -------------------------------------@C2
 if(@C2<0x80)
 begin
  if(@CodeLength=2)
  begin
   --128-2047
   --0080-07FF
   --110x xx xx 10xx xxxx
   Set @Code=((@Code%0x2000)/0x100)*0x10+@Code%0x40
   SET @output=@output+NCHAR(@Code)
   --print @Code 
   Set @Code=0
   Set @CodeLength=0
  end
  SET @output=@output+CAST(Cast(@C2 AS BINARY(1))AS VARCHAR(1))
 end
 else
 begin
  --码字连接
  Set @Code=@Code*0x100+@C2
  SET @CodeLength=@CodeLength+1
  if(@CodeLength=3)
  begin
   --0110 0010 0001 0001
   --1110 xxxx 10xx xxxx 10xx xxxx
   --1110 0110 1000 1000 1001 0001
   Set @Code=((@Code%0x100000)/0x10000)*0x1000+((@Code%0x4000)/0x100)*0x40+@Code%0x40
   SET @output=@output+NCHAR(@Code)
   Set @Code=0
   Set @CodeLength=0
  end
 end
 -------------------------------------@C3
 if(@C3<0x80)
 begin
  if(@CodeLength=2)
  begin
   --128-2047
   --0080-07FF
   --110x xx xx 10xx xxxx
   Set @Code=((@Code%0x2000)/0x100)*0x10+@Code%0x40
   SET @output=@output+NCHAR(@Code)
   --print @Code 
   Set @Code=0
   Set @CodeLength=0
  end
  SET @output=@output+CAST(Cast(@C3 AS BINARY(1))AS VARCHAR(1))
 end
 else
 begin
  --码字连接
  Set @Code=@Code*0x100+@C3
  SET @CodeLength=@CodeLength+1
  if(@CodeLength=3)
  begin
   --0110 0010 0001 0001
   --1110 xxxx 10xx xxxx 10xx xxxx
   --1110 0110 1000 1000 1001 0001
   Set @Code=((@Code%0x100000)/0x10000)*0x1000+((@Code%0x4000)/0x100)*0x40+@Code%0x40
   SET @output=@output+NCHAR(@Code)
   Set @Code=0
   Set @CodeLength=0
  end
 end
 SET @block_start = @block_start + 4  
END  
IF RIGHT(@encoded_text,2)='=='
 SET @decoded_length=@decoded_length-2
ELSE IF RIGHT(@encoded_text,1)='='
 SET @decoded_length=@decoded_length-1
RETURN LEFT(@output ,@decoded_length)  
END 

登入後複製
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

Video Face Swap

Video Face Swap

使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱工具

記事本++7.3.1

記事本++7.3.1

好用且免費的程式碼編輯器

SublimeText3漢化版

SublimeText3漢化版

中文版,非常好用

禪工作室 13.0.1

禪工作室 13.0.1

強大的PHP整合開發環境

Dreamweaver CS6

Dreamweaver CS6

視覺化網頁開發工具

SublimeText3 Mac版

SublimeText3 Mac版

神級程式碼編輯軟體(SublimeText3)

熱門話題

Java教學
1664
14
CakePHP 教程
1421
52
Laravel 教程
1316
25
PHP教程
1266
29
C# 教程
1239
24
華為手機如何實現雙微信登入? 華為手機如何實現雙微信登入? Mar 24, 2024 am 11:27 AM

華為手機如何實現雙微信登入?隨著社群媒體的興起,微信已成為人們日常生活中不可或缺的溝通工具之一。然而,許多人可能會遇到一個問題:在同一部手機上同時登入多個微信帳號。對於華為手機用戶來說,實現雙微信登入並不困難,本文將介紹華為手機如何實現雙微信登入的方法。首先,華為手機自帶的EMUI系統提供了一個很方便的功能-應用程式雙開。透過應用程式雙開功能,用戶可以在手機上同

PHP程式設計指南:實作斐波那契數列的方法 PHP程式設計指南:實作斐波那契數列的方法 Mar 20, 2024 pm 04:54 PM

程式語言PHP是一種用於Web開發的強大工具,能夠支援多種不同的程式設計邏輯和演算法。其中,實作斐波那契數列是一個常見且經典的程式設計問題。在這篇文章中,將介紹如何使用PHP程式語言來實作斐波那契數列的方法,並附上具體的程式碼範例。斐波那契數列是一個數學上的序列,其定義如下:數列的第一個和第二個元素為1,從第三個元素開始,每個元素的值等於前兩個元素的和。數列的前幾元

Ubuntu安裝PHP並設定MSSQL連線的詳細指南 Ubuntu安裝PHP並設定MSSQL連線的詳細指南 Feb 29, 2024 am 11:15 AM

Ubuntu是一款流行的開源作業系統,常用於伺服器運作。在Ubuntu上安裝PHP並設定MSSQL連線是許多開發者和系統管理員經常需要做的操作之一。本文將為讀者提供一份詳細的指南,步驟包含安裝PHP、設定Apache、安裝MSSQLServer等內容,並附上具體的程式碼範例。第一步:安裝PHP及相關擴展首先,我們需要安裝PHP及其相關擴展,以便支援PHP連接

如何在華為手機上實現微信分身功能 如何在華為手機上實現微信分身功能 Mar 24, 2024 pm 06:03 PM

如何在華為手機上實現微信分身功能隨著社群軟體的普及和人們對隱私安全的日益重視,微信分身功能逐漸成為人們關注的焦點。微信分身功能可以幫助使用者在同一台手機上同時登入多個微信帳號,方便管理和使用。在華為手機上實現微信分身功能並不困難,只需要按照以下步驟操作即可。第一步:確保手機系統版本和微信版本符合要求首先,確保你的華為手機系統版本已更新至最新版本,以及微信App

掌握Golang如何實現遊戲開發的可能性 掌握Golang如何實現遊戲開發的可能性 Mar 16, 2024 pm 12:57 PM

在現今的軟體開發領域中,Golang(Go語言)作為一種高效、簡潔、並發性強的程式語言,越來越受到開發者的青睞。其豐富的標準庫和高效的並發特性使它成為遊戲開發領域的一個備受關注的選擇。本文將探討如何利用Golang來實現遊戲開發,並透過具體的程式碼範例來展示其強大的可能性。 1.Golang在遊戲開發中的優勢作為靜態類型語言,Golang正在建構大型遊戲系統

PHP遊戲需求實作指南 PHP遊戲需求實作指南 Mar 11, 2024 am 08:45 AM

PHP遊戲需求實現指南隨著網路的普及和發展,網頁遊戲的市場也越來越火爆。許多開發者希望利用PHP語言來開發自己的網頁遊戲,而實現遊戲需求是其中一個關鍵步驟。本文將介紹如何利用PHP語言來實現常見的遊戲需求,並提供具體的程式碼範例。 1.創造遊戲角色在網頁遊戲中,遊戲角色是非常重要的元素。我們需要定義遊戲角色的屬性,例如姓名、等級、經驗值等,並提供方法來操作這些

word解密怎麼設定 word解密怎麼設定 Mar 20, 2024 pm 04:36 PM

現今的工作環境中,大家的保密意識越來越強了,在使用軟體的時候也常常進行加密操作,對文件進行保護。尤其是重點的文件,保密意識更要增加,時時刻刻將文件的安全性放在首要位置。那麼關於word解密不知道大家理解得怎麼樣,具體該如何操作?今天我們就透過下文的講解為大家實際展示一下關於word解密的過程,需要學習word解密知識的小夥伴不要錯過今天的課程。首先需要進行解密操作來保護文件,這意味著對文件進行了保護文檔處理。在對文件進行此處理後,再次開啟文件會彈出提示。解密檔案的方法是輸入密碼,這樣就可以直接

Ubuntu環境下安裝PHP支援MSSQL資料庫的步驟詳解 Ubuntu環境下安裝PHP支援MSSQL資料庫的步驟詳解 Feb 29, 2024 am 10:39 AM

在Ubuntu環境下安裝PHP支援MSSQL資料庫的步驟詳解在開發Web應用程式時,常會遇到需要連接MicrosoftSQLServer(MSSQL)資料庫的情況。在Ubuntu環境下,要實現PHP對MSSQL資料庫的連接,需要安裝相關的軟體以及配置適當的設定。接下來,將詳細介紹在Ubuntu環境下安裝PHP支援MSSQL資料庫的步驟,並提供具體的程式碼

See all articles