为什么 ironclad:decrypt-in-place 不能按预期工作?

why not ironclad:decrypt-in-place work as expected?

代码如下:

(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应该和原来的一样,但不是。为什么?

有人可以帮忙吗?

以下是加密快捷方式版本:

(cryptos:decrypt (cryptos:encrypt str
                              key                                  
                              :cipher :arcfour
                              :mode :stream)
             key
             :cipher :arcfour
             :mode :stream)

一切正常

根据我的理解,Ironclad中的密码arcfour的算法是累积的:在算法的每一步,内部结果都在变化。

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

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

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

(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)