?
Solved

Encrypt and decrypt a string

Posted on 2002-05-16
9
Medium Priority
?
619 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
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…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
Suggested Courses

649 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