VFP Encode / Decode Alpha Numeric String

Posted on 2012-09-10
1 Endorsement
Last Modified: 2012-09-10
Hi all. I need a way to encrypt / decrypt an alpha numeric string in vfp 9.

Someone here pointed me toward BITXOR a week or so ago and I am using that in one portion of the program and it works quite well.

However when the string is encoded it results in mostly in odd, unpronounceable characters. I need something that can be spoken, read over the telephone, sort of like a Microsoft key.

I would prefer something that will take a string consisting of both characters and numbers and produce a numeric result.
Question by:formadmirer
    LVL 29

    Accepted Solution

    Well, it's not impossible to stay within a set of chars, if you also come from one, eg by CHRTRAN() instead of BITXOR:

    #Define ccEncrypt "ghiefmndcyzopqrstuvwxjklab"
    lcMessage = "hello"
    lcEncrypted = chrtran(lcMessage,'abcdefghijklmnopqrstuvwxyz',ccEncrypt)
    lcDecrypted = chrtran(lcEncrypted,ccEncrypt,'abcdefghijklmnopqrstuvwxyz')

    You have to keep the ccEncrypt value a secret, of course, as it's you encryption/decryption key. You could apply a password in a may to modify ccEncrypt before usage, so you would have another secret an attacker, or I as the "inventor" of this would need to know to decrypt your encrypted text.

    Notice, this does not work with "Hello", before you not also add H to the ccEncrypt. It will remain unchanged, though.

    The other thing you can do is use whatever easy or complex encryption, like VFPEncryption FLL and as a final step use Base64 encoding:

    cReadable = strconv(cEncrypted,13)
    Then on the receiver side
    cEncrypted = Strconv(cReadable,14)

    It has the disadvantage of being 33% longer, as it encodes 3 ascii bytes in 4 ascii bytes, but readable ones, letters, numbers and a few more 64 readable chars.

    Don't be tempted to think this alone really is encryption. It's not, it always generates the same output from the same input, you don't specify any key or password, so it's reversable and if you ever took a look into a mime mail text you can recognize this as base64 format by the typical compostion of the 64 base64 chars.

    Bye, Olaf.
    LVL 27

    Expert Comment

    You can also generate an algorithm for coding and decoding based on the position of the letter and a password.

    To maintain within capital alphabets you have to use MOD.


    MOD(nDividend, nDivisor)
    LVL 29

    Expert Comment

    by:Olaf Doschke
    While MOD puts values into a range of eg 0 to 25, you lose bits you will not be able to reconstruct. As far as I understand formadmirer, the whole process should be reversable to get back to the initial value.

    you can do mod(28,26) and get 2, but also Mod(2,26) is 2. If you want to revert from 2 to the original, you can't decide, if the original was 28,2, or any other N*26+2. So you also have to store int(charValue/26). Base64 is already doing that, it spreads 3x8=24 bits into 4x6 bits.

    Bye, Olaf.

    Author Closing Comment

    This looks very usable and has some good information - thanks!
    LVL 29

    Expert Comment

    by:Olaf Doschke
    Here's one way to incorporate a password into chrtran encryption: Decompose it into it's bits and let each bit decide, wether to switch or not switch tow chars of ccEncrypt before using that for encryption.

    You can make a block or stream cipher with it, by encoding lcMessage in blocks of N chars or each char individually.

    There are many ideas, maybe you have your own. You could also XOR password chars again, but that will result in unreadable chars or control chars, whcih you want to avoid. My idea is to use the password as control data to shuffle your ccEncrypt translation string. Just make sure each char is in there once only and also make sure every char you want to encrypt is in there.

    Bye, Olaf.

    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    Join & Write a Comment

    Microsoft Visual FoxPro (short VFP) is a programming language with it’s own IDE and database, ranking somewhat between Access and VB.NET + SQL Server (Express). Product Description: (http://msd…
    Digital marketing agencies have encountered both the opportunities and difficulties that emerge from working with a wide-ranging organizations.
    To add imagery to an HTML email signature, you have two options available to you. You can either add a logo/image by embedding it directly into the signature or hosting it externally and linking to it. The vast majority of email clients display l…
    This video discusses moving either the default database or any database to a new volume.

    732 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

    23 Experts available now in Live!

    Get 1:1 Help Now