Go Back   TechArena Community > Software > Software Development
Become a Member!
Forgot your username/password?
Tags Active Topics RSS Search Mark Forums Read

Reply
 
Thread Tools Search this Thread
  #1  
Old 27-02-2009
Member
 
Join Date: Jan 2009
Posts: 36
How to encrypt & decrypt text or files in Vb dot net?

HI,

I want to know how can I encrypt and decrypt text string on a simple Vb form accepting text from textbox?
Reply With Quote
  #2  
Old 27-02-2009
Member
 
Join Date: May 2008
Posts: 44
Re: How to encrypt & decrypt text or files in Vb dot net?

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:

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
How to use this function:

To encrypt a word just put something like this in a button:

Code:
Text1.Text = Encrypt(Text1)
And then to decrypt the word just call the function again!

Code:
Text1.Text = Encrypt(Text1)
Reply With Quote
  #3  
Old 27-02-2009
Member
 
Join Date: Apr 2008
Posts: 1,948
Re: How to encrypt & decrypt text or files in Vb dot net?

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));
     }

}
Reply With Quote
  #4  
Old 27-02-2009
Member
 
Join Date: May 2008
Posts: 35
Re: How to encrypt & decrypt text or files in Vb dot net?

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:
dim a as string
dim b as string
dim c as string
Code for Command1 :

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
Code for Command2 :

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
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").
Reply With Quote
  #5  
Old 27-02-2009
Member
 
Join Date: Jun 2008
Posts: 144
Re: How to encrypt & decrypt text or files in Vb dot net?

Hello!

http://www.codeproject.com/KB/security/EncryptFile.aspx

Please check it out I hope this helps!
Reply With Quote
Reply

  TechArena Community > Software > Software Development
Tags: , , ,



Thread Tools Search this Thread
Search this Thread:

Advanced Search


Similar Threads for: "How to encrypt & decrypt text or files in Vb dot net?"
Thread Thread Starter Forum Replies Last Post
Is it possible to decrypt jpg files Akolekar Windows Software 6 13-01-2012 03:05 AM
How to Encrypt Gmail, Facebook and other Text-Based Messages Gerri Technology & Internet 5 12-09-2011 09:48 AM
Tools required to encrypt and decrypt data BOGUMIL Networking & Security 4 09-05-2011 04:21 PM
How to decrypt PGP files ? fARUQ aHMED Operating Systems 3 04-01-2011 06:52 PM
Add Encrypt / Decrypt Options to Windows Vista Right-Click Menu Eldon Tips & Tweaks 0 05-11-2008 02:54 PM


All times are GMT +5.5. The time now is 08:52 AM.