加载中...
查看文章
  • 使用ASP加密算法加密你的数据(二)
  • 作者:Batman 来源:未知 添加时间:2003年3月6日 字体:
  • 83487Julain Sitkewich

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

    L0cK5Vp字符串。

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

    L0cK5VpCrypt.asp文件

    L0cK5Vp<%

    L0cK5VpDim g_Key

    L0cK5Vp

    L0cK5VpConst g_CryptThis = "Now is the time for all good men to come to the aid of their country."

    L0cK5VpConst g_KeyLocation = "c:\key.txt"

    L0cK5Vp

    L0cK5Vpg_Key = mid(ReadKeyFromFile(g_KeyLocation),1,Len(g_CryptThis))

    L0cK5Vp

    L0cK5VpResponse.Write "<p>ORIGINAL STRING: " & g_CryptThis & "<p>"

    L0cK5VpResponse.Write "<p>KEY &#118alue: " & g_Key  & "<p>"

    L0cK5VpResponse.Write "<p>ENCRYPTED CYPHERTEXT: " & EnCrypt(g_CryptThis) & "<p>"

    L0cK5VpResponse.Write "<p>DECRYPTED CYPHERTEXT: " & DeCrypt(EnCrypt(g_CryptThis)) & "<p>"

    L0cK5Vp

    L0cK5VpFunction EnCrypt(strCryptThis)

    L0cK5Vp   Dim strChar, iKeyChar, iStringChar, i

    L0cK5Vp   for i = 1 to Len(strCryptThis)

    L0cK5Vp      iKeyChar = Asc(mid(g_Key,i,1))

    L0cK5Vp      iStringChar = Asc(mid(strCryptThis,i,1))

    L0cK5Vp      ' *** uncomment below to encrypt with addition,

    L0cK5Vp      ' iCryptChar = iStringChar + iKeyChar

    L0cK5Vp      iCryptChar = iKeyChar Xor iStringChar

    L0cK5Vp      strEncrypted =  strEncrypted & Chr(iCryptChar)

    L0cK5Vp   next

    L0cK5Vp   EnCrypt = strEncrypted

    L0cK5VpEnd Function

    L0cK5Vp

    L0cK5VpFunction DeCrypt(strEncrypted)

    L0cK5VpDim strChar, iKeyChar, iStringChar, i

    L0cK5Vp   for i = 1 to Len(strEncrypted)

    L0cK5Vp      iKeyChar = (Asc(mid(g_Key,i,1)))

    L0cK5Vp      iStringChar = Asc(mid(strEncrypted,i,1))

    L0cK5Vp      ' *** uncomment below to decrypt with subtraction    

    L0cK5Vp      ' iDeCryptChar = iStringChar - iKeyChar

    L0cK5Vp      iDeCryptChar = iKeyChar Xor iStringChar

    L0cK5Vp      strDecrypted =  strDecrypted & Chr(iDeCryptChar)

    L0cK5Vp   next

    L0cK5Vp   DeCrypt = strDecrypted

    L0cK5VpEnd Function

    L0cK5Vp

    L0cK5VpFunction ReadKeyFromFile(strFileName)

    L0cK5Vp   Dim keyFile, fso, f

    L0cK5Vp   set fso = Server.CreateObject("Scripting.FileSystemObject")

    L0cK5Vp   set f = fso.GetFile(strFileName)

    L0cK5Vp   set ts = f.OpenAsTextStream(1, -2)

    L0cK5Vp

    L0cK5Vp   Do While not ts.AtEndOfStream

    L0cK5Vp     keyFile = keyFile & ts.ReadLine

    L0cK5Vp   Loop

    L0cK5Vp

    L0cK5Vp   ReadKeyFromFile =  keyFile

    L0cK5VpEnd Function

    L0cK5Vp

    L0cK5Vp%>  

    L0cK5Vp    在Crypt.asp中我L0cK5Vp们首先从密钥文件中得到密钥值,然后从这段密钥中

    L0cK5Vp截取和我L0cK5Vp们需要加密的明文同样长度的密钥。然后使用一个简单的异或操作

    L0cK5Vp将明文和密钥进行运算,那么得到的结果就是加密后的密文了。过程很简单的。

    L0cK5Vp    由于是使用了异或操作,所以解密将非常简单,只要使用同样的密钥对密文

    L0cK5Vp再次进行异或操作就能够解密了。

    L0cK5Vp    在上面介绍的基础上,你可以少加改动,就可以使用同样的方法加密一个文件。

    L0cK5Vp唯一需要注意的是,对于一个二进制文件,你需要做一些完整性检查以保证转换回来

    L0cK5Vp的字符不要越界。

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

    L0cK5Vp附注:

    L0cK5Vp    Vernam密码是由Gilbert Vernam (他是AT&T的工程师)在1918年发明的。

    L0cK5Vp这是一种使用异或方法进行加密解密的方法。 83484
  • 返回页面顶端
  • 上一篇:使用ASP加密算法加密你的数据(一)
  • 下一篇:ASP.NET中带有口令加密的注册页面
  • 返回上一页 打印本文 加入收藏
  • 页面最后更新时间:2011年5月18日