Results 1 to 13 of 13

Thread: Your Own Encryption Algorithm

  1. #1

    Thread Starter
    Hyperactive Member kazar's Avatar
    Join Date
    Apr 2006
    Location
    UK
    Posts
    323

    Your Own Encryption Algorithm

    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.
    KAZAR

    The Law Of Programming:

    As the Number of Lines of code increases, the number of bugs generated by fixing a bug increases exponentially.
    __________________________________
    www.startingqbasic.co.uk

  2. #2

    Thread Starter
    Hyperactive Member kazar's Avatar
    Join Date
    Apr 2006
    Location
    UK
    Posts
    323

    Re: Your Own Encryption Algorithm

    Right then, this is the decrypt code.


    VB Code:
    1. Private Sub Decrypt(sourcefile As String, destfile As String, encryptionkey As String, encryptionnumber As Long)
    2.  
    3.  
    4.  
    5. Close
    6. encodestring = encryptionkey
    7. If encodestring = "" Then GoTo skipall
    8.  
    9. swapnumber = encryptionnumber
    10.  
    11. If swapnumber = 0 Then GoTo skipall
    12.  
    13. Open sourcefile For Binary As #1
    14.  
    15. charactercount = 1
    16.  
    17.  
    18. Do Until EOF(1) = True
    19. Get #1, , storebinary(charactercount)
    20. charactercount = charactercount + 1
    21. Loop
    22.  
    23. Close #1
    24. encodenumber = 0
    25.     For i = 1 To Len(encodestring)
    26.          encodenumber = encodenumber + Asc(Mid(encodestring, i, 1))
    27.      Next i
    28.  
    29. Open destfile For Output As #1
    30. status_label.Caption = "Decrypting and Exporting to File"
    31.     On Error Resume Next
    32.     For i = 1 To charactercount
    33.        
    34.         currentchar = storebinary(i)
    35.        
    36.         If currentchar = 9999 Then
    37.         Print #1, ""
    38.        
    39.         GoTo skipthis
    40.         End If
    41.        
    42.         currentchar = currentchar / swapnumber
    43.         currentchar = currentchar - encodenumber
    44.        
    45.        
    46.          Print #1, Chr(currentchar);
    47.        
    48.          
    49.          Me.Refresh
    50. skipthis:
    51. Err = 0
    52.     Next i
    53.  Close
    54.  
    55. MsgBox "Decryption Complete", vbDefaultButton1, "Decrypted"
    56. GoTo skipall
    57.  
    58. filerror:
    59. MsgBox "File Error Detected", vbCritical, "File Error"
    60. Err = 0
    61. skipall:
    62. If Err <> 0 Then
    63. MsgBox "Error Encountered", vbCritical, "Error"
    64. End If
    65. Close
    66.  
    67. End Sub

    This is to be fair, not the most brilliant code in the world, and i would appreciate any suggestions for improvement.
    KAZAR

    The Law Of Programming:

    As the Number of Lines of code increases, the number of bugs generated by fixing a bug increases exponentially.
    __________________________________
    www.startingqbasic.co.uk

  3. #3
    PowerPoster
    Join Date
    Feb 2006
    Location
    East of NYC, USA
    Posts
    5,691

    Re: Your Own Encryption Algorithm

    1) You never use storebinary in the encode routine, so get rid of it.
    2) Change
    VB Code:
    1. For i = 1 To linecounter
    2. Line Input #1, stringcounter
    to
    VB Code:
    1. Do Until EOF(1)
    There's no reason to read through the file twice. (And there's no reason to test whether a binary is true twice - if <binary> and if <binary> = true yield the same result. [EOF() returns True or False].)

    Same thing in the decode routine - open the file as binary and read the bytes until EOF - no need to read them into an array first - and you can get rid of the array.

    Oh - and you have "On Error GoTo fileskip" twice in the encode routine, one right after the other.
    The most difficult part of developing a program is understanding the problem.
    The second most difficult part is deciding how you're going to solve the problem.
    Actually writing the program (translating your solution into some computer language) is the easiest part.

    Please indent your code and use [HIGHLIGHT="VB"] [/HIGHLIGHT] tags around it to make it easier to read.

    Please Help Us To Save Ana

  4. #4

    Thread Starter
    Hyperactive Member kazar's Avatar
    Join Date
    Apr 2006
    Location
    UK
    Posts
    323

    Re: Your Own Encryption Algorithm

    Thanks AL42, i'll change the eof thing and get rid of some useless code. btw, if you can break it, tell me, i am working on ways to better the encryption algorithm.
    KAZAR

    The Law Of Programming:

    As the Number of Lines of code increases, the number of bugs generated by fixing a bug increases exponentially.
    __________________________________
    www.startingqbasic.co.uk

  5. #5
    Hyperactive Member g4hsean's Avatar
    Join Date
    May 2006
    Posts
    267

    Red face Re: Your Own Encryption Algorithm

    Verry good algorithm but i still say the hardest encryptions gotta be manualy giving each letter an encryption string.
    example:

    VB Code:
    1. With Text1
    2. If .text = "A" Then
    3. .text = "9021079"
    4. End If

    it took me a day to do and damn i swear no one could break the file encryption on it unless they had my program and a couple of dissasembling tool's.I've sent it to Mozzila and they really got a kick out of it and they asked if they could use it on an upcoming experimental program
    Coding's a Breeze if you'r at ease! GOD THATS CORNY!!
    -

  6. #6

    Thread Starter
    Hyperactive Member kazar's Avatar
    Join Date
    Apr 2006
    Location
    UK
    Posts
    323

    Re: Your Own Encryption Algorithm

    however, as security goes it's pretty poor, i mean, the code never changes. So if someone has a file encrypted with your algorithm, that they know the clear text of, then they know the value of every single letter in your algorithm. If it doesn't change the algorithm every times it runs, it's pretty weak. Not only that, but u have no way of securing the file. Mine requires two codes which govern decryption and encryption. That means that only people with both codes can decrypt the file, not just someone with a copy of ur program
    KAZAR

    The Law Of Programming:

    As the Number of Lines of code increases, the number of bugs generated by fixing a bug increases exponentially.
    __________________________________
    www.startingqbasic.co.uk

  7. #7
    Hyperactive Member g4hsean's Avatar
    Join Date
    May 2006
    Posts
    267

    Re: Your Own Encryption Algorithm

    That is darn true i thank you for pointing that out for me. Ok im going to go hard at work and build an algorithm. It all makes sense now lol.
    Coding's a Breeze if you'r at ease! GOD THATS CORNY!!
    -

  8. #8

    Thread Starter
    Hyperactive Member kazar's Avatar
    Join Date
    Apr 2006
    Location
    UK
    Posts
    323

    Re: Your Own Encryption Algorithm

    Thats whats the forums here for.
    KAZAR

    The Law Of Programming:

    As the Number of Lines of code increases, the number of bugs generated by fixing a bug increases exponentially.
    __________________________________
    www.startingqbasic.co.uk

  9. #9
    New Member
    Join Date
    Jun 2006
    Posts
    3

    Re: Your Own Encryption Algorithm

    I'm a complete greeney to VB, but wanted to add 2-whole-cents worth to the general idea of building your own encryption/decryption algorithm. My previous software is in another, incompatible language (a 4GL), but here's two things I do there that make it better....

    1) It's often nice to map only "printable" ASCII characters to other "printable" ASCII characters. Since they have a contiguous block of byte values, this is easy by subtracting the minimum number and MOD-ing by the total number of printable characters....

    2) Shuffle the string before (and of course after) encryption. In other words....
    (a) take characters from the back forward
    (b) take characters from the middle outwards....
    (c) or any other unique shuffling mechanism....

    These both work great for the licensing and "fueling" utilities I've built - and must now rebuild in VB - for all my software products.

  10. #10
    Lively Member Agilaz's Avatar
    Join Date
    Jun 2006
    Posts
    98

    Re: Your Own Encryption Algorithm

    you ask for it, so here is the code to break this encryption

    VB Code:
    1. Private Sub bforce()
    2.     Dim ff As Integer, tlong As Long, x As Long, Y As Long, tchar As Single, z As Long
    3.     Dim bdata() As Long, cdata() As Long, arrdim As Long
    4.     Dim found As Boolean, cnt As Long
    5.    
    6.     Const maxkeylength As Long = 10
    7.    
    8.     ff = FreeFile
    9.    
    10.     Open App.Path & "\ecrypted.txt" For Binary As #ff
    11.  
    12.     Do
    13.         Get #ff, , tlong
    14.     Loop Until tlong <> 9999
    15.    
    16.    
    17.     arrdim = 10000
    18.     ReDim bdata(arrdim)
    19.     ReDim cdata(arrdim)
    20.  
    21.     For x = 0 To 255
    22.         For Y = 1 To (255 * maxkeylength) + 255
    23.             tchar = tlong / (x + Y)
    24.             If tchar = Fix(tchar) Then
    25.                 found = False
    26.                 For z = 0 To cnt
    27.                     If bdata(z) = tchar And cdata(z) = Y Then
    28.                         found = True
    29.                         Exit For
    30.                     End If
    31.                 Next z
    32.                 If found = False Then
    33.                     bdata(cnt) = CLng(tchar)
    34.                     cdata(cnt) = Y
    35.                     cnt = cnt + 1
    36.                     If cnt > arrdim Then
    37.                         arrdim = arrdim + 10000
    38.                         ReDim Preserve bdata(arrdim)
    39.                         ReDim Preserve cdata(arrdim)
    40.                     End If
    41.                 End If
    42.             End If
    43.         Next Y
    44.     Next x
    45.     ReDim Preserve bdata(cnt - 1)
    46.     ReDim Preserve cdata(cnt - 1)
    47.  
    48.     Do While cnt > 1 And Not EOF(ff)
    49.         Get #ff, , tlong
    50.         cnt = 0
    51.         For x = 0 To UBound(bdata)
    52.             If bdata(x) <> 0 And tlong > 0 And tlong <> 9999 Then
    53.                 tchar = (tlong / bdata(x)) - cdata(x)
    54.                 If tchar = Fix(tchar) And tchar > -1 And tchar < 256 Then
    55.                     cnt = cnt + 1
    56.                 Else
    57.                     bdata(x) = 0
    58.                     cdata(x) = 0
    59.                 End If
    60.             End If
    61.         Next x
    62.         If cnt < 256 Then Exit Do
    63.     Loop
    64.     Close #ff
    65.     Dim swapnumber() As Long
    66.     Dim encodenumber() As Long
    67.    
    68.     cnt = 0
    69.     For z = 0 To UBound(bdata)
    70.         If bdata(z) <> 0 Then
    71.             ReDim Preserve encodenumber(cnt)
    72.             ReDim Preserve swapnumber(cnt)
    73.             swapnumber(cnt) = bdata(z)
    74.             encodenumber(cnt) = cdata(z)
    75.             cnt = cnt + 1
    76.         End If
    77.     Next z
    78.    
    79. End Sub

    this procedure takes just a minute to create possible swapnumber/encodenumber combinations. most often less then 255 pairs which always contain the right combination.

    note that you don't need the password for decryption if you have the decryption code.

    the method to create the encodenumber from the password is weak. there are a lot of possible crypto collisions.
    for example the password abcd1234 results in the same encodenumber as 4a3b2c1d or 1234dcba etc.

    but even if you change that method...the encryption algo is still the same.

    i hope this will give you some ideas to create a better algo.

  11. #11

    Thread Starter
    Hyperactive Member kazar's Avatar
    Join Date
    Apr 2006
    Location
    UK
    Posts
    323

    Re: Your Own Encryption Algorithm

    congratulations agilaz, nice code.

    tho, to be fair, the final version won't give you the code to work from.
    KAZAR

    The Law Of Programming:

    As the Number of Lines of code increases, the number of bugs generated by fixing a bug increases exponentially.
    __________________________________
    www.startingqbasic.co.uk

  12. #12
    Lively Member Agilaz's Avatar
    Join Date
    Jun 2006
    Posts
    98

    Re: Your Own Encryption Algorithm

    IMHO an encryption algo is only good if you can't break it even if you have the source code. programs can be disassembled and reverse engineered.
    btw...the best encrpytion algos like Blowfish, Rijndael, Twofish and others are open source

  13. #13
    Hyperactive Member g4hsean's Avatar
    Join Date
    May 2006
    Posts
    267

    Re: Your Own Encryption Algorithm

    Who knows how the worlds best algorithm goes "cypherunicorn-A". Id like to see how they did it.
    Coding's a Breeze if you'r at ease! GOD THATS CORNY!!
    -

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width