首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >为什么不铁板一块:按预期解密现场工作?

为什么不铁板一块:按预期解密现场工作?
EN

Stack Overflow用户
提问于 2021-11-05 02:05:39
回答 1查看 129关注 0票数 2

守则如下:

代码语言:javascript
复制
(ql:quickload :ironclad)
(ql:quickload :crypto-shortcuts)

(use-package :ironclad)

(defparameter str "Hello World!")
(defparameter message (ascii-string-to-byte-array str))
(defparameter key "1234")

(let ((cipher (make-cipher :arcfour
                           :key (ascii-string-to-byte-array key)
                           :mode :stream
                           :initialization-vector (make-random-salt)))
      (text
       (ascii-string-to-byte-array
        (cryptos:to-base64 (copy-seq message)))))
  ;; #(83 71 86 115 98 71 56 103 86 50 57 121 98 71 81 104)
  (format t "~a~%" text)
  (encrypt-in-place cipher text)
  ;; #(86 14 39 220 145 171 63 106 89 41 57 41 135 32 85 188)
  ;; "Vg4n3JGrP2pZKTkphyBVvA=="
  (format t "~a~%" text)
  (format t "~a~%" (cryptos:to-base64 text))
  (decrypt-in-place cipher text)
  ;; ?
  ;; #(83 71 86 115 98 71 56 103 86 50 57 121 98 71 81 104)
  (format t "~a~%" text))

我的密码怎么了?

(decrypt-in-place cipher text)之后,text应该和原来的一样,但它不是。为什么?

有人能帮忙吗?

下面是密码快捷键版本:

代码语言:javascript
复制
(cryptos:decrypt (cryptos:encrypt str
                              key                                  
                              :cipher :arcfour
                              :mode :stream)
             key
             :cipher :arcfour
             :mode :stream)

一切都很好。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-11-05 13:29:20

据我所知,密码arcfourIronclad中的算法是累加的:在算法的每一步,内部结果都在变化。

在这里,您将encryption的结果重用到decryption中。此时,密码的内部状态未被正确初始化,其值是前一次加密的结果。

为了正常工作,需要以与decryption密码相同的方式初始化encryption密码。

最简单的方法是创建两个cipher实例

代码语言:javascript
复制
(let* ((salt (make-random-salt))
       (cipher-encrypt (make-cipher :arcfour
                                    :key (ascii-string-to-byte-array key)
                                    :mode :stream
                                    :initialization-vector salt))
       (cipher-decrypt (make-cipher :arcfour
                                    :key (ascii-string-to-byte-array key)
                                    :mode :stream
                                    :initialization-vector salt))
       (text
        (ascii-string-to-byte-array
         (cryptos:to-base64 (copy-seq message)))))
  ;; #(83 71 86 115 98 71 56 103 86 50 57 121 98 71 81 104)
  (format t "~a~%" text)
  (encrypt-in-place cipher-encrypt text)
  ;; #(86 14 39 220 145 171 63 106 89 41 57 41 135 32 85 188)
  ;; "Vg4n3JGrP2pZKTkphyBVvA=="
  (format t "~a~%" text)
  (format t "~a~%" (cryptos:to-base64 text))
  (decrypt-in-place cipher-decrypt text)
  ;; ?
  ;; #(83 71 86 115 98 71 56 103 86 50 57 121 98 71 81 104)
  (format t "~a~%" text))
#(83 71 86 115 98 71 56 103 86 50 57 121 98 71 81 104)
#(86 14 39 220 145 171 63 106 89 41 57 41 135 32 85 188)
Vg4n3JGrP2pZKTkphyBVvA==
#(83 71 86 115 98 71 56 103 86 50 57 121 98 71 81 104)
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/69847907

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档