home *** CD-ROM | disk | FTP | other *** search
/ Planet Source Code Jumbo …e CD Visual Basic 1 to 7 / 4_2005-2006.ISO / data / Zips / Crash_Encr196996252006.psc / CrashEncryption2 / CryptAction.bas next >
BASIC Source File  |  2006-02-03  |  12KB  |  279 lines

  1. Attribute VB_Name = "CryptAction"
  2. '|||||||||||||||||||||||||||||||||||Disclaimer|||||||||||||||||||||||||||||||||||||'
  3. ' This code is written by Matthew Kernes. It is not intended for commercial use.   '
  4. ' It has no warranty nor is Matthew Kernes responsible for any damage it does to   '
  5. ' any computer it runs on. Any changes made to this software after the day October '
  6. ' 24th, 2005 by anyone other then Matthew Kernes is liabile for the changes and    '
  7. ' Matthew Kernes is not responsible for those changes or the problems they may     '
  8. ' cause.                                                                           '
  9. '||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||'
  10.  
  11. ' I wrote this code because I wanted to see if I could make an unhackable encryption.
  12. ' I know there is no such thing as "unhackable" encryption. But I'd like to see how fast
  13. ' someone can break the code. Originally, I created a program that uses the Ceaser Cypher,
  14. ' but I didn't know it at the time. I was pretty new to encryption and I feel I still am.
  15. ' But I lay the challenge out now to anyone who might want it. Can you break this encryption?
  16.  
  17. ' I wrote the software with 2 ideas in mind.
  18. '    - How many varaibles does it take to make it so you can't solve for x?
  19. '    - How do you make it so the user cannot control the key or password?
  20.  
  21. ' With this in mind, I set out to make what is now "Crash Encryption".
  22. ' I dubbed it "Crash Encryption" because this program is somewhat a resource hog,
  23. ' as well, my nick-name is "Crash".
  24.  
  25. ' If you like what you see and have comments or questions, please feel free
  26. ' to email me at compiano@socal.rr.com. Voting is not necessary on my code.
  27.  
  28. ' Thanks for the view,
  29. '                   Matthew Kernes (Crash)
  30.  
  31.  
  32. ' I'd like to thank Derek Haas for the great I/O module. It's saved me a LOT of time and
  33. ' it's probably the easiest I/O mod to use that I've seen.
  34.  
  35.  
  36. ' +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  37. ' THIS IS THE HEART OF MY ENCRYPTION. IT IS THE SINGLE MOST IMPORTANT PART OF THE
  38. ' PROGRAM. I'VE COMMENTED IT PRETTY HIGHLY AND HOPE YOU CAN UNDERSTAND IT AS WELL
  39. ' AS I CAN. IF YOU HAVE QUESTIONS, YOU CAN EMAIL ME.
  40.  
  41.  
  42. Private Type KeyType ' Create a key type.
  43.         bitloc(7) As Integer ' 8 bits in a byte. This is the location in our string.
  44.         bitsLen As Integer ' The full length of the bin string.
  45. End Type
  46.  
  47.  
  48. Option Explicit
  49. Public Const CrashID As String = "<|-Crash-Encryption-2-|>"  ' This is going to be our identifier.
  50.  
  51. Public Function encryptFile(File2Encrypt As String, SaveDataFileAs As String, SaveKeyFileAs As String, _
  52.                             FileTitle As String, Complexity As Integer, Optional Progbar As ProgressBar, _
  53.                             Optional StatusLabel As Label)
  54.  
  55. ' IMPORANT! - The complexity must be between 8 and 50. At 8, there are NO random bits inserted. I use a minimum of 10.
  56.  
  57. Dim EncFile As BitFile ' File we're getting data from (i.e. picture, document, zip, etc.)
  58. Dim KeyFile As BitFile ' File we're saving the key to.
  59. Dim SaveFile As BitFile ' File we're saving the encrypted data to.
  60. Dim oBits(7) As Integer ' This is going to be 0 - 8 of our bits in our byte.
  61. Dim nBits(50) As Integer ' This is going to be up to 50 possible new bits for our byte.
  62. Dim ByteKey As KeyType ' This is our key for the single byte.
  63. Dim oB As Integer ' This is our universal interger to count with.
  64. Dim oB2 As Integer ' This is another one of our universal intergers to count with.
  65. Dim tmpBitLoc As Integer ' Temporary bit holder.
  66. Dim bitFound As Boolean ' Specify when we use a data bit.
  67. Dim CarryKey As Integer ' This is to specify how many times we will use the same keyset. More times saves space in the key.
  68. Dim CariedKey As Integer ' Our CarryKey place holder.
  69.  
  70. CarryKey = 51 - Complexity ' 41 possible carriers.
  71. CariedKey = 1 ' Set the carrier to be reset on first use.
  72.  
  73. ' Be sure we're working with seperate file names.
  74. If File2Encrypt = SaveDataFileAs Then MsgBox "Source File, Destination File, and Key File must all have different names.", vbExclamation, "Cannot Encrypt.": Exit Function
  75. If File2Encrypt = SaveKeyFileAs Then MsgBox "Source File, Destination File, and Key File must all have different names.", vbExclamation, "Cannot Encrypt.": Exit Function
  76. If SaveDataFileAs = SaveKeyFileAs Then MsgBox "Source File, Destination File, and Key File must all have different names.", vbExclamation, "Cannot Encrypt.": Exit Function
  77.  
  78.  
  79. ' Open our files for reading and writing.
  80. EncFile = OpenIBitFile(File2Encrypt) ' Source File
  81. SaveFile = OpenOBitFile(SaveDataFileAs) ' Destination File
  82. KeyFile = OpenOBitFile(SaveKeyFileAs) ' Key File
  83.  
  84.  
  85. 'Here we add the file name to the key so that we will know what to call the extracted file at decryption time.
  86. For oB = 1 To Len(CrashID & FileTitle & CrashID) ' I add a crashID to each end of the file name to make sure it is completely visible by our system.
  87.         oB2 = Asc(Mid(CrashID & FileTitle & CrashID, oB, 1))
  88.         OutputBits KeyFile, oB2, 8 ' Write these bytes 1 by 1 so that the file will only need to be openned once.
  89. Next oB
  90.  
  91.  
  92. Dim EncFileLen As Long ' We need to see how big in bytes this file is.
  93. EncFileLen = LOF(EncFile.FileNum) ' Get the length.
  94.  
  95.  
  96. Progbar.Max = EncFileLen ' Setup the progress bar (obviously.)
  97. Progbar.value = 0 ' And make sure you reset it.
  98. StatusLabel = "Encrypting Data: 0 Bytes" ' First label use.
  99.  
  100. 'We create our CarryKey value.
  101. OutputBits KeyFile, CarryKey, 8 ' Write to the key file what our carrier number is.
  102.  
  103.  
  104. Dim X As Long ' Create our X for our for x run.
  105. For X = 1 To EncFileLen ' 1 to the end of the file.
  106.         
  107.         
  108.         
  109.         'Get our byte to work with.
  110.         For oB = 0 To 7
  111.             oBits(oB) = InputBit(EncFile) ' Put the bits in our temporary array.
  112.         Next oB
  113.         
  114.         
  115.         
  116.         CariedKey = CariedKey - 1 ' Take 1 token off our carrier to keep tally.
  117.         If CariedKey = 0 Then ' If it's time to create a new key.
  118.                 'Create our random bits and our random string length.
  119.                 Randomize
  120.                 ByteKey.bitsLen = Int(Rnd * (Complexity - 10)) + 10 ' Random number 10 - 50
  121.                 
  122.                 For oB = 0 To 7 ' Clear our bit locations
  123.                     ByteKey.bitloc(oB) = 60
  124.                 Next oB
  125.                 
  126.                 For oB = 0 To 7 ' Create our byte string. '-1' for 0 to 49.
  127. getnewbitloc:         ' This is our return to try again...
  128.                     tmpBitLoc = Int(Rnd * ByteKey.bitsLen)  ' get a temporary bit location in our string.
  129.                     For oB2 = 0 To oB  ' Check to see if this location is taken.
  130.                         If ByteKey.bitloc(oB2) = tmpBitLoc Then GoTo getnewbitloc  ' if it is, go back and try again.
  131.                     Next oB2
  132.                     ByteKey.bitloc(oB) = tmpBitLoc ' Empty location, save it.
  133.                 Next oB
  134.             
  135.                 CariedKey = CarryKey ' Reset our carrier.
  136.         End If
  137.         
  138.         
  139.         
  140.         'Create our full string to be written.
  141.         For oB = 0 To ByteKey.bitsLen
  142.             bitFound = False ' reset our trigger
  143.             For oB2 = 0 To 7 ' check to see if we're going to use a data bit.
  144.                 If ByteKey.bitloc(oB2) = oB Then
  145.                     nBits(oB) = oBits(oB2) ' We insert a data bit.
  146.                     bitFound = True ' Set our trigger
  147.                     Exit For
  148.                 End If
  149.             Next oB2
  150.             
  151.             If bitFound = False Then
  152.                     nBits(oB) = Int(Rnd * 2) + 1 ' if the bit wasn't triggered, insert a fake bit.
  153.                     If nBits(oB) = 2 Then nBits(oB) = 1 Else nBits(oB) = 0
  154.             End If
  155.         Next oB
  156.         
  157.         If CariedKey = CarryKey Then ' If the carrier was reset, write the key to the file.
  158.             'Write the key to the file.
  159.             'Write To File bits 0-7 & strlen
  160.             For oB = 0 To 7
  161.                 OutputBits KeyFile, ByteKey.bitloc(oB), 8
  162.             Next oB
  163.             
  164.             OutputBits KeyFile, ByteKey.bitsLen, 8
  165.         End If
  166.         
  167.         'Write our new data to the data file.
  168.         For oB = 0 To ByteKey.bitsLen - 1
  169.             OutputBit SaveFile, Val(nBits(oB))
  170.         Next oB
  171.         
  172.         Progbar.value = X
  173.         
  174.  
  175.         
  176.         If X Mod 100 = 0 Then StatusLabel = "Encrypting Data:" & Str(Loc(EncFile.FileNum)) & " Bytes": DoEvents
  177.         
  178. Next X
  179.  
  180. StatusLabel = "Closing Files..."
  181.  
  182. 'Close our open files so they can be used while this application is open.
  183. CloseOBitFile SaveFile
  184. CloseOBitFile KeyFile
  185. CloseIBitFile EncFile
  186.         
  187. End Function
  188.  
  189. Public Function decryptFile(EncFileName As String, KeyFileName As String, saveFileName As String, Optional Progbar As ProgressBar, Optional StatusLabel As Label) As String
  190. Dim EncFile As BitFile ' File we're getting data from (i.e. picture, document, zip, etc.)
  191. Dim KeyFile As BitFile ' File we're saving the key to.
  192. Dim SaveFile As BitFile ' File we're saving the encrypted data to.
  193. Dim oBits(7) As Integer ' This is going to be 0 - 8 of our bits in our byte.
  194. Dim oB As Integer ' This is our universal interger to count with.
  195. Dim oB2 As Integer ' This is another one of our universal intergers to count with.
  196. Dim KeyInfo(8) As Integer ' This will store our temp data from our key.
  197. Dim TempBits(50) As Integer ' This is our temporary string
  198. Dim FileTitle As String ' This is the original title of the encrypted file.
  199. Dim CarryKey As String ' This is the first 5 bits in the key that tells us how many times to use each key.
  200. Dim CariedKey As Integer ' Our CarryKey place holder.
  201.  
  202.  
  203. ' Open our files for reading and writing.
  204. EncFile = OpenIBitFile(EncFileName) ' Our Data File
  205. KeyFile = OpenIBitFile(KeyFileName)  ' Key File
  206. SaveFile = OpenOBitFile(saveFileName)  ' Destination File
  207. 'Open KeyFileName For Input As #15
  208.  
  209.  
  210. Progbar.value = 0 ' Reset the progress bar.
  211. Progbar.Max = LOF(KeyFile.FileNum) * 1.01 ' Setup the progress bar.
  212. ' I added the ".01" because there is a small amount of bit-wise overhead that I didn't want to get introuble over.
  213.  
  214.  
  215. 'Get the original filename.
  216. Do
  217.     FileTitle = FileTitle & Chr(InputBits(KeyFile, 8)) ' We add our bytes (8 bits at a time) to our string.
  218.     If InStr(Len(CrashID) + 1, FileTitle, CrashID, vbTextCompare) Then Exit Do ' if we have 2 CrashIDs then we have our file name.
  219. Loop
  220. FileTitle = Replace(FileTitle, CrashID, "") ' Get rid of the crashIDs from the name.
  221.  
  222. StatusLabel = "Decrypting: 0 Bytes"
  223.  
  224. 'Decryption time. This is the most simple part of the show.
  225. On Error GoTo pof ' Sometimes if a file is too small, the progress bar will cause an error. I just ignore it.
  226.  
  227. CarryKey = InputBits(KeyFile, 8) ' Get our key use number.
  228. CariedKey = 1
  229. Do
  230.  
  231.     'Get our key string for our first bit.
  232.     ' 9x8 bits. 8 bits for each of the real bit locations and 8 bits at the end to tell us the string length.
  233.     CariedKey = CariedKey - 1
  234.     If CariedKey = 0 Then
  235.     For oB = 0 To 8
  236.         KeyInfo(oB) = InputBits(KeyFile, 8)  ' KeyInfo(8) is the string length.
  237.         Progbar.value = Loc(KeyFile.FileNum)
  238.     Next oB
  239.     CariedKey = CarryKey
  240.     End If
  241.     
  242.     If EOF(KeyFile.FileNum) Then Exit Do ' If we're empty, there's nothing more for us to do. Exit do.
  243.     
  244.     'Grab our string of bits that we will extract the original real bits from.
  245.     For oB = 0 To KeyInfo(8) - 1
  246.         TempBits(oB) = InputBit(EncFile)
  247.     Next oB
  248.     
  249.     
  250.     'Reorder the bits in the byte from our trusty map we created.
  251.     For oB = 0 To 7
  252.         oBits(oB) = TempBits(KeyInfo(oB))
  253.     Next oB
  254.     
  255.     
  256.     'Write the byte to the file now that we have it configured just how we want.
  257.     For oB = 0 To 7
  258.         OutputBit SaveFile, Val(oBits(oB))
  259.     Next oB
  260.     
  261.  
  262.     If Loc(KeyFile.FileNum) Mod 100 = 0 Then StatusLabel = "Decrypting:" & Str(Loc(KeyFile.FileNum)) & " Bytes": DoEvents
  263.  
  264.     
  265. Loop
  266.  
  267. 'Close our open files so they can be used while this application is open.
  268. CloseOBitFile SaveFile
  269. CloseIBitFile EncFile
  270. CloseIBitFile KeyFile
  271.  
  272. decryptFile = FileTitle
  273. Exit Function
  274. pof:
  275. If Err = 380 Then Resume Next
  276. MsgBox Err.Description, vbCritical, "Error: " & Err
  277. Resume Next
  278. End Function
  279.