Solved

Encrypt and decrypt a string

Posted on 2002-05-16
9
595 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
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 

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

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Suggested Solutions

Introduction I needed to skip over some file processing within a For...Next loop in some old production code and wished that VB (classic) had a statement that would drop down to the end of the current iteration, bypassing the statements that were c…
When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
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…
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…

705 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

20 Experts available now in Live!

Get 1:1 Help Now