葉子函式分享五十六 對字串進行加密解密

2021-08-25 18:50:24 字數 1614 閱讀 6950

create

view v_rand as

select c=

unicode

(cast

(round

(rand

()*255,0)

astinyint

))go

create

function f_jmstr (

@str varchar

(8000),

@type bit)

returns

varchar

(8000)/*

*引數說明

*str:要加密的字串或已經加密後的字元

*type:操作型別--0加密--解密

*返回值說明

*當操作型別為加密時(type--0):返回為加密後的str,即存放於資料庫中的字串

*當操作型別為解密時(type--1):返回為實際字串,即加密字串解密後的原來字串 */

asbegin

declare @re varchar

(8000)

--返回值

declare @c int

--加密字元

declare @i int

/**加密方法為原字元異或乙個隨機ascii字元 */

if @type=0--加密

begin

select @c=c,@re=

'',@i=

len(@str)

from v_rand

while @i>0

select @re=

nchar

(unicode

(substring

(@str,@i,1))^@c^@i)+@re

,@i=@i-1

set @re=@re+

nchar

(@c)

endelse

--解密

begin

select @i=

len(@str)-1,@c=

unicode

(substring

(@str,@i+1,1)),@re=

''while @i>0

select @re=

nchar

(unicode

(substring

(@str,@i,1))^@c^@i)+@re ,@i=@i-1

endreturn

(@re)

end go

--測試

declare

@tempstr varchar

(20)

set@tempstr=

' 1 2 3aa'

select

'原始值:'

,@tempstr

select

'加密後:'

,dbo.f_jmstr(@tempstr,0)

select

'解密後:'

,dbo.f_jmstr(dbo.f_jmstr(@tempstr,0),1)

--輸出結果 /*

原始值: 1 2 3aa

加密後: __0'15`'17__°{1

解密後: 1 2 3aa */

葉子函式分享五十 無序字元比較函式

go 建立函式 第一版 create function get orderstr str varchar 8000 returns varchar 8000 as begin set str rtrim str declare tb table s varchar 1 a int while len...

葉子函式分享三十七 求字串中漢字的個數

一 分解字串法 首先建立這個函式 將字串分解 create function dbo splitchar str one nvarchar 100 returns result table col nvarchar 1 as begin declare number one int select n...

葉子函式分享四十六 獲取元素個數的函式

go create function getstrarrlength str varchar 8000 returns intas begin declare int return int declare start int declare next int declare location int...