Link to home
Start Free TrialLog in
Avatar of suhani
suhani

asked on

Encrypt and decrypt a string

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.
Avatar of falconew
falconew

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..
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...
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
Avatar of suhani

ASKER

The encrypted string should be in HEX value of a FIXED length, e.g 16 characters. How do I do that?
Avatar of DanRollins
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
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.
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
ASKER CERTIFIED SOLUTION
Avatar of SpideyMod
SpideyMod

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial