欢迎光临
我们一直在努力

使用ASP加密算法加密你的数据(二)-ASP教程,安全加密

建站超值云服务器,限时71元/月

在第一部分,讨论了如何生成密钥,下面将介绍如何使用这个密钥来加密和解密一个字符串。

  下面的代码就是能够同时实现这个功能的函数

crypt.asp文件

<%

dim g_key

const g_cryptthis = "now is the time for

all good men to come to the aid of their country."

const g_keylocation = "c:\key.txt"

g_key = mid(readkeyfromfile(g_keylocation),1,len(g_cryptthis))

response.write "<p>original string: " & g_cryptthis & "<p>"

response.write "<p>key value: " & g_key & "<p>"

response.write "<p>encrypted cyphertext: " & encrypt(g_cryptthis) & "<p>"

response.write "<p>decrypted cyphertext: " & decrypt(encrypt(g_cryptthis)) & "<p>"

function encrypt(strcryptthis)

dim strchar, ikeychar, istringchar, i

for i = 1 to len(strcryptthis)

ikeychar = asc(mid(g_key,i,1))

istringchar = asc(mid(strcryptthis,i,1))

*** uncomment below to encrypt with addition,

icryptchar = istringchar + ikeychar

icryptchar = ikeychar xor istringchar

strencrypted = strencrypted & chr(icryptchar)

next

encrypt = strencrypted

end function

function decrypt(strencrypted)

dim strchar, ikeychar, istringchar, i

for i = 1 to len(strencrypted)

ikeychar = (asc(mid(g_key,i,1)))

istringchar = asc(mid(strencrypted,i,1))

*** uncomment below to decrypt with subtraction

idecryptchar = istringchar – ikeychar

idecryptchar = ikeychar xor istringchar

strdecrypted = strdecrypted & chr(idecryptchar)

next

decrypt = strdecrypted

end function

function readkeyfromfile(strfilename)

dim keyfile, fso, f

set fso = server.createobject("scripting.filesystemobject")

set f = fso.getfile(strfilename)

set ts = f.openastextstream(1, -2)

do while not ts.atendofstream

keyfile = keyfile & ts.readline

loop

readkeyfromfile = keyfile

end function

%>

  在crypt.asp中我们首先从密钥文件中得到密钥值,然后从这段密钥中截取和我们需要加密的明文同样长度的密钥。然后使用一个简单的异或操作将明文和密钥进行运算,那么得到的结果就是加密后的密文了。过程很简单的。由于是使用了异或操作,所以解密将非常简单,只要使用同样的密钥对密文再次进行异或操作就能够解密了。在上面介绍的基础上,你可以少加改动,就可以使用同样的方法加密一个文件。唯一需要注意的是,对于一个二进制文件,你需要做一些完整性检查以保证转换回来

的字符不要越界。现在你需要做的就是把密钥保存在服务器上的一个安全的地方(不能够被外部访问)

附注:

  vernam密码是由gilbert vernam (他是at&t的工程师)在1918年发明的。这是一种使用异或方法进行加密解密的方法。

赞(0)
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com 特别注意:本站所有转载文章言论不代表本站观点! 本站所提供的图片等素材,版权归原作者所有,如需使用,请与原作者联系。未经允许不得转载:IDC资讯中心 » 使用ASP加密算法加密你的数据(二)-ASP教程,安全加密
分享到: 更多 (0)

相关推荐

  • 暂无文章