Solved

Encript a string

Posted on 2011-02-26
4
272 Views
Last Modified: 2012-06-21
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
Comment
Question by:Carl Sudholz
  • 2
4 Comments
 
LVL 76

Assisted Solution

by:GrahamSkan
GrahamSkan earned 83 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

Open in new window

0
 
LVL 76

Accepted Solution

by:
GrahamSkan earned 83 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

Open in new window

0
 
LVL 13

Assisted Solution

by:devlab2012
devlab2012 earned 42 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

by:Carl Sudholz
ID: 34986946
Absolutely perfect!

I love experts exchange
0

Featured Post

Migrating Your Company's PCs

To keep pace with competitors, businesses must keep employees productive, and that means providing them with the latest technology. This document provides the tips and tricks you need to help you migrate an outdated PC fleet to new desktops, laptops, and tablets.

Question has a verified solution.

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

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…
It’s been over a month into 2017, and there is already a sophisticated Gmail phishing email making it rounds. New techniques and tactics, have given hackers a way to authentically impersonate your contacts.How it Works The attack works by targeti…
Basics of query design. Shows you how to construct a simple query by adding tables, perform joins, defining output columns, perform sorting, and apply criteria.
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…

770 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