对字符串进行加密解密

create view v_rand

as

select c=unicode(cast(round(rand()*255,0) as tinyint))

Go

create function f_jmstr

(

@str varchar(8000),

@type bit

)

returns varchar(8000)

/*

*参数说明

*str:要加密的字符串或已经加密后的字符

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

*返回值说明

*当操作类型为加密时(type--0):返回为加密后的str,即存放于数据库中的字符串

*当操作类型为解密时(type--1):返回为实际字符串,即加密字符串解密后的原来字符串

*/

As

begin

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)[email protected]

,@[email protected]

set @[email protected]+nchar(@c)

end

else--解密

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)[email protected] ,@[email protected]

end

return(@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

*/

时间: 2024-12-06 21:02:48

对字符串进行加密解密的相关文章

Java小案例——对字符串进行加密解密

 要求:  *  对用户输入的每个字符的值进行加密,将解密后的字符串输出  *  对用户输入的已加密字符串进行解密并输出 实现代码: import java.util.Scanner; /** * 要求: * 1.对用户输入的每个字符的值进行加密,将解密后的字符串输出 * 2.对用户输入的已加密字符串进行解密并输出 * @author Administration * */ public class Encryption { public static void main(String[] ar

java 字符串 asc 加密解密

package com; public class MD5Test { /** * @param args */ public static void main(String[] args) { System.out.println(decrypt("137%128%143%145%124%144%135%143%76%")); System.out.println(encrypt("frontdev")); } /** *用户名解密 *@param ssoToke

Python_字符串简单加密解密

1 def crypt(source,key): 2 from itertools import cycle 3 result='' 4 temp=cycle(key) 5 for ch in source: 6 result=result+chr(ord(ch)^ord(next(temp))) 7 return result 8 9 source='Jiangxi Insstitute of Busiess and Technology' 10 key='zWrite' 11 12 prin

ASP.NET数据库连接字符串的加密与解密

ASP.NET web.config中,数据库连接字符串的加密与解密. 虽然不怎么新鲜,但相信还是有许多人不知道,好,不说废话,直接给方法:开始--->运行,输入cmd,接着输入以下内容 加密: C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\aspnet_regiis.exe -pef "connectionStrings" "你的Web项目路径" 解密: C:\WINDOWS\Microsoft.NET\Fram

C#DES加密解密字符串

1.添加引用 using System.Security.Cryptography; using System.IO; 2.添加默认密匙向量 //默认密钥向量 private static byte[] Keys = { 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF }; 3.添加加密解密类 /// <summary> /// DES加密字符串 /// </summary> /// <param name="encry

字符串加密解密

字符串加密(指定加密密钥) 如字符串"Good good study,day day up! You can you up,no can no bibi!",加密密钥为"marchfour",对字符串进行加密 原理很简单,就是字符之间的异或 错误的代码: 1 #include<stdio.h> 2 #include<stdlib.h> 3 #include<string.h> 4 void encrypt_decode(char

php 加密解密字符串

/********************************************************************* 函数名称:encrypt 函数作用:加密解密字符串 使用方法: 加密 :encrypt('str','E','nowamagic'); 解密 :encrypt('被加密过的字符串','D','nowamagic'); 参数说明: $string :需要加密解密的字符串 $operation:判断是加密还是解密:E:加密 D:解密 $key :加密的钥匙(密

DES加密解密字符串的JAVA实现(lp)

野火烧不尽,春风吹又生.众里寻他千百度,蓦然回首,那人却在灯火阑珊处.天作孽,犹可违,自作孽,不可活.山高月小,水落石出.晴川历历汉阳树,芳草萋萋鹦鹉洲. package test.des2.tt; /** * * 这个是真正实用的.修正了以前方法的只加密了前8位正确的bug. */ import java.security.*; import javax.crypto.*; public class DESPlus { private static String strDefaultKey =

C#一个字符串的加密与解密

using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Security.Cryptography; using System.IO;   namespace ConsoleApplication1 {     class Program     {         static string encryptKey = "Oyea";    //