Ccmmutty logo
Commutty IT
0 pv8 min read

AESで暗号化

https://cdn.magicode.io/media/notebox/pexels-cottonbro-7319078.jpg
人には誰しも隠ぺいしたいものがありますよね
crypto = require "crypto"

createKeyHash = (pass)->
  hash = crypto.createHash 'sha256'
  hash.update pass
  hash.digest().toString("hex").substr(0,32)

createIV = (pass)->
  hash = crypto.createHash 'sha256'
  hash.update pass
  hash.digest().toString("hex").substr(32,16)

lock = (val, pass)-> new Promise (f,r)->
  deflate val
  .then (comp)->
    cipher = crypto.createCipheriv 'aes-256-cbc', createKeyHash(pass), createIV(pass)
    crypted = cipher.update comp, 'utf-8', 'hex'
    crypted += cipher.final 'hex'
    f crypted
  .catch (e)-> r e

unlock = (cry, pass)-> new Promise (f,r)->
  decipher = crypto.createDecipheriv 'aes-256-cbc', createKeyHash(pass), createIV(pass)
  decode = decipher.update cry, 'hex', 'utf-8'
  decode += decipher.final "utf-8"
  inflate decode
  .then (pt)->
    f pt
  .catch (e)-> r e
このように書きました。deflateとinflateに関しては前回を参照してください。

暗号化

では実際にできるかやってみます。
textは前回と同じくbacon ipsumです。
https://baconipsum.com/?paras=4&type=all-meat&start-with-lorem=1&make-it-spicy=1
lock text, "key"
.then (c)->
  console.log Buffer.from(c).length
  console.log c
とやりますと
2944
3b5ff06135aa30b4f2fe198d5bfb67cf50e433f65cd56ac6b76abb4792b5e62f76d2dc0377ba88d23810a1f10dcfc348c3ad318fdd69f7278bd820c590b7a53bbd272fe95652593f4a1c3a4530f7b8cd3f0807abbdb6100bc80202b53fe4f6a2d4b944f1753d6bba1353b1cc0a738a529009c2bc4b154cc3d1719480d07138f5fc6fea6812306eec14d1d144cfedbb34de97c58dec790150d9504b018942ea986f0d7a4f498b40808fe1e4b08509dc0c74ef4bb8b4f98af2b2ca9c577c91567fdbd59f84f8a540db51df651f8776f5066e79b5966f5da1643f9d43ab23f221d36ec3ce3b7547c20bfa895c4e1fba7eb58d7381bb20b9ca77fb2b9afe322bf8246079c7b747bd6261613cee4c21da46efa8b43455e919f4591881ecf426fa12835f4f327a225ae780c10e3ea0b7f3fadbb31ba794d6a880ab49fdafe21176af753c34467350198998022c7256c5db7f628f039fc3e5e9bb24fa9fab5f9b1953d55d24332b90b9d2648d75855181079c5c5470d7e831f05e687f7b80fa0460ef9d4b0050efbe61e7240689c1a4fc29f58ddea685796e9bc0071d2dbc022ba0adf9193d0a0067d51b3a0cf01c62d148c7b1acb6ac9315c42fb56a3df471684a05e3020ccf00972adf790f8858052e0fd13911c2248eda5666c8a2f608ace4fea3e17d4b6af80c25c74d4ea2c763aa42486644c6964ffeda0b2c9861836c11c5ff5cdd32d55161d2a2aaf4e3518b69f025276ca22f2ca9acbf3062f900f244c897fea3a782beb0ce29580e5a5d278eae8a97df33b6ea825347ad0b98a86857c6d9c4ee926e5caa848cd6c71727bfc93d0806c45ee26729eb4bdc0aee7796b0e9c92982ed282ea4cc2d040a10cf7c77114ae187af438fe70f5a971a239960eee4abbd82b3b73292da4058cfb222ba3bda965908dca244e3c08e8126bdb1bf6f82f18b0a478e3b3595a3d2682a7b8f3535f80f52c1fdf219feb6d4e945f7021905fb548e996f6d13f8f0d995a8c03c3c816b4278310d98939eb7ae8c1f189575060dd60a15df3c16c056420995b4a1a73fbf70ef5531af7b63fb3cda17ba854cfe7654759ae874d884aec12f31e486ead6ad4d1b0cda018a7da61285408d85663c1704f7d9726b615fd8e908176c8895d03a687ff55ab9c743be53418c4e60bd03d055e72235940367406f12817a4496dba89e4587a1247e0b5d00f9ba62a5526af14b513a50de23230cd9030e42a2f4d9cc763e795f343614691c0cff022144ec1543de605725378a41cbcc118bbf8c27961f7de0ca524f36b700645af12510e5ab32f8f87e05eee9a88b7e5d016b3e8fc22e19956485b75d51f75693f22ad0c7bcab06340af3881d5f29461faf885a0e05e8e522aceef7b4ec97a48552bb9335d4131f945c7645368a8b2c3b31e305ab0e50310aa2e42665f5b90d1b3b9f3a26f8c528acde11caa9326f9fe41c985a9f07b3b6ea1930d9a1868819209cad028d9166c1813b9475973f186f0a925ae594a68b1d654533aeeb457322c7e41989663e0d827434b5cfc4b310b41f21a0bc00cee1c76c358d0a59e45d63b646a6126ed5b2c357ec0883a8d3bad29755e554a17e0c30bf3301f9bf78e1bcc2eac86ebe4525f611ade6b94b98926002d356182fb39ec18464edffe8a3300b617edef1fb9db6d4a5c73daf539239cfda15a4dfcbf8811bf88f6bc8ffbb97e6dd2fb0eae2467e9512d6b58a9c85bb11e1d3fde634c624bb5372cd404cacf448a5133d2fe9162a5be87e1e3a0ef0693749d99842122037f10d6405cba8cc9f664f84e615efcd1b1020a501f0aee7a3567e8d60351943e1c3567bdfff601aaefcd9fa51ef85e2fad3661079dd431d19eeb79829dfef4c1ef9aeb9c60417bb83985ad2d3c2783cf3fea343475a0d0bc2c9e5009b11bae997c587b7a95e689d1db8a4385007b842ea33d0e22fd2675fc492f73ca2f45ca95046932d9cc726421165c830855b59976f7946e025340e063494cbeef8819b378c41e38040abbd05d531f328af70b589f906862ffa27655f924dc73cc7a57d15587d12c86e689eba7d2087302a8883308a
とでてきました。
圧縮メソッドを書いたのはこのためだったのです。

復号

unlock c, "key"
.then (p)->
  console.log Buffer.from(p).length
  console.log p
と、やりますと
1906
Spicy jalapeno bacon ipsum dolor amet doner shoulder brisket tail. Hamburger flank beef bresaola pig, pork loin turducken chicken burgdoggen. Shoulder fatback t-bone ground round. Bacon short ribs shankle chicken cow pancetta corned beef turkey venison flank meatball pork belly boudin picanha bresaola.

Shank swine beef ribs, filet mignon tenderloin chislic andouille turducken. Short ribs shankle landjaeger sirloin, prosciutto shank pastrami turkey picanha sausage ball tip pork chop beef ribs. Porchetta filet mignon pork chop bacon cow landjaeger short ribs brisket swine ground round tongue. Fatback ground round kielbasa ball tip boudin sirloin flank ribeye frankfurter salami leberkas alcatra beef. Pancetta chicken strip steak filet mignon andouille kevin. Venison swine ham hock, shoulder meatball turducken rump tail pork chop short loin. Frankfurter porchetta swine, pork belly bresaola chuck rump leberkas jowl alcatra.

Burgdoggen ham pig t-bone turducken jowl ball tip. Shank bresaola kevin hamburger, frankfurter pig flank biltong beef ham. Chislic sausage pancetta biltong landjaeger short loin leberkas flank. Doner ham hock ground round beef short loin, t-bone tri-tip drumstick pastrami meatball. Spare ribs biltong tail meatloaf porchetta ball tip pork chop fatback pastrami buffalo pancetta boudin doner. Chuck filet mignon brisket, tail turkey frankfurter meatloaf ham hock pork chop ball tip salami hamburger strip steak shank prosciutto.

Corned beef chuck picanha ham turkey meatloaf strip steak salami shank jerky burgdoggen. Tail ground round meatball hamburger pork loin buffalo. Beef ribs jerky meatball tail. Landjaeger cow shoulder jowl prosciutto. Alcatra cupim rump turkey kielbasa. Biltong beef flank pork venison tri-tip swine porchetta kevin chuck spare ribs cupim. Tongue landjaeger beef, short loin sirloin ribeye picanha ham andouille ball tip filet mignon tri-tip.
ちゃんと解凍できましたね。 次回はこの鍵を知られないように他人に渡す(ディフィーヘルマン)を描きます。

Discussion

コメントにはログインが必要です。