Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
Solved

# Encript a string

Posted on 2011-02-26
Medium Priority
277 Views
Experts,

I need a function or procedure that will encrypt an alpha numeric string.  Nothing complex, just a mapping (or something) that changes the string to something you couldn't make sense of and then back again.  I am not fussed about security of access, just security of interpretation.  Something like a mapping where A =Code1 and B=code2 C= code3.

I figure, someone would have done this before.

Code snippets or links are all I need.
0
Question by:Carl Sudholz
[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
• 2

LVL 76

Assisted Solution

GrahamSkan earned 332 total points
ID: 34986886
You could simply shift the ASCII code by one
Sub Encode()
Dim strNewText As String
Dim i As Integer

Const strSample = "Hello World"

For i = 1 To Len(strSample)
strNewText = strNewText & Chr\$(Asc(Mid(strSample, i, 1)) + 1)
Next i
MsgBox strNewText
MsgBox Decode(strNewText)
End Sub

Function Decode(strEncoded As String)
Dim strDecoded As String
Dim i As Integer

For i = 1 To Len(strEncoded)
strDecoded = strDecoded & Chr\$(Asc(Mid(strEncoded, i, 1)) - 1)
Next i
Decode = strDecoded
End Function
0

LVL 76

Accepted Solution

GrahamSkan earned 332 total points
ID: 34986908
Or you could use XOR.

Sub Encode_1()
Dim strNewText As String
Dim i As Integer

Const strSample = "Hello World"

For i = 1 To Len(strSample)
strNewText = strNewText & Chr\$(Asc(Mid(strSample, i, 1)) Xor 170)
Next i
MsgBox strNewText
MsgBox Decode_1(strNewText)
End Sub

Function Decode_1(strEncoded As String)
Dim strDecoded As String
Dim i As Integer

For i = 1 To Len(strEncoded)
strDecoded = strDecoded & Chr\$(Asc(Mid(strEncoded, i, 1)) Xor 170)
Next i
Decode_1 = strDecoded
End Function
0

LVL 13

Assisted Solution

devlab2012 earned 168 total points
ID: 34986932
Create a new module with the following code:

Option Compare Database
Option Explicit

Public Function Encrypt(str As String) As String
Encrypt = ""
Dim s1 As String
s1 = ""
Dim i As Integer
i = 0
For i = 1 To Len(str)
s1 = Mid(str, i, 1)
s1 = Right("000" & CStr(Asc(s1)), 3)
Encrypt = Encrypt & s1
Next
End Function

Public Function Decrypt(str As String) As String
Decrypt = ""
Dim s1 As String
Dim code As Integer
s1 = ""
Dim i As Integer
i = 0
For i = 1 To Len(str) Step 3
code = CInt(Mid(str, i, 3))
s1 = Chr(code)
Decrypt = Decrypt & s1
Next
End Function

Run the following queries to test it:

select Encrypt('devlab2012')
select Decrypt('100101118108097098050048049050')
select Decrypt(Encrypt('devlab2012'))
0

Author Closing Comment

ID: 34986946
Absolutely perfect!

I love experts exchange
0

## Featured Post

Question has a verified solution.

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

Microsoft Access is a place to store data within tables and represent this stored data using multiple database objects such as in form of macros, forms, reports, etc. After a MS Access database is created there is need to improve the performance and…
If you need a simple but flexible process for maintaining an audit trail of who created, edited, or deleted data from a table, or multiple tables, and you can do all of your work from within a form, this simple Audit Log will work for you.
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…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
###### Suggested Courses
Course of the Month7 days, 19 hours left to enroll