Click here to Skip to main content
Licence 
First Posted 19 Jan 2002
Views 176,806
Bookmarked 51 times

Encrypting Passwords in ASP

By | 19 Jan 2002 | Article
A simple function to encrypt your users passwords

Introduction

Do you have a website where users need to login, and when they do you compare the password they enter with a column in a usertable? Some people do logins like this. It's easy to program and it works just fine. But what if someone got hold of the usertable and all the passwords of everyone? You may want to hide or encrypt the passwords in the usertable. Many programming languages have functions to do this. I make ASP-webpages, and I haven't found any quick functions to do this. There are plenty of components to do this, some free of charge even. But what if you cant install components on the webserver

Here is a short and neat way to encrypt your users passwords. You need two strings for it to work. Typically the username and the password.

Code

Function encrypt(x1, x2)
    s = ""
    t = 0
    For i = 1 to len(x1)
        t = t + asc(mid(x1,i,1))
    Next
    For i = 1 to len(x2)
        y = (t + asc(mid(x2,i,1)) * asc(mid(x2,((i+1) mod len(x2)+1),1))) mod 255
        s = s & chr(y)
    Next
    For i = (len(x2) + 1) to 10
        If t>598.8 Then t = 598.8
        y = t^3*i mod 255
        s = s & chr(y)
    Next
    encrypt = s
End Function

If you want to test this function you can create an asp-page and upload it to your website. Here's my codelisting to encrypt.asp

<%
Function encrypt(x1, x2)
    s = ""
    t = 0
    For i = 1 to len(x1)
        t = t + asc(mid(x1,i,1))
    Next
    For i = 1 to len(x2)
        y = (t + asc(mid(x2,i,1)) * asc(mid(x2,((i+1) mod len(x2)+1),1))) mod 255
        s = s & chr(y)
    Next
    For i = (len(x2) + 1) to 10
        If t>598.8 Then t = 598.8
        y = t^3*i mod 255
        s = s & chr(y)
    Next
    encrypt = s
End Function
%>

<html>
<head>
 <title>Encrypt</title>
</head>

<body>
<% If request.form("name") = "" Then %>
<form action="encrypt.asp" method="post">
<input type="text" name="name"><input type="text" name="pass">
<input type="submit">
</form>
<% Else  %>
<% response.write encrypt(request.form("name"),request.form("pass")) %>
<% End If %>
</body>
</html>

Remarks

  • The function is not reversible, so there is no way to take the result and reverse it into the password. You will need to recreate the password with a new one (some users seem to forget their passwords and always wants it retreieved)
  • This is not a high-level encryption, but its good enough to hide it from lame hackers (hehe).
  • The password is always sent from the user inputpage to the page encrypting it. Somewhere in between a hacker can fetch it. Secure zones (SSL) can remedy this.
  • Feel free to use the code to whatever you like. But if you alter it make a post in the thread related to this article so we all can share the fun.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

tommy skaue

Web Developer

Norway Norway

Member

Tommy live in Tromsø, a city far up north in Norway. He does programming and webdevelopment for a living.

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
QuestionEncrypt & Decrypt string [modified] Pinmemberc2love16:14 14 Nov '07  
AnswerRe: Encrypt & Decrypt string Pinmembertommy skaue21:43 14 Nov '07  
Questionencrypt & decrypt password Pinmemberred-apple0:04 11 Aug '07  
AnswerRe: encrypt & decrypt password Pinmembertommy skaue2:56 11 Aug '07  
GeneralRe: encrypt & decrypt password Pinmemberkenbhavin21:18 13 May '08  
AnswerRe: encrypt & decrypt password Pinmemberkenbhavin21:16 13 May '08  
Generalone small change Pinmemberguildwyn11:17 24 Dec '06  
QuestionHow to decrypt?? PinsussShunHung17:11 14 Jul '05  
AnswerRe: How to decrypt?? PinmemberChristian Graus17:47 14 Jul '05  
Generalmodification of this code PinsussAnonymous14:24 2 Sep '04  
GeneralRe: modification of this code Pinmembertommy skaue21:13 2 Sep '04  
GeneralRe: modification of this code Pinmemberkryzchek7:40 18 May '05  
tommy skaue wrote:
old_string = "website"
for i = 0 to len(old_string)
new_String = new_String & Chr(Asc(Mid(old_string,i,1))+1)
next

 
That won't necessarily work to get the next alphabetic character though. It's not smart enough to wrap back around for "Z". Smile | :)
GeneralRe: modification of this code Pinmembertommy skaue10:30 18 May '05  
GeneralSame function ported to Perl Pinmembersoffen7:55 21 Nov '03  
GeneralReturn value of Encrypt Function PinmemberVen Yetukuri8:54 3 Mar '03  
GeneralIt wont work this way PinsussStarLite9:19 28 Jan '03  
GeneralRe: It wont work this way Pinmembertommy skaue21:14 28 Jan '03  
GeneralRe: It wont work this way PinsussAnonymous3:34 29 Jan '03  
GeneralRe: It wont work this way Pinmembertommy skaue20:59 29 Jan '03  
Generalits a pretty good article PinmemberHoratiu CRISTEA23:30 31 Jan '02  
GeneralRe: its a pretty good article Pinmembertommy skaue0:18 1 Feb '02  
GeneralRe: its a pretty good article PinmemberHoratiu CRISTEA2:18 1 Feb '02  
GeneralRe: its a pretty good article Pinmembertommy skaue2:18 1 Feb '02  
GeneralTommy's son (off-topic) PinmemberOz3:29 22 Jan '02  
GeneralRe: Tommy's son (off-topic) Pinmembertommy skaue3:32 22 Jan '02  

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Mobile
Web01 | 2.5.120528.1 | Last Updated 20 Jan 2002
Article Copyright 2002 by tommy skaue
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid