教你怎樣實現SQL密文加密,不可反編譯

本文介绍了一个SQL自定义函数fn_ACITEncryption,该函数用于在SQL环境中实现字符串的加密与解密操作。通过输入指定的字符串、操作标志及密钥,可以完成数据的加密或解密过程。此函数利用字符的Unicode值进行位运算来达到加密效果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

<!-- [if gte mso 9]><xml> <w:WordDocument> <w:View>Normal</w:View> <w:Zoom>0</w:Zoom> <w:PunctuationKerning/> <w:DisplayHorizontalDrawingGridEvery>0</w:DisplayHorizontalDrawingGridEvery> <w:DisplayVerticalDrawingGridEvery>2</w:DisplayVerticalDrawingGridEvery> <w:ValidateAgainstSchemas/> <w:SaveIfXMLInvalid>false</w:SaveIfXMLInvalid> <w:IgnoreMixedContent>false</w:IgnoreMixedContent> <w:AlwaysShowPlaceholderText>false</w:AlwaysShowPlaceholderText> <w:Compatibility> <w:SpaceForUL/> <w:BalanceSingleByteDoubleByteWidth/> <w:DoNotLeaveBackslashAlone/> <w:ULTrailSpace/> <w:DoNotExpandShiftReturn/> <w:AdjustLineHeightInTable/> <w:BreakWrappedTables/> <w:SnapToGridInCell/> <w:WrapTextWithPunct/> <w:UseAsianBreakRules/> <w:DontGrowAutofit/> <w:UseFELayout/> </w:Compatibility> <w:BrowserLevel>MicrosoftInternetExplorer4</w:BrowserLevel> </w:WordDocument> </xml><![endif]--><!-- [if gte mso 9]><xml> <w:LatentStyles DefLockedState="false" LatentStyleCount="156"> </w:LatentStyles> </xml><![endif]--><!-- [if !mso]> <object classid="clsid:38481807-CA0E-42D2-BF39-B33AF135CC4D" id=ieooui> </object> <mce:style><!-- st1/:*{behavior:url(#ieooui) } --> <!-- [endif]--> <!-- [if gte mso 10]> <mce:style><!-- /* Style Definitions */ table.MsoNormalTable {mso-style-name:表格內文; mso-tstyle-rowband-size:0; mso-tstyle-colband-size:0; mso-style-noshow:yes; mso-style-parent:""; mso-padding-alt:0cm 5.4pt 0cm 5.4pt; mso-para-margin:0cm; mso-para-margin-bottom:.0001pt; mso-pagination:widow-orphan; font-size:10.0pt; font-family:"Times New Roman"; mso-fareast-font-family:"Times New Roman"; mso-ansi-language:#0400; mso-fareast-language:#0400; mso-bidi-language:#0400;} --> <!-- [endif]-->

use Tempdb

go

if object_ID ( 'fn_ACITEncryption' ) is not null

drop function fn_ACITEncryption

go

create function fn_ACITEncryption

(

@Str nvarchar ( 4000), -- 加密的字符串

@Flag bit = 1, --1 、加密 0 、解密

@Key nvarchar ( 50) -- 密文

)

returns nvarchar ( 4000) -- 這里可轉換成二進制

with Encryption

as

begin

Declare @LenStr int , @i int , @Str2 nvarchar ( 4000), @Split nvarchar ( 2), @LenKey int

select @Str= @Str+ 'A' , @LenStr= len ( @Str), @i= 1, @Str2= '' , @LenKey= Len ( @Key+ 'A' )- 1

while @i< @LenStr

select @Split= substring ( @Str, @i, 1),

@Split= nchar (( unicode ( @Split)+ case @Flag when 1 then unicode ( substring ( @Key+ 'A' , @i% @LenKey+ 1, 1))- 1

when 0 then 65535- unicode ( substring ( @Key+ 'A' , @i% @LenKey+ 1, 1))

else 0 end )% 65535+ cast ( @Flag as int )),

@Str2= @Str2+ @Split, @i= @i+ 1

return @Str2

end

go

select dbo. fn_ACITEncryption( N'Roy' , 1, '123' ) as 加密后字符串

/*

加密后字符串

------------------------------

(1 個資料列受到影響 )

*/

select dbo. fn_ACITEncryption( N, 0, '123' ) as 解密后字符串

/*

解密后字符串

--------------------------

Roy

(1 個資料列受到影響 )

*/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值