Solved

Encrypt and decrypt a string

Posted on 2002-05-16
9
598 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
 
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
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…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

929 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now