TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

Typing The Letters A-E-S Into Your Code? You’re Doing It Wrong

258 pointsby cscottalmost 16 years ago

18 comments

tptacekalmost 16 years ago
It's probably worth pointing out: you have the same problems if you're using CFB, OFB, or CTR mode (these are the "stream cipher" modes for DES/AES/whatever that encrypt one byte at a time). There's apocrypha about these modes not being vulnerable to the attack. Bad apocrypha:<p>Set up an encryptor:<p><pre><code> irb&#62; e = OpenSSL::Cipher::Cipher.new('aes-256-ofb') =&#62; #&#60;OpenSSL::Cipher::Cipher:0x647100&#62; irb&#62; e.key = "\x11" * 32 irb&#62; e.iv = "\x00" * 16 </code></pre> A decryptor:<p><pre><code> irb&#62; d = OpenSSL::Cipher::Cipher.new('aes-256-ofb') =&#62; #&#60;OpenSSL::Cipher::Cipher:0x647120&#62; irb&#62; d.decrypt irb&#62; d.key = "\x11" * 32 irb&#62; d.iv = "\x00" * 16 </code></pre> Encrypt something:<p><pre><code> irb&#62; ciphertext = (e &#60;&#60; "A * 40") =&#62; "a\255N\211XEn\001\347$\275)\311%Ht\2356\254m\b\234z\375\311\006\335\305F\231~\201\243\236\3628w\267\3454" </code></pre> Make an XOR mask:<p><pre><code> irb&#62; mask = ("187 she wrote".to_bignum ^ ("A" * 13).to_bignum).to_rawstring =&#62; "pyva2)$a63.5$" </code></pre> XOR it into the ciphertext:<p><pre><code> irb&#62; new_ciphertext = (ciphertext.to_bignum ^ mask.to_bignum).to_rawstring </code></pre> NOW decrypt it:<p><pre><code> irb&#62; d &#60;&#60; nct =&#62; "AAAAAAAAAAAAAAAAAAAAAAAAAAA187 she wrote"</code></pre>
tptacekalmost 16 years ago
Hm. I wrote this for our normal blog readers, who live and breathe security stuff, so I don't know how well it'll carry here.
评论 #639713 未加载
评论 #639759 未加载
评论 #640152 未加载
评论 #639774 未加载
评论 #641015 未加载
smanekalmost 16 years ago
I didn't buy the GPG/TLS thing before. After reading this I do. I just realized how completely out of my depth I am for crypto stuff. At least now I know ...
theBobMcCormickalmost 16 years ago
Great post! I'd love to see more of this kind of thing; approachable explanations of the right way to do security and how easy it is to do it wrong.<p>Unfortunately, there's seems to be a <i>huge</i> gap between the average working programmer, who often has little idea how to do security, and the security guru's who are often barely intelligible to the rest of us. :-)<p>Sadly, the result of most programmer's poor understanding of security is even worse than the bad crypto implementation in the article. It's often something as basic as thinking it's O.K. to store user passwords un-hashed. (<a href="http://news.ycombinator.com/item?id=628680" rel="nofollow">http://news.ycombinator.com/item?id=628680</a>)
gojomoalmost 16 years ago
Is there a shorthand name for "the industry standard answer; the cookie both apps honor to let you in, encrypted so users can’t change their account to someone else’s" pattern, especially that pattern 'done right'?<p>What's the best battle-tested library (and call) for implementing exactly that, without making any of the common mistakes?
评论 #641065 未加载
tptacekalmost 16 years ago
I would just like to point out that in 13 hours nobody has posted a comment here that even bothered me a little, whereas it took less than 3 hours for Reddit to bust out the one-time pads.
jameskpolkalmost 16 years ago
Thanks, Thomas. I just finished implementing my own crypto in a webapp I am working on. (AES, with Diffie Hellman for a shared secret we needed)<p>You've made me so nervous about everything I thought was true that I did a hg revert and am looking at gpgme bindings.<p>You've done a good deed, I think.
评论 #640032 未加载
yanalmost 16 years ago
Good read. +1 for Louis CK reference.<p>A question to start up founders: How do you guys handle security? Do you hire consultants/pen-testers? Do you just copy+paste code from the internet? Do you use whatever framework's components for it?
评论 #639804 未加载
jrockwayalmost 16 years ago
<i>A “young, cool-people’s” coffee shop on the first floor of an old office building in downtown Chicago.</i><p>A place like this exists? All I can think of is Intelligensia.
评论 #639829 未加载
评论 #639826 未加载
amalconalmost 16 years ago
It seems kind of silly to write one's own code for anything, if you have access to a well-tested library that does what you want. The only reason to do otherwise is where there is no such library. Crypto is just an example of an area that's particularly hard to get right: the solution is no different than for other things.
评论 #639790 未加载
CGamesPlayalmost 16 years ago
It was a nice read, but the screenplay delivery added a lot of confusion that I felt detracted from the point of the actual message. Maybe if it hadn't been so fragmented. Still, educational, though moderately confusing.
评论 #640040 未加载
shoalmost 16 years ago
Isn't the whole problem in this situation that you are trusting the client with critical data? He takes possession of it, has unlimited time and opportunity to work on it, and successful falsification will be obvious for him? Why on earth trust the client with the data in the first place? I have never liked the "encrypted cookie" way of handling session storage.<p>Just have a server side session store and all of this cookie encryption crap just vanishes. Of course that won't help you with session fixation etc, but the post doesn't address that stuff either. Do it over TLS and you're pretty safe though.<p>And let's not forget the author's suggestion to pad out the cookie with 1000 bytes to make it harder to falsify. That cookie gets send with every single request. 20 images on the page, you're sending 20KB of junk up just to load the page. On a connection with slow upstream, like say ADSL, you can easily add a second or two of request latency. You might not care but some people sure do.<p>And come on, I read until the very end expecting to hear what "processes or threads" have to do with security, so tell us already!
评论 #640051 未加载
评论 #640207 未加载
jermyalmost 16 years ago
So, is there much to be gained from encryption anyway? If, as the candidate, I suggested sending a cookie as 'userId=39493&#38;role=user&#38;timestamp=1414919&#38;hash=&#60;sha256-of-key-with-data&#62;' then would I lose brownie points?<p>I assume my hash comparison function is constant time (eg. XOR(a[x],b[x])==0), rather than comparing of char-by-char.
评论 #639764 未加载
评论 #640436 未加载
jimflalmost 16 years ago
I always try to espouse the benefits of triple-rot13 as the cipher in interview questions about security. Key management is a breeze, for example, and it's VERY fast.<p>But it allows everyone to concentrate on how you're managing the secure context instead of what algorithm has been shown to cause cancer in rats.
DenisMalmost 16 years ago
I don't understand what's so hard about encryption. There are simple, well-known rules (except the timing one that is sort of news), and if you follow them you should be safe, no?<p>1. Use a blocks-size unique prefix (IV) for each message (random will do as well)<p>2. SHA-256 your entire message before encryption and add the hash value at the end to prevent tampering<p>3. Use AES-256 with chaining to encrypt<p>4. Use SHA-256 to turn a password into a key. If your key space is small harden it with hashing 1000 times or so.<p>5. The time your algorithm takes should be always the same (or at worst one time for success and one for failure)<p>6. Don't use any other symmteric crypto algorithm.
评论 #640048 未加载
paulgbalmost 16 years ago
Great post, not that I can claim to understand all of it. One question:<p><i>Just put a “for” loop around SHA-1 and run it 1000 times to generate the key; that’ll at least slow down a brute force attack. SHA-1 is lightning fast. By itself, it’s a crappy way to generate a key.</i><p>I see the purpose of this, but doesn't it also provide 999 more opportunities for a hash collision to happen? Are you so confident in the collision resistance of SHA-1 that this isn't an issue?<p>My (probably naive) proposal would be to keep the result of the first hash and XOR it with the result of each subsequent hash. Bad idea?
评论 #641301 未加载
arsalmost 16 years ago
Related to this: <a href="http://www.codinghorror.com/blog/archives/001267.html" rel="nofollow">http://www.codinghorror.com/blog/archives/001267.html</a> I assume.
评论 #639978 未加载
snorkelalmost 16 years ago
As far as doing it wrong can we go back to "app ‘A’ set a cookie with your account ID in it" and discuss why on Earth the app works like that???
评论 #640053 未加载