Results 1 to 5 of 5

Thread: How to encrypt & decrypt text or files in Vb dot net?

  1. #1
    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?

  2. #2
    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)

  3. #3
    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));
         }
    
    }

  4. #4
    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").

  5. #5
    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!

Similar Threads

  1. Is it possible to decrypt jpg files
    By Akolekar in forum Windows Software
    Replies: 6
    Last Post: 13-01-2012, 03:05 AM
  2. How to Encrypt Gmail, Facebook and other Text-Based Messages
    By Gerri in forum Technology & Internet
    Replies: 5
    Last Post: 12-09-2011, 09:48 AM
  3. Tools required to encrypt and decrypt data
    By BOGUMIL in forum Networking & Security
    Replies: 4
    Last Post: 09-05-2011, 04:21 PM
  4. How to decrypt PGP files ?
    By fARUQ aHMED in forum Operating Systems
    Replies: 3
    Last Post: 04-01-2011, 06:52 PM
  5. Replies: 0
    Last Post: 05-11-2008, 02:54 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,713,996,644.37942 seconds with 17 queries