HI,
I want to know how can I encrypt and decrypt text string on a simple Vb form accepting text from textbox?
Printable View
HI,
I want to know how can I encrypt and decrypt text string on a simple Vb form accepting text from textbox?
Below is an example of a substitution cipher. Substitution ciphers can be broken by a cryptanalyst who knows what they're doing, so this method of encryption shouldn't be used if you want your data to be 99% secure. However, substitution ciphers will keep common users from being able to see your data. For high levels of security look into public key encryption and AES. To continue on, copy and paste the function below into your *.bas file:
How to use this function:Code:Public Function Encrypt(text As String) As String
Dim charSet1 As String, charSet2 As String, i As Long
Dim pos As Long, encryptedChar, encryptedText
charSet1 = " ?!@#$%^&*()_+|0123456789abcdefghijklmnopqrstuvwxyz.,-~ABCDEFGHIJKLMNOPQRSTUVWXYZ¿¡²³ÀÁÂÃÄÅÒÓÔÕÖÙÛÜ*áâãä娶§Ú¥"
charSet2 = " ¿¡@#$%^&*()_+|01²³456789ÀbÁdÂÃghÄjklmÅÒÓqÔÕÖÙvwÛÜz.,-~A*áâãFGHäJKåMNضQR§TÚVWX¥Z?!23acefinoprstuxyBCDEILOPSUY"
For i = 1 To Len(text)
pos = InStr(charSet1, Mid(text, i, 1))
If pos > 0 Then
encryptedChar = Mid(charSet2, pos, 1)
encryptedText = encryptedText + encryptedChar
Else
encryptedText = encryptedText + Mid(text, i, 1)
End If
Next
Encrypt = encryptedText
End Function
To encrypt a word just put something like this in a button:
And then to decrypt the word just call the function again!Code:Text1.Text = Encrypt(Text1)
Code:Text1.Text = Encrypt(Text1)
A program to encrypt or decrypt text using vigenere cipher technique
Code:#include <stdio.h>
#include <ctype.h>
#include <string.h>
#include <process.h>
void vigenereCipher(char *,char *);
void encipher();
void decipher();
void main()
{
int choice;
//loop takes choice from user and calles appropriate function
while(1)
{
printf("\n1. Encrypt Text\n");
printf("2. Decrypt Text\n");
printf("3. Exit\n");
printf("Enter Your Choice : ");
scanf("%d",&choice);
fflush(stdin);
if(choice == 3)
exit(0);
else if(choice == 1)
encipher();
else if(choice == 2)
decipher();
else
printf("Please Enter Valid Option.");
}
}
void encipher()
{
unsigned int i,j;
char input[257],key[33];
printf("Enter Text to be Encrypted [Max. 256 characters/ only alphabets]:\n ");
gets(input);
printf("Enter Encryption Key [Max. 32 Characters/ only aphabets]: ");
gets(key);
for(i=0,j=0;i<strlen(input);i++,j++)
{
//repeat the key if you are at end of it.
if(j>=strlen(key))
{
j=0;
}
//actual logic -> character from input + character from key % 26 is encrypted charater
printf("%c",65+(((toupper(input[i])-65)+(toupper(key[j])-65))%26));
}
}
void decipher()
{
unsigned int i,j;
char input[257],key[33];
int value;
printf("Enter Text to be Decrypted [Max. 256 characters/ only alphabets]:\n ");
gets(input);
printf("Enter Decryption Key [Max. 32 Characters/ only aphabets]: ");
gets(key);
for(i=0,j=0;i<strlen(input);i++,j++)
{
//repeat the key if you are at end of it.
if(j>=strlen(key))
{
j=0;
}
//similar to encipher only difference is you need to subtract
value = (toupper(input[i])-64)-(toupper(key[j])-64);
//make positive if value is negative.
if( value < 0)
{
value = value * -1;
}
printf("%c",65 + (value % 26));
}
}
This is a very simple method of encryption and you may want to use a different one for bigger projects. You change the values for your own encryption types and keys. Hope you guys like my first tutorial. Constructive criticism is always nice, as well on errors in my code.
GUI:
2 Text Boxes
2 Command Buttons
That's it.
Code for Declarations:
Code for Command1 :Code:dim a as string
dim b as string
dim c as string
Code for Command2 :Code:Private Sub Command1_Click()
a = "a"
b = "b"
c = "c"
Text2.Text = Text1.Text
Text2.Text = Replace(Text2, a, "1")
Text2.Text = Replace(Text2, b, "2")
Text2.Text = Replace(Text2, c, "3")
End Sub
Notice the difference from Command1 and Command2. In Command1 a = "a" and text2.text = Replace(text2, a, "1"). In Command2 it's a = "1" and text2.text = Replace(text2, a, "a").Code:Private Sub Command1_Click()
a = "1"
b = "2"
c = "3"
Text2.Text = Text1.Text
Text2.Text = Replace(Text2, a, "a")
Text2.Text = Replace(Text2, b, "b")
Text2.Text = Replace(Text2, c, "c")
End Sub
Hello!
http://www.codeproject.com/KB/security/EncryptFile.aspx
Please check it out I hope this helps!