Hey,

A while back i needed to write an encryption algorithm, and came up with the following. Note, the encryption is blisteringly fast, it can encrypt a 1 MB file in about 4 seconds, but to decrypt it takes about 60.

This code works by getting the contents of a file in binary, and then multiplying, adding, etc using random numbers as well, as an Encryption String and Number. The result is then outputted to a file. As far as i can tell, this is pretty tough encryption, especially since you need both the string and number to decrypt it.


Som neccessary declarations:

VB Code:
  1. Option Explicit
  2. Dim encodestring As String
  3. Dim encodenumber As Long
  4. Dim i As Long
  5. Dim linecounter As Long
  6. Dim stringcounter As String
  7. Dim Y As Long
  8. Dim currentencode As Long
  9. Dim endline As Long
  10. Dim swapnumber As Integer
  11. Dim storebinary(10000000) As Long
  12. Dim counter As Long
  13. Dim charactercount As Long
  14. Dim stepcount As Long
  15. Dim currentchar As Long

This is the encryption code itself:

VB Code:
  1. Sub Encrypt(sourcefile As String, destfile As String, encryptionkey As String, encryptionnumber As Long)
  2.  
  3. On Error GoTo filerror
  4. Open sourcefile For Input As #1
  5.  
  6. On Error GoTo skipall
  7. encodestring = encryptionstring
  8. If encodestring = "" Then GoTo skipall
  9.  
  10. swapnumber = encryptionnumber
  11. If swapnumber = 0 Then GoTo skipall
  12. encodenumber = 0
  13.  
  14.     For i = 1 To Len(encodestring)
  15.          encodenumber = encodenumber + Asc(Mid(encodestring, i, 1))
  16.      Next i
  17.  On Error GoTo fileskip
  18.  
  19. On Error GoTo fileskip
  20. Do Until EOF(1) = True
  21.             Line Input #1, stringcounter
  22.             linecounter = linecounter + 1
  23. Loop
  24.  
  25. fileskip:
  26. Err = 0
  27. On Error GoTo skipall
  28. Close
  29.  
  30. Open sourcefile For Input As #1
  31.     Close #2
  32.     On Error GoTo filecloser
  33.     Open destfile For Output As #2
  34.     On Error GoTo skipall
  35.         Print #2, ""
  36.     Close #2
  37.     Open destfile For Binary As #2
  38.    
  39.            
  40.                 On Error Resume Next
  41.                 For i = 1 To linecounter
  42.                    
  43.                     Line Input #1, stringcounter
  44.                         For Y = 1 To Len(stringcounter)
  45.                            
  46.                             currentencode = ((Asc(Mid(stringcounter, Y, 1)) + encodenumber) * swapnumber)
  47.                             Put #2, , currentencode
  48.                             charactercount = charactercount + 1
  49.                            
  50.                            
  51.                         Next Y
  52.                      endline = 9999
  53.                      Put #2, , endline
  54.                 Next i
  55.               On Error GoTo skipall
  56. Close
  57. For i = 1 To 1000000
  58.     storebinary(i) = 0
  59. Next i
  60. counter = 1
  61.  
  62. MsgBox "Encryption Complete", vbDefaultButton1, "Encrypted"
  63. GoTo skipall
  64. filecloser:
  65. Close
  66. GoTo skipall
  67. filerror:
  68. MsgBox "File Error Detected", vbCritical, "File Error"
  69. Err = 0
  70. skipall:
  71. If Err <> 0 Then
  72. MsgBox "Error Encountered: " + Err.Description, vbCritical, "Error"
  73. End If
  74. Close
  75.  
  76.  
  77. End Sub

The decryption is in the next post.