Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Premature Ascii "reading"

Status
Not open for further replies.

CaptainBob007

Programmer
Dec 20, 2003
42
US
Hi all -

Ok, I'm working on an ascii offset encryption procedure, which changes its offset after each character (so its much more difficult to figure out), and its working fine for encrypting strings of any size.

When it comes to decrypting, however, the problems start. Undoubtedly some ascii values such as 24 (cancel) start to come up, and rather than offset and decrypt them like I want it to, it decides to halt the process. Is there any way to stop the program from "reading" the encrypted ascii values as ascii values, and just to treat them as numbers instead?

I realize this may not make sense. I can clarify if necessary. Any help would be appreciated!

~Bob
 
Take a look at the Asc and Chr functions.

Hope This Help, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
I'm making use of those functions already, and know what they do. What I'm trying to do is have the function take in an ascii value such as 24 (which is CANCEL) and not execute it, but just treat it as 24..
 
and not execute it
???
Can you please post your code where you have problem ?

Hope This Help, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
the problem doesnt occur in the code, the problem occurs when the value is read into the code, because rather than execute the code, the computer is executing the command associated with the value (CANCEL), and stopping the code.
 
when the value is read into the code
How do you read the value ?

Hope This Help, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
ok, here's the code I have. I got it from another thread, but have done a bunch of modifications:

Code:
Public Function Encrypt(strString As String) As String

' DESCRIPTION:
'  Function takes a character string and encrypts it by
'   offsetting each character of the string by an integer value.
'
'   In the event that the character string is equal to "OTH000"
'   the string is not encrypted.
'
'
' INPUTS:
'   strString -String expression to encrypt
'
'
' OUTPUT:
'   Encrypt   -Encrypted string
'
'

    'declare variables
    Dim intStrLength As Integer
    Dim intOffset As Integer
    Dim OffsetOffset As Integer
    Dim intCounter As Integer
    Dim strLetter As String
    Dim intLetterPlain As Integer
    Dim intLetterEncrypted As Integer
    Dim strLetterEncrypted As String
    Dim strEncryptedString As String
    
    'set character offset value
    intOffset = 12
    OffsetOffset = 300
        
    'calculate length of string to encrypt
     intStrLength = Len(strString)
     
     MsgBox "Length = " & intStrLength, vbExclamation, "encrypt"
    
'test to see if strString is "OTH000"
    If strEncryptedString = "OTH000" Then
         strString = "OTH000"
            
    Else
        'encrypt string one letter at a time
        'build encrypted string by concatenating one letter at a time
        'stop at end of string
        For intCounter = 1 To intStrLength
            strLetter = Mid(strString, intCounter, 1)
            intLetterPlain = Asc(strLetter) - 32
            intLetterEncrypted = intLetterPlain + intOffset + OffsetOffset
            OffsetOffset = OffsetOffset + 1
            strLetterEncrypted = Chr$(intLetterEncrypted Mod 256)

            strEncryptedString = strEncryptedString & strLetterEncrypted
        Next intCounter
    End If
    
    'return encrypted string
    Encrypt = strEncryptedString

End Function

Public Function Decrypt(strEncryptedString As Variant) As String

' DESCRIPTION:
'   Function takes a the chatacter string previously encrypted by
'   the Encrypt() function, and returns the original plain text
' character string values.
'
'   In the event that the character string is equal to "OTH000"
'   the string is not decrypted.
'
'
' INPUTS:
'   strEncryptedString -Encrypted string expression
'
'
' OUTPUT:
'   Decrypt   -Decrypted string
'
'

    'declare variables
    Dim intStrLength As Integer
    Dim intOffset As Integer
    Dim OffsetOffset As Integer
    Dim intCounter As Integer
    Dim strLetter As String
    Dim intLetterPlain As Variant
    Dim strLetterPlain As String
    Dim intLetterEncrypted As Integer
    Dim strLetterEncrypted As String
    Dim strString As String
    
    'set character offset value
    intOffset = 12
    OffsetOffset = 300
        
    'test to see if strString is null
    If IsNull(strEncryptedString) Then
       strString = ""
        
    'test to see if strString is "OTH000"
    ElseIf strEncryptedString = "OTH000" Then
         strString = "OTH000"
                   
    Else
        'calculate length of string to encrypt
        intStrLength = Len(strEncryptedString)
     
     MsgBox "Length = " & intStrLength, vbExclamation, "decrypt"
     
        'encrypt string one letter at a time
        'build encrypted string by concatenating one letter at a time
        'stop at end of string
        For intCounter = 1 To intStrLength
            strLetterEncrypted = Mid(strEncryptedString, intCounter, 1)
            intLetterEncrypted = Asc(strLetterEncrypted)
            intLetterPlain = intLetterEncrypted - intOffset - OffsetOffset
            OffsetOffset = OffsetOffset + 1
            While intLetterPlain < 0
                intLetterPlain = intLetterPlain + 256
            Wend
            strLetterPlain = Chr$(intLetterPlain + 32)

            strString = strString & strLetterPlain
        Next intCounter
    End If
    
    'return encrypted string
    Decrypt = strString

End Function

As you can see, we have separate functions for encrypting and decrypting. Encrypt works fine, but when decrypt is running, if the encrypted value it takes in is 24, (which means cancel in ascii), it will in fact cancel the function.. how can I avoid this?
 
I've copy/paste your functions in an access module and got no problem even with chr(24) somewhere in the encrypted string.
In which context is the Decrypt function called ?

Hope This Help, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
try typing a very long string.... The test I'm using has about 350 characters, and fails after about 140.
 
I'm not sure if it matters but the following test will never fire in the Encrypt block:
Code:
    'test to see if strString is "OTH000"
    If strEncryptedString = "OTH000" Then
         strString = "OTH000"

VBSlammer
redinvader3walking.gif

[sleeping]Unemployed in Houston, Texas
 
I'm sure I've tested encrypted strings with chr(24) as i've added this statement in the encrypt function code:
If strLetterEncrypted = Chr$(24) Then MsgBox intCounter
No problem even with long encrypted string containing 2 chr(24)

Hope This Help, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top