MSSQL 调用C#程序集 实现C#字符串到字符的转化
10多年前用过MSSQL 调用C#程序集来实现数据的加密和解密,也搞过通过字符偏移实现简单的加密和解密。这次就总结一下吧:
C#如下:
public class CLRFunctions
{
/// <summary>
/// 数据加密
/// </summary>
/// <param name="target"></param>
/// <returns></returns> public static String DataEncrypt(string target, int keyLen)
{
if (string.IsNullOrEmpty(target)) return string.Empty;
string str = string.Empty;
char[] arr = target.ToCharArray();
for (int i = ; i < arr.Length; i++)
{
char c = arr[i]; str = str + ((char)(c + keyLen));
}
return str;
} /// <summary>
/// 数据解密
/// </summary>
/// <param name="target"></param>
/// <returns></returns> public static String DataDecrypt(string target, int keyLen)
{
if (string.IsNullOrEmpty(target)) return string.Empty;
string str = string.Empty;
char[] arr = target.ToCharArray();
for (int i = ; i < arr.Length; i++)
{
char c = arr[i];
str = str + ((char)(c - keyLen));
}
return str;
}
}
SQL如下,这里需要启用程序集功能,然后在创建程序集:
exec sp_configure 'show advanced options', '';
go
reconfigure;
go
exec sp_configure 'clr enabled', ''
go
reconfigure;
exec sp_configure 'show advanced options', '';
go if exists (select * from sys.assemblies where name='CLRFunctions')
drop assembly CLRFunctions;
go CREATE ASSEMBLY CLRFunctions FROM 'd:\sql\xxx.dll' ;
Go if exists(select from sysobjects where name='dbo.DataEncrypt' and xtype='FN')
begin
drop function dbo.DataEncrypt
end go CREATE FUNCTION dbo.DataEncrypt
(
@target as nvarchar (),
@keyLen as int
)
RETURNS nvarchar ()
AS EXTERNAL NAME CLRFunctions.CLRFunctions.DataEncrypt go
if exists(select from sysobjects where name='dbo.DataDecrypt' and xtype='FN')
begin
drop function dbo.DataDecrypt
end go CREATE FUNCTION dbo.DataDecrypt
(
@target as nvarchar (),
@keyLen as int
)
RETURNS nvarchar ()
AS EXTERNAL NAME CLRFunctions.CLRFunctions.DataDecrypt
后来尝试用SQL实现C#的ToCharArray方法,在实现字符偏移,SQL如下:
if exists(select from sysobjects where name='DataEncrypt' and xtype='FN')
begin
drop function dbo.DataEncrypt
end go
CREATE FUNCTION dbo.DataEncrypt(@target nvarchar (),@keyLen int)
RETURNS nvarchar ()
AS
BEGIN
DECLARE @position int,@ret nvarchar();
SET @position = ;
set @ret=''
WHILE @position <= len(@target)
BEGIN
set @ret=@ret+ nchar(unicode(SUBSTRING(@target, @position, ))+@keylen)
SET @position = @position +
END;
RETURN @ret
END go
if exists(select from sysobjects where name='DataDecrypt' and xtype='FN')
begin
drop function dbo.DataDecrypt
end go CREATE FUNCTION dbo.DataDecrypt(@target nvarchar (),@keyLen int)
RETURNS nvarchar ()
AS
BEGIN
DECLARE @position int,@ret nvarchar();
SET @position = ;
set @ret=''
WHILE @position <= len(@target)
BEGIN
set @ret=@ret+ nchar(unicode(SUBSTRING(@target, @position, ))-@keylen)
SET @position = @position +
END;
RETURN @ret
END
MSSQL 调用C#程序集 实现C#字符串到字符的转化的更多相关文章
- MSSQL 调用 .net 代码
http://www.cnblogs.com/laozhao8/p/3398681.html 在SQL Server中调用.NET程序集 需求是这样的,我在.net程序里操作数据时将一些字段数据加 ...
- c# 调用 C++ dll 传入传出 字符串
c# 调用 C++ dll 传入传出 字符串 2013-07-02 09:30 7898人阅读 评论(2) 收藏 举报 本文章已收录于: 分类: windows 版权声明:随便转载,随便使用. C ...
- The Swift Programming Language-官方教程精译Swift(4)字符串和字符
String 是一个有序的字符集合,例如 "hello, world", "albatross".Swift 字符串通过 String 类型来表示,也可以表示为 ...
- Swift语言指南(十)--字符串与字符
原文:Swift语言指南(十)--字符串与字符 字符串是一段字符的有序集合,如"hellow,world"或"信天翁".Swift 中的字符串由 String ...
- Swift 学习- 04 -- 字符串和字符
// 字符串 和 字符 // 字符串 是有序的 Character (字符) 类型的值的集合, 通过 String 类型的集合 // swift 的 String 和 Character 类型提供了 ...
- [精校版]The Swift Programming Language--语言指南--字符串和字符 (转)
今天装了10.10.马上就可以实际编写swift了.还是很兴奋啊. 哈哈.字符串和字符是大家最容易打交道的.今天就转一下讲解swift中字符串和字符的文章.希望对大家有帮助. 原文地址:http:// ...
- 5.Swift教程翻译系列——Swift字符串和字符
英文版PDF下载地址http://download.csdn.net/detail/tsingheng/7480427 字符串是一组字符的有序序列,比方"hello,china"或 ...
- UTF-8编码的字符串拆分成单字、获取UTF-8字符串的字符个数的代码及原理
一.字符编码简介 1. ASCII码 在计算机内部,所有的信息最终都表示为一个二进制的字符串.每一个二进制位(bit)有0和1两种状态,因此八个二进制位就可以组合出256种状态,这被称为一个字节(by ...
- Python中的字符串与字符编码
本节内容: 前言 相关概念 Python中的默认编码 Python2与Python3中对字符串的支持 字符编码转换 一.前言 Python中的字符编码是个老生常谈的话题,同行们都写过很多这方面的文章. ...
随机推荐
- web前端中navigator
<script> if(navigator.cookieEnabled){ document.write("浏览器已启用cookie,请妥善保存个人信息"); }els ...
- 浏览器iscroll
::-webkit-scrollbar{width:4px;height:4px;background:transparent}::-webkit-scrollbar-track{background ...
- 在github上创建新的分支(包括管理分支)
考虑到前面的项目在master分支上,这个不是太友好,下面在只有master分支的基础上,新建一个dev分支 一:查看 1.查看本地分支 git branch 2.查看远程分支 git branch ...
- day 68 django 之api操作 | jQueryset集合与对象
我们的orm里面分为: jQueryset集合, 还有对象, 我们的jqueryset集合里面可以有多个对象,这句话的意思就是我们的对象是最小的单位,不可以再拆分了,我们的jQueryset集合就相当 ...
- L - Ray in the tube Gym - 101911L (暴力)
---恢复内容开始--- You are given a tube which is reflective inside represented as two non-coinciding, but ...
- Pedestrian Attributes Recognition Paper List
Pedestrian Attributes Recognition Paper List 2018-12-22 22:08:55 [Note] you may also check the upda ...
- mybatis查询语句的背后之封装数据
转载请注明出处... 一.前言 继上一篇mybatis查询语句的背后,这一篇主要围绕着mybatis查询的后期操作,即跟数据库交互的时候.由于本人也是一边学习源码一边记录,内容难免有错误或不足之处,还 ...
- 835.Hamming距离
描述 两个整数的Hamming距离是对应比特位不同的个数. 给定两个整数x和y,计算两者的Hamming距离. 0 ≤ x, y < 2^31. 您在真实的面试中是否遇到过这个题? 样例 输入: ...
- Django视图层、虚拟环境
一.虚拟环境安装 目的:为了解决版本共存问题 ''' 1.通过pip3安装虚拟环境: -- pip3 install virtualenv 2.前往目标文件夹: -- cd 目标文件夹 (C:\Vir ...
- SQL Server中的标识列
一.标识列的定义以及特点 SQL Server中的标识列又称标识符列,习惯上又叫自增列. 该种列具有以下三种特点: .列的数据类型为不带小数的数值类型 .在进行插入(Insert)操作时,该列的值是由 ...