选择显示字体大小

使用asp加密算法加密你的数据(二)

    
  
    在第一部分,讨论了如何生成密钥,下面将介绍如何使用这个密钥来加密和解密一个字符串。
    下面的代码就是能够同时实现这个功能的函数
  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年发明的。这是一种使用异或方法进行加密解密的方法。
  
    


 


关键字 本文所属关键字

相关 与本文相关文章

分类 所有文章关键字导航

源码编程相关

Java   Asp   PHP   .Net   XML   C/C++   CGI   VB   Jsp   J2ee   J2se   J2me   EJB   Servlet   Tomcat   Resin   Struts   Weblogic   Eclipse   ANT   GUI   JMS   Web servise   IDEA   Webphere   Hibernate   Spring   Jboss   Applet   Swing   Socket   Javamail   Perl   Ajax   P2P   安全   模式   框架   测试   开源   游戏

SQL数据库相关

My-SQL   Ms-SQL   Access   DB2   Oracle   Sybase   SQLserver   索引   存储过程   加密   数据库   分页   视图  

手机无线相关

3G   Wap   CDMA   GRPS   GSM   IVR   彩信   短信   无线   增值业务

网页设计制作相关

HTML   CSS   网页配色   网页特效   Javascript   VBscript   Dreamweaver   Frontpage   JS   Web   网站设计

网站建设推广相关

建站经验   网站优化   网站排名   推广   Alexa

操作系统/服务器相关

Windows XP   Windows 2000   Windows 2003   Windows Me   Windows 9.x   Linux   UNIX   注册表   操作系统   服务器   应用服务器

图形图像多媒体相关

Photoshop   Fireworks   Flash   Coreldraw   Illustrator   Freehand   Photoimpact   多媒体   图形图像

标准 网站致力的规范

Valid CSS!

无不良内容,无不良广告,无恶意代码

Valid XHTML 1.0 Transitional

creativecommons