Solved

Encrypt and decrypt a string

Posted on 2002-05-16
9
605 Views
Last Modified: 2008-03-10
Hi, I'm looking for an algorithm to take a string and encrypt it to an alphanumeric of a fixed length, i.e. 16 char.

The plaintext is fixed to 32 char. After encryption, I need to decrypt and get back the 32 char plaintext.

Thank you.
0
Comment
Question by:suhani
9 Comments
 
LVL 1

Expert Comment

by:falconew
ID: 7015579
This is a simple algorithm for encryption using operand XOR. You can modify it to a more complex algorithm.
In the VB form place 2 command button, and 2 text box. Command1 is for encryption a text in text1 textbox control, the result will be in text2 textbox control. Command2 for decrypt the result of encription from text2 textbox control.

Private Sub Command1_Click()
Dim i As Long
Dim strEncrypt As String
For i = 1 To Len(Text1.Text)
    strEncrypt = strEncrypt + Chr(Asc(Mid(Text1.Text, i, 1)) Xor 100)
Next i
Text2.Text = strEncrypt
End Sub

Private Sub Command2_Click()
Dim i As Long
Dim strDecrypt As String
For i = 1 To Len(Text2.Text)
    strDecrypt = strDecrypt + Chr(Asc(Mid(Text2.Text, i, 1)) Xor 100)
Next i
MsgBox strDecrypt
End Sub

hope this help..
0
 
LVL 1

Expert Comment

by:falconew
ID: 7015618
This link is a sample for encryption and decryption..
http://www.planet-source-code.com/vb/scripts/ShowCode.asp?txtCodeId=15208&lngWId=1
0
 
LVL 3

Expert Comment

by:SirNick
ID: 7015682
Hello suhani

Here is a page containing a sample of encryption.

http://www.freevbcode.com/ShowCode.Asp?ID=2715

You could do a search using the word encyrption on the following site.

http://www.freevbcode.com

Hope this helps...
0
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 2

Expert Comment

by:johnny6
ID: 7016972
Option Explicit

Public Key As String


Public Function Encrypt(s As String) As String
Dim slen As Integer
Dim klen As Integer
Dim i, j As Integer
slen = Len(s)
klen = Len(Key)
For i = 0 To slen Step klen
 For j = 1 To klen
    If i + j <= slen Then
       Mid(s, i + j, 1) = _
         Chr(Asc(Mid(s, i + j, 1)) _
         Xor Asc(Mid(Key, j, 1)))
       If Mid(s, i + j, 1) = Chr(0) Then
         Mid(s, i + j, 1) = Mid(Key, j, 1)
       End If
    End If
 Next j
Next i
Encrypt = s
End Function

Public Function Decrypt(s As String) As String
Dim slen As Integer
Dim klen As Integer
Dim i, j As Integer
slen = Len(s)
klen = Len(Key)
For i = 0 To slen Step klen
 For j = 1 To klen
    If i + j <= slen Then
       Mid(s, i + j, 1) = _
         Chr(Asc(Mid(s, i + j, 1)) _
         Xor Asc(Mid(Key, j, 1)))
       If Mid(s, i + j, 1) = Chr(0) Then
         Mid(s, i + j, 1) = Mid(Key, j, 1)
       End If
    End If
 Next j
Next i
Decrypt = s
End Function



Private Sub Command1_Click()
   Key = "My-Password"
   Text1 = Encrypt("this is a test")
   Text2 = Decrypt(Text1)
End Sub
0
 

Author Comment

by:suhani
ID: 7020701
The encrypted string should be in HEX value of a FIXED length, e.g 16 characters. How do I do that?
0
 
LVL 49

Expert Comment

by:DanRollins
ID: 7817751
Hi suhani,
It appears that you have forgotten this question. I will ask Community Support to close it unless you finalize it within 7 days. I will ask a Community Support Moderator to:

    Accept falconew's comment(s) as an answer.

suhani, if you think your question was not answered at all or if you need help, just post a new comment here; Community Support will help you.  DO NOT accept this comment as an answer.

EXPERTS: If you disagree with that recommendation, please post an explanatory comment.
==========
DanRollins -- EE database cleanup volunteer
0
 

Expert Comment

by:zainalab
ID: 7843120
I think suhani is right not accepting any answer. The answers given did not answer the question. All answers will result in an encrypted string that grows as the original text grows. I think what suhani wanted is how to make it so that encrypted string is FIXED (16 characters), even if the unencrypted string grows.
0
 
LVL 49

Expert Comment

by:DanRollins
ID: 7844445
Thanks for your input zainalab.
Upon re-reading, I tend to agree.  The original question was somewhat ambiguous, and suhani's only followup was also ambiguous, but did provide enough information that an Expert *could have* presented a solution that would work in certain cases (for instance, if the password was all uppercase letters A-Z).  

I am changing my recommendation to:

   Refund and 0-pt PAQ
==========
DanRollins -- EE database cleanup volunteer
0
 

Accepted Solution

by:
SpideyMod earned 0 total points
ID: 7909701
per recommendation

SpideyMod
Community Support Moderator @Experts Exchange
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

839 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question