UTF8 encoded Base64 decryption MSSQL implementation

Source: Internet
Author: User
Encryption and decryption UTF8 encoded BASE64 string without GOCREATEFUNCTION [dbo]. [c_GetUTF8Code] (@ charNchar) RETURNSintAS -- UTF8 transcoding BEGINDeclare @ CodeintSelect @ CodeCast (Unicode (@ char) asint) Declare @ Utf8CodeintSet @ Utf8Code0if (@ Code128) begin -- 0-127 --

The UTF-8 encoded BASE64 string is encrypted and decrypted without the gocreate function [dbo]. [c_GetUTF8Code] (@ char Nchar) RETURNS intAS -- UTF8 transcoding BEGIN Declare @ Code int Select @ Code = Cast (Unicode (@ char) as int) declare @ Utf8Code int Set @ Utf8Code = 0 if (@ Code128) begin -- 0-127 --

UTF-8 encoded BASE64 string for encryption and decryption <无>
Gocreate function [dbo]. [c_GetUTF8Code] (@ char Nchar) RETURNS intAS -- UTF8 transcoding 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 -- 0 xxxxxxx -- 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 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 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 @ Utf8CodeEnd gocreate function [dbo]. [base64_utf8encode] (@ plain_text varchar (max) RETURNS varchar (max) as begin -- Base64 decrypt DECLARE @ output varchar (max) DECLARE @ block_start integer DECLARE @ map char (64) SET @ map = 'prop +/'SET @ output = ''SET @ block_start = 0 Declare @ plain_textLength int Set @ plain_textLength = Len (@ plain_text) declare @ RestTransfer int -- Cumulative transcoding count 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-bit -- 128-2047 2-2047 3-bit if (@ CodeInt <65535) begin -- + 1-bit 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 else if (@ CodeInt> 127 and @ CodeInt <2048) begin -- + 2-bit 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 else if (@ CodeInt> 2047) begin -- + 3-bit 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 -- the remaining part shifts the sum of the two digits to the first two digits of the new data in hexadecimal notation: Set @ CodeTransfer = 0x100 * @ RestTransfer + @ CodeInt/0x10000 Set @ RestTransfer = @ CodeInt % 0x10000 set @ RestTransferLenth = 2 end --- accumulate to 3 bits, execute encrypted conversion if (@ CodeTransfer> 0x100000) begin SET @ block_val = CAST (@ CodeTransfer as binary (3) SET @ output = @ output + SUBSTRING (@ map, @ block_val/262144 + 4096) + SUBSTRING (@ map, (@ block_val/& 63) + 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) + case when @ RestTransferLenth = 1 then replace (SUBSTRING (@ map, (@ block_val/64 & 63) + ), '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 return @ output end gocreate function [dbo]. [base64_utf8decode] (@ encoded_text varchar (max) RETURNS varchar (max) as begin -- BASE64 encrypted DECLARE @ output varchar (max) DECLARE @ block_start intDECLARE @ encoded_length intDECLARE @ decoded_length int DECLARE @ mapr binary (122) SET @ output = ''SET @ mapr = 0 rows -- 1-33 + rows -- 33-64 + rows -- 65-96 + rows -- 97-122 SET @ encoded_length = LEN (@ encoded_text) SET @ decoded_length = @ encoded_length/4*3 SET @ block_start = 1 Declare @ Code intSet @ Code = 0 Declare @ CodeLength int -- Cumulative connections, 1, 2, 3 Set @ CodeLength = 0 WHILE @ block_start <@ encoded_lengthBEGIN Declare @ Integer Set @ Integer = substring (@ mapr, Unicode (substring (@ encoded_text, @ block_start, 1 )), 1) * 262144 + substring (@ mapr, Unicode (substring (@ encoded_text, @ block_start + 4096), 1) * + 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 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 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 -- codeword connection 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 Set @ Code = (@ Code % 0x0001)/0x100000) * 0x1000 + (@ Code % 0x4000)/0x100) * 0x40 + @ Code % 0x40 SET @ output = @ output + NCHAR (@ Code) set @ Code = 0 Set @ CodeLength = 0 end --------------------------------------- @ C2 if (@ C2 <0x80) begin if (@ CodeLength = 2) begin -- 128-2047 -- 0080-07FF -- 110x 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 -- codeword connection 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 Set @ Code = (@ Code % 0x0001)/0x100000) * 0x1000 + (@ Code % 0x4000)/0x100) * 0x40 + @ Code % 0x40 SET @ output = @ output + NCHAR (@ Code) set @ Code = 0 Set @ CodeLength = 0 end --------------------------------------- @ C3 if (@ C3 <0x80) begin if (@ CodeLength = 2) begin -- 128-2047 -- 0080-07FF -- 110x 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 -- codeword connection 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 Set @ Code = (@ Code % 0x0001)/0x100000) * 0x1000 + (@ Code % 0x4000)/0x100) * 0x40 + @ Code % 0x40 SET @ output = @ output + NCHAR (@ Code) set @ Code = 0 Set @ CodeLength = 0 end SET @ block_start = @ block_start + 4 end if right (@ encoded_text, 2) = 'SET @ decoded_length = @ decoded_length-2ELSE if right (@ encoded_text, 1) =' SET @ decoded_length = @ decoded_length-1RETURN LEFT (@ output, @ decoded_length) END

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.