home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #30 / NN_1992_30.iso / spool / comp / lang / c / 18428 < prev    next >
Encoding:
Internet Message Format  |  1992-12-15  |  11.4 KB

  1. Xref: sparky comp.lang.c:18428 comp.lang.c++:18024 comp.lang.pascal:7494
  2. Newsgroups: comp.lang.c,comp.lang.c++,comp.lang.pascal
  3. Path: sparky!uunet!newsgate.watson.ibm.com!yktnews!admin!wo0z!lwloen
  4. From: lwloen@rchland.vnet.ibm.com (Larry Loen)
  5. Subject: Re: Encryption algorythm/code needed...
  6. Sender: news@rchland.ibm.com
  7. Message-ID: <1992Dec15.174920.16036@rchland.ibm.com>
  8. Date: Tue, 15 Dec 1992 17:49:20 GMT
  9. Distribution: na
  10. Reply-To: lwloen@rchland.vnet.ibm.com
  11. Disclaimer: This posting represents the poster's views, not necessarily those of IBM
  12. References: <Byt18n.6Cr@andy.bgsu.edu> <1992Dec7.160128.18678@sbcs.sunysb.edu> <1992Dec09.200208.11243@rchland.ibm.com> <1992Dec11.113831.2520@doug.cae.wisc.edu>
  13. Nntp-Posting-Host: wo0z.rchland.ibm.com
  14. Organization: IBM Rochester
  15. Lines: 189
  16.  
  17. In article <1992Dec11.113831.2520@doug.cae.wisc.edu>, mccullou@cae.wisc.edu (Mark McCullough) writes:
  18. |> 
  19. |> In article <1992Dec09.200208.11243@rchland.ibm.com>, lwloen@rchland.vnet.ibm.com (Larry Loen) writes:
  20. |> |> In article <1992Dec7.160128.18678@sbcs.sunysb.edu>, clane@csws2.ic.sunysb.edu (Charles F Lane) writes:
  21. |> |> |> In article <Byt18n.6Cr@andy.bgsu.edu> jzawodn@bgsu.edu (jeremy zawodny) writes:
  22. |> |> |> >I am writing a small program to help secure some computers from
  23. |> |> |> >unauthorized access.  I have alread writen the interface and
  24. |> |> |> >unsername/password checking routines.  Now what I need is a way to encrypt
  25. |> |> |> >the usernames and passwords.  The code need not be extravagent...  I only
  26. |> |> |> >need to encrypt single words (letters and numbers both).
  27. |> |> |> >
  28. |> |> |> >I can use either Pascal, C, or C++ code.
  29. |> |> |> >Thanks for any help!
  30. |> |> |> >Jeremy
  31. |> |> |> >Jeremy Daniel Zawodny                           Computer Science Undergraduate
  32. |> |> |> >                      Bowling Green State University
  33. |> |> |> A very easy way to encrypt data is to use a random number generator.  Suppose
  34. |> |> |> you have a string "CHAIR".  Assign a value to your random number generator's
  35. |> |> |> seed.  Then add random(256) to the ord of the first character (C, in this
  36. |> |> |> case), then random(256) + ord("H"), random(256) + ord("A"), etc.  When adding
  37. |> |> |> the random number you should wrap around if the sum is > 255.  In tp just
  38. |> |> |> disable range checking.  Decrypting is just as easy -- reassign the original
  39. |> |> |> seed value and subtract the "random" number rather than adding.  Code (TP) to
  40. |> |> |> encrypt/decrypt a string follows:
  41. |> |> |> 
  42. |> |> |> procedure EncryptString(var s : string; seed : LongInt);
  43. |> |> |> var i : word;
  44. |> |> |> begin
  45. |> |> |>  RandSeed := seed;
  46. |> |> |>  {$R-}
  47. |> |> |> (* ^^^--Disable range checking.  To be honest, I'm not sure if this is the   *)
  48. |> |> |> (*      proper directive for disabling range checking.  Or if range checking *)
  49. |> |> |> (*      can be disabled locally.  Check your manual because mine's not with  *)
  50. |> |> |> (*      me right now!                                 *)
  51. |> |> |> 
  52. |> |> |>  for i := 1 to ord(s[0]) do s[i] := s[i] + random(256);
  53. |> |> |> 
  54. |> |> |>  {$R+}
  55. |> |> |> (* ^^^--Enable range checking, see above comment *)
  56. |> |> |> end;
  57. |> |> |> 
  58. |> |> |> procedure DecryptString(var s : string; seed : LongInt);
  59. |> |> |> var i : word;
  60. |> |> |> begin
  61. |> |> |>  RandSeed := seed;
  62. |> |> |>  {$R-} (* See above comment *)
  63. |> |> |>  for i := 1 to ord(s[0]) do s[i] := s[i] - random(256);
  64. |> |> |>  {$R+} (* See above comment *)
  65. |> |> |> end;
  66. |> |> |> 
  67. |> |> |> That's it!  Apply the method to data other than strings, as well.  Just be sure
  68. |> |> |> to pass the same value to the seed parameter of both procedures.  No one will
  69. |> |> |> be able to decipher the encrypted string without knowing the algorithm AND 
  70. |> |> |> the seed value.  This is a guess because I never tried, but don't writeln
  71. |> |> |> an encrypted string to the screen because it may have control characters in
  72. |> |> |> it.
  73. |> |> |> 
  74. |> |> |> -- 
  75. |> |> |>                           | "So who is this Al Gorithm guy they keep
  76. |> |> |> Charles F. Lane           |  mentioning in my Computer Science classes?"
  77. |> |> |> clane@Libws1.ic.sunysb.edu|
  78. |> |> 
  79. |> |> Unfortunately, this is a well known _poor_ encryption algorithm.
  80. |> |> 
  81. |> |> Encryption and its subject, cryptology, has a lot of suprising twists and
  82. |> |> turns and is generally counter-intuitive.  For one thing, the people 
  83. |> |> one is trying to defeat are dishonest people and will get past any barrier
  84. |> |> by hook or by crook.
  85. |> |> 
  86. |> |> Mr Lane has re-invented a known cipher that has been solved many times.
  87. |> |> It is a worthy concept; it just happens not to work very well.
  88. |> |> One problem:  If someone is at all serious about the penetration attempt,
  89. |> |> one can expect them to get their hands on the source code.  Even if the
  90. |> |> "seed" is customized for each instalation, the above code fragment is
  91. |> |> enough to permit overall failure under a variety of circumstances that I
  92. |> |> will omit in the interests of space.  I can't predict all of them, but
  93. |> |> the odds against success using the scheme above are so high as to make
  94. |> |> further analysis not worth it.  The only people this kind of scheme defeats
  95. |> |> would also be defeated by simply XORing each byte with 0x55.  I should
  96. |> |> also point out that this scheme has been re-invented so many times, it
  97. |> |> might be simply "tried out" by an opponent even with no source code.  This
  98. |> |> sort of thing is often tried, because really good schemes take so long to
  99. |> |> solve, it is easy to try the known, weak ones in parallel.
  100. |> |> 
  101. |> |> I suggest anyone interested in adding encryption to their application
  102. |> |> for any reason first consult the many fine books now available on the 
  103. |> |> subject.  It is easy to add stuff that looks really good that is really
  104. |> |> worthless.
  105. |> |> 
  106. |> |> I've seen this basic problem this twice today; it is almost worth adding a 
  107. |> |> note to the FAQ.  Cryptology is not an easy place to do re-use and it is kind
  108. |> |> of a special case all around.  Here are some good, 
  109. |> |> widely available references:
  110. |> |> 
  111. |> |>     H. F. Gaines, Cryptanalysis, Dover, 1956 [originally 1939, as
  112. |> |>         Elementary Cryptanalysis]
  113. |> |> 
  114. |> |>     Abraham Sinkov, Elementary Cryptanalysis, Math. Assoc. of Amer.,
  115. |> |>         1966
  116. |> |> 
  117. |> |>     D Denning, Cryptography and Data Security, Addison-Wesley, 1983
  118. |> |> 
  119. |> |>     Alan G. Konheim, Cryptography:  A Primer, Wiley-Interscience, 1981
  120. |> |> 
  121. |> |>     Meyer and Matyas, Cryptography:  A New Dimension in Computer Data
  122. |> |>         Security, John Wiley & Sons, 1982.
  123. |> |> 
  124. |> |> There are also some legal issues, different for each country, on using
  125. |> |> encryption technology in products.  Export and Import can be problems, even
  126. |> |> for schemes which aren't any good.
  127. |> |> 
  128. |> |> Any of the references should give enough information to the aspiring designer 
  129. |> |> to have an idea of what they are getting into.  Meyer and Matyas or
  130. |> |> Denning are probably the most relevant to what I've seen here, but all should
  131. |> |> help out a lot.
  132. |> |> 
  133. |> |> -- 
  134. |> |>    Larry W. Loen        |  My Opinions are decidedly my own, so please
  135. |> |>                         |  do not attribute them to my employer
  136. |> 
  137. |> The basic technique, frequently known as a one time cypher pad, is one of
  138. |> the most secure algorithms of all.  The algorithm above doesn't work, but
  139. |> a slight modification does.  You create say a 20 element long array of
  140. |> random numbers, in some range greater than or equal to 256.  In other 
  141. |> words, generate random numbers between 0 and some number larger than
  142. |> 256.  Put each number in the program itself, and destroy your copy of
  143. |> those numbers.  Then you do add the ord of each character to the appropriate
  144. |> random number and put it mod 256.  You need to generate two procedures,
  145. |> one to code, the other to uncode, so you need to put in the numbers twice.
  146. |> The key to keeping this secure is that the random number string must
  147. |> be made unreadable by compilation, ie, can not be read from a file.
  148. |> The larger the range of random numbers, the harder to crack the code.
  149. |> 
  150. |> Note, that the algorithm I described isn't easy to code, but it is 
  151. |> secure, you can't see my random numbers, and I can safely store the
  152. |> encrypted version on disk.  I load the encrypted version, unencrypt, and
  153. |> compare with what they have entered.  This takes a loading the entire 
  154. |> list of passwords, but that isn't too hard.  If you really want to make
  155. |> it secure, make the unencryption, a variation of the encryption algorithm.
  156. |> i.e. don't make a separate unencryption, make a special procedure that
  157. |> calls the encryption to unencrypt.
  158. |> :)M^2
  159.  
  160. Nope, sorry, it really is not that easy.  I've been here before.  I say
  161. "this is a known poor encryption".  Then, the poster patches it up.  Then,
  162. I say, "but it still doesn't work" and it gets patched again.  Eventually,
  163. one of us gives up, usually me, but the system still isn't any good.  This
  164. basic idea set is very attractive to beginners, but has never yet led to a
  165. working system that one can count on to defeat a serious, intelligent, 
  166. knowledgable opponent.
  167.  
  168. Crypto just isn't that easy.  The conditions for a one-time pad are
  169. very tough to arrange and the circumstances cited above _do not qualify_.  
  170. Your new scheme is a little improved, but is still vulnerable.  It
  171. has only a superficial resemblance to a one-time pad.  Let's
  172. not debate it here -- you can come up with any number of schemes
  173. but you've gotta go out and learn how to do what you want to do.  A true
  174. one-time pad is probably not feasible in this application.  True one-time
  175. pads are _provably_ indecipherable, but achieving its proper environment 
  176. is _so_ cumbersome, it cannot be used in many applications.  That's why DES 
  177. and RSA are around at all; if one-time pads worked so easily, it'd be the 
  178. only thing used.  Anything other than a _true_ one time pad is just another
  179. proposed cipher system and is more or less vulnerable compared to others.
  180.  
  181. There's a lot of people, here and there, that have studied crypto more seriously 
  182. than I have (and I have studied it a long time).  They will make mincemeat of 
  183. the above scheme.  Under the right circumstances, I may be able to defeat
  184. your scheme.  For example, are the starting seeds the same for every
  185. file?  If so, all I need is two to ten different files and I don't care how
  186. you generate your "random" numbers or where you store them.  Will the same
  187. bytes of a given file get XORed with the same value?  Then I can probably
  188. defeat your scheme by getting access to the system as the lowliest user and
  189. simply change my password two to ten times.  Even if you 
  190. avoid those mistakes, there are a dozen others that basically can be
  191. expected to blast this scheme to bits.  You are unlikely to avoid all of
  192. them, because you don't know what they are.  There _is_ no security without
  193. studying it.  You can't fake it.  At the least, you have to have experience
  194. in breaking ciphers to invent them.  I know this sound annoying, but it happens
  195. to be true.  If you doubt it, I suggest reading David Kahn's excellent book
  196. "The Codebreakers", available in hardcover in most libraries.  After you've
  197. finished a few chapters of it, you will realise that it is easy to _seem_
  198. to have encrypted something and hard to have really _done_ it.
  199.  
  200. Sorry to be so annoying about it, but I've been here many times with many
  201. intelligent people.  It still takes a while to sink in.  It looks sooo easy. . .
  202.  
  203. -- 
  204.    Larry W. Loen        |  My Opinions are decidedly my own, so please
  205.                         |  do not attribute them to my employer
  206.