Library code snippets
Encrypt/Decrypt
This is a test of a rudimentary encryption/decryption
algorithm.
Note: It is possible for the encrypted string to include Chr$(0).
Visual Basic handles this just fine but Windows (and
therefore the TextBox control) uses Chr$(0) to signify the end
of the string. Therefore, programs such as this one that store
the encrypted data in a text box may end up truncating the data.
First, add a textbox, called txtText, with its MultiLine property set to true. Then, add another textbox called txtPassword, and two command buttons named cmdEncrypt, and cmdDecrypt. Finally, add the code below.
'This program may be distributed on the condition that it is
'distributed in full and unchanged, and that no fee is charged for
'such distribution with the exception of reasonable shipping and media
'charged. In addition, the code in this program may be incorporated
'into your own programs and the resulting programs may be distributed
'without payment of royalties.
'
'This example program was provided by:
' SoftCircuits Programming
' http://www.softcircuits.com
' P.O. Box 16262
' Irvine, CA 92623
Option Explicit
'Set to True to make the password case-sensitive
#Const CASE_SENSITIVE_PASSWORD = False
Private Sub cmdEncrypt_Click()
' You can encrypt twice for extra security
txtText = EncryptText((txtText), txtPassword)
txtText = EncryptText((txtText), txtPassword)
End Sub
Private Sub cmdDecrypt_Click()
txtText = DecryptText((txtText), txtPassword)
txtText = DecryptText((txtText), txtPassword)
End Sub
'Encrypt text
Private Function EncryptText(strText As String, ByVal strPwd As String)
Dim i As Integer, c As Integer
Dim strBuff As String
#If Not CASE_SENSITIVE_PASSWORD Then
'Convert password to upper case
'if not case-sensitive
strPwd = UCase$(strPwd)
#End If
'Encrypt string
If Len(strPwd) Then
For i = 1 To Len(strText)
c =
Asc(Mid$(strText, i, 1))
c = c +
Asc(Mid$(strPwd, (i Mod Len(strPwd)) + 1, 1))
strBuff =
strBuff & Chr$(c And &HFF)
Next i
Else
strBuff = strText
End If
EncryptText = strBuff
End Function
'Decrypt text encrypted with EncryptText
Private Function DecryptText(strText As String, ByVal strPwd As String)
Dim i As Integer, c As Integer
Dim strBuff As String
#If Not CASE_SENSITIVE_PASSWORD Then
'Convert password to upper case
'if not case-sensitive
strPwd = UCase$(strPwd)
#End If
'Decrypt string
If Len(strPwd) Then
For i = 1 To Len(strText)
c =
Asc(Mid$(strText, i, 1))
c = c -
Asc(Mid$(strPwd, (i Mod Len(strPwd)) + 1, 1))
strBuff =
strBuff & Chr$(c And &HFF)
Next i
Else
strBuff = strText
End If
DecryptText = strBuff
End Function
Related articles
Related discussion
-
Run-time error '91'
by converter2009 (1 replies)
-
VB6 Runtime error 381 subsript out of range Error
by Uncle (2 replies)
-
passing and reading parameters from using Shell
by jigartoliya (0 replies)
-
Convert C++ code to VB6
by mawcot (4 replies)
-
listbox scrollbar
by Dennijr (10 replies)
Related podcasts
-
Christian Beauclair
14 mai 2008 (�mission #0074) ::.Christian Beauclair: Stratégies de migration VB6 vers .NET Nous discutons avec Christian Beauclair des stratégies de migration VB6 vers .NET. Entre autres, nous discutons comment utiliser le "VB 6 Code Advisor" et le "Interop Forms Toolkit" pour ajouter la puiss...
looks good lemme try it
This is the C# Code
class Class1
{
// Encryption Method
public object EncryptText(string strText, string strPwd)
{
int i;
int c;
string strBuff = "";
// Convert User Password to upper case
strPwd = strPwd.ToUpper();
for (i = 0; i < (strText.Length); i++)
{
c = Convert.ToInt32(char.Parse(strText.Substring(i, 1)));
c = c + Convert.ToInt32(char.Parse(strPwd.Substring((i % (strPwd.Length)), 1)));
strBuff = strBuff + Chr(c & 255);
}
return strBuff;
}
// Decryption Method
public object DecryptText(string strText, string strPwd)
{
int i;
int c;
string strBuff = "";
// Convert User Password to upper case
strPwd = strPwd.ToUpper();
for (i = 0; i < (strText.Length); i++)
{
c = Convert.ToInt32(char.Parse(strText.Substring(i, 1)));
c = c - Convert.ToInt32(char.Parse(strPwd.Substring((i % (strPwd.Length)), 1)));
strBuff = strBuff + Chr(c & 255);
}
return strBuff;
}
char Chr(int n)
{
return (char)n;
}
}
regards
Amila
hi,
i implemented the code and it encrypts correctly,but when i decrypt it it still gives me weird characters and not the string that i had passed to encrypt function.
Can you please guide me in the same.
I have been trying to use your code for my application in C#,but i am less successful.
Could you please post the code in C#?
Thanks in advance.
Regards
Anuradha
The pseudocode is like this. In the final encrypted text, the chr(0) cuts the rest of the string. We can
mark those positions and decrypt accordingly.
In our encrypted text we can include these characters in the front.
flag & noOfFaults & pos(1), .....pos(n),
flag indicated that the problem occured. noofFaults indicated the total number of faults and pos(1),..pos(n) indicates the positions of faults. In the decryption process we will first replace those positions with chr(0) and decrypt accordingly.
Option Explicit
'Set to True to make the password case-sensitive
#Const CASE_SENSITIVE_PASSWORD = False
Private Sub cmdEncrypt_Click()
Dim buffText As String
Dim lngFileEnd As Integer
' You can encrypt twice for extra security
buffText = EncryptText((txtText), txtPassword)
buffText = EncryptTextFinal((buffText), txtPassword)
Dim path As String
path = App.path & "\a.txt"
'writing the contents to a file
'opening the file and writing contents
Open path For Binary Access Write As #1
lngFileEnd = LOF(1) + 1
'putting data into file
Put #1, lngFileEnd, buffText
Close #1
txtText = buffText
cmdEncrypt.Enabled = False
cmdDecrypt.Enabled = True
End Sub
Private Sub cmdDecrypt_Click()
txtText = DecryptTextFault((txtText), txtPassword)
txtText = DecryptText((txtText), txtPassword)
cmdEncrypt.Enabled = True
cmdDecrypt.Enabled = False
End Sub
'Encrypt text finally
Private Function EncryptTextFinal(strText As String, ByVal strPwd As String)
Dim i As Integer, c As Integer
Dim strBuff As String
Dim jhamela As Integer
Dim noFault As Integer
jhamela = 0
noFault = 0
#If Not CASE_SENSITIVE_PASSWORD Then
'Convert password to upper case
'if not case-sensitive
strPwd = UCase(strPwd)
#End If
'Encrypt string
If Len(strPwd) Then
For i = 1 To Len(strText)
c = Asc(Mid(strText, i, 1))
c = c + Asc(Mid(strPwd, (i Mod Len(strPwd)) + 1, 1))
Dim addBuff As String
'checking for faulty position
If Chr(c And &HFF) = Chr(0) Then
'marking of a faulty portion
jhamela = 1
noFault = noFault + 1
addBuff = Trim(addBuff) & Trim(Str(i)) & ","
strBuff = strBuff & "0"
Else
strBuff = strBuff & Chr(c And &HFF)
End If
Next i
End If
Dim jhBuff As String
If jhamela = 1 Then
jhBuff = "1"
strBuff = Trim(jhBuff) & Trim(Str(noFault)) & "," & Trim(addBuff) & Trim(strBuff)
Else
jhBuff = "0"
strBuff = Trim(jhBuff) & Trim(strBuff)
End If
EncryptTextFinal = strBuff
End Function
'Encrypt text
Private Function EncryptText(strText As String, ByVal strPwd As String)
Dim i As Integer, c As Integer
Dim strBuff As String
Dim jhamela As Integer
Dim noFault As Integer
jhamela = 0
noFault = 0
#If Not CASE_SENSITIVE_PASSWORD Then
'Convert password to upper case
'if not case-sensitive
strPwd = UCase(strPwd)
#End If
'Encrypt string
If Len(strPwd) Then
For i = 1 To Len(strText)
c = Asc(Mid(strText, i, 1))
c = c + Asc(Mid(strPwd, (i Mod Len(strPwd)) + 1, 1))
strBuff = strBuff & Chr(c And &HFF)
Next i
Else
strBuff = strText
End If
EncryptText = strBuff
End Function
'Decrypt text encrypted with EncryptText
Private Function DecryptText(strText As String, ByVal strPwd As String)
Dim i As Integer, c As Integer
Dim strBuff As String
#If Not CASE_SENSITIVE_PASSWORD Then
'Convert password to upper case
'if not case-sensitive
strPwd = UCase(strPwd)
#End If
'Decrypt string
If Len(strPwd) Then
For i = 1 To Len(strText)
c = Asc(Mid(strText, i, 1))
c = c - Asc(Mid(strPwd, (i Mod Len(strPwd)) + 1, 1))
strBuff = strBuff & Chr(c And &HFF)
Next i
Else
strBuff = strText
End If
DecryptText = strBuff
End Function
'Decrypt text encrypted with EncryptText
Private Function DecryptTextFault(strText As String, ByVal strPwd As String)
Dim i As Integer, c As Integer
Dim strBuff As String
#If Not CASE_SENSITIVE_PASSWORD Then
'Convert password to upper case
'if not case-sensitive
strPwd = UCase(strPwd)
#End If
'getting the first portion and finding the faulty positions
Dim jhText As String
Dim mainText As String
Dim noFault As Integer
Dim pos(100) As Integer
mainText = strText
jhText = Mid(strText, 1, 1)
'MsgBox jhText
If jhText = "1" Then
'getting the jhamelas
Dim jhNo As String
jhNo = ""
Dim j As Integer
j = 2
Do
jhNo = jhNo & Trim(Mid(mainText, j, 1))
j = j + 1
Loop While Mid(mainText, j, 1) <> ","
noFault = CInt(jhNo)
'looping through the maintext for finding the positions
'MsgBox j
For i = 1 To noFault
Do
pos(i) = pos(i) & Trim(Mid(mainText, j, 1))
j = j + 1
Loop While Mid(mainText, j, 1) <> ","
'MsgBox pos(i) & "and current j " & j
Next i
'now correction of the main text and getting our desired text
mainText = Mid(mainText, j + 1, Len(mainText))
j = 1 'updating the current position
'going to faulty positions and correcting those
Dim correctedMainText As String
correctedMainText = ""
For i = 1 To noFault
j = pos(i)
'previous text
correctedMainText = Trim(Mid(mainText, 1, j - 1))
'faulty text
correctedMainText = correctedMainText & Chr(0)
'trailing text
correctedMainText = correctedMainText & Trim(Mid(mainText, j + 1, Len(mainText)))
mainText = correctedMainText
Next i
mainText = correctedMainText
Else
mainText = Mid(strText, 2, Len(strText))
End If
'Decrypt string
If Len(strPwd) Then
For i = 1 To Len(mainText)
c = Asc(Mid(mainText, i, 1))
c = c - Asc(Mid(strPwd, (i Mod Len(strPwd)) + 1, 1))
strBuff = strBuff & Chr(c And &HFF)
Next i
Else
strBuff = strText
End If
DecryptTextFault = strBuff
End Function
Hello,
i'm doing a small app to create encrypt keys, and i'm used your example, except in Chr$ i got an error and just used Chr, and it works fine.the problem is, after create encrypt word, and saved to a file, when reading the encrypted word i can't decrypt.......any idea???
thanks very much for your time
This thread is for discussions of Encrypt/Decrypt.