Code Snippets

  

Visual Basic Source Code


Welcome to Dream.In.Code
Become a VB Expert!

Join 137,192 VB Programmers for FREE! Get instant access to thousands of VB experts, tutorials, code snippets, and more! There are 2,344 people online right now. Registration is fast and FREE... Join Now!





Encrypt-Decrypt File with Password

Encryption and Decryption of any file with a password.

Submitted By: Nitish4uall
Actions:
Rating:
Views: 9,711

Language: Visual Basic

Last Modified: May 26, 2006
Instructions: 'Add a CoommandButton - Command1

'Just Run and select click the button to see the action.

Snippet


  1. Sub FileEncodeAndDecode(InputFile As String, OutputFile As String, PasswordKey As String)
  2.    
  3.     Dim temp As Single
  4.     Dim Char As String * 1
  5.     Dim XORMask As Single
  6.     Dim temp1 As Integer
  7.    
  8.     Open InputFile For Binary As #1
  9.     Open OutputFile For Binary As #2
  10.    
  11.     For x = 1 To Len(PasswordKey)
  12.         temp = Asc(Mid$(PasswordKey, x, 1))
  13.         For y = 1 To temp
  14.             temp1 = Rnd
  15.         Next y
  16.        
  17.         ' Re-seed to throw off prying eyes
  18.         Randomize temp1
  19.     Next x
  20.        
  21.    
  22.     Counter = 0
  23.     For z = 1 To FileLen(InputFile)
  24.        
  25.         'Generate random mask
  26.         XORMask = Int(Rnd * 256)
  27.        
  28.         'Get the char & change it
  29.         Get 1, , Char
  30.         Char = Chr$((Asc(Char) Xor XORMask))
  31.         Put 2, , Char
  32.        
  33.         Counter = Counter + 1
  34.         If Counter > Len(PasswordKey) Then Counter = 1
  35.        
  36.         ' Pull random numbers from the hat
  37.         For x = 1 To (Asc(Mid$(PasswordKey, Counter, 1)) * 2)
  38.             temp = Rnd
  39.         Next x
  40.     Next z
  41.  
  42.  
  43.     Close #1
  44.     Close #2
  45.    
  46. End Sub
  47.  
  48. Private Sub Command1_Click()
  49.  
  50. Dim InputFile As String
  51.     Dim OutputFile As String
  52.     Dim PasswordKey As String
  53.    
  54.     InputFile = InputBox("Enter a filename to encode")
  55.     OutputFile = InputBox("Enter the new filename this will         become ")
  56.     PasswordKey = InputBox("Enter the password (key)")
  57.    
  58.     Call FileEncodeAndDecode(InputFile, OutputFile, PasswordKey)
  59.    
  60.     MsgBox "File written to " + OutputFile
  61.     End
  62. End Sub
  63.  
  64. Private Sub Form_Load()
  65.  
  66. Command1.Caption = "Code/Decode"
  67.  
  68. End Sub
  69.  
  70.  
  71.  

Copy & Paste


Comments


There are currently no comments for this snippet. Be the first to comment!

Add comment


You must be registered and logged on to </dream.in.code> to leave comments.





Live VB Help!

VB Tutorials

Reference Sheets

VB Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month