加密【Petite Chez 方案】

发布于 2024-10-27 20:38:40 字数 1189 浏览 1 评论 0原文

我对我一直试图运行的程序有疑问。 加密采用消息、公钥和私钥,并返回消息,并将公钥中消息中的字母更改为私钥中的字母。

例如,(加密“abcd”“abcd”“efgh”)应返回“efgh” 并且(加密“abcl”“abcd”“efgh”)应该返回“efgl”(不在公钥中的消息中的字母将保持不变)。

我已经编写了一些帮助程序来解决这个问题,但是当我尝试运行它时,我不断收到错误“汽车异常,__ 不是一对”。但我不确定出了什么问题。如果有人有任何指示,请告诉我。谢谢!

(define encrypt
  (lambda (message public-key private-key)
    (cond
      [(list->string (encrypt-helper (string->list message)
      (string->list public-key) (string->list private-key)))])))

(define encrypt-helper
  (lambda (msg-ls public-ls private-ls)
    (cond
      [(null? public-ls) '()]
      [(null? private-ls) '()]
      [(and (null? public-ls) (null? private-ls)) msg-ls]
      [else (cons (encrypt-key (car msg-ls) (car public-ls) (car private-ls))
        (encrypt-helper (cdr msg-ls) (cdr public-ls) (cdr private-ls)))])))

;should encrypt all letters in msg-ls. not working correctly

(define encrypt-key
  (lambda (char pub-key priv-key)
    (cond
      [(null? pub-key) char]
      [(equal? char (car pub-key)) (car priv-key)]
      [else (encrypt-key char (cdr pub-key) (cdr priv-key))])))

;encrypts just one letter, ex: (encrypt-key 'a '(a) '(b)) => b
;works correctly

I had a question about a program I've been trying to get running.
Encrypt takes a message, public-key, and private-key, and returns the message with the letters from the message in the public-key changed to the letters from the private-key.

For ex, (encrypt "abcd" "abcd" "efgh") should return "efgh"
and (encrypt "abcl" "abcd" "efgh") should return "efgl" (the letter from message that was not in the public-key will stay the same).

I've written a few helper programs to solve this but I keep getting the error "exception in car, __ is not a pair" when I try to run it.. but I'm not sure what's amiss. If anyone has any pointers, let me know. Thanks!

(define encrypt
  (lambda (message public-key private-key)
    (cond
      [(list->string (encrypt-helper (string->list message)
      (string->list public-key) (string->list private-key)))])))

(define encrypt-helper
  (lambda (msg-ls public-ls private-ls)
    (cond
      [(null? public-ls) '()]
      [(null? private-ls) '()]
      [(and (null? public-ls) (null? private-ls)) msg-ls]
      [else (cons (encrypt-key (car msg-ls) (car public-ls) (car private-ls))
        (encrypt-helper (cdr msg-ls) (cdr public-ls) (cdr private-ls)))])))

;should encrypt all letters in msg-ls. not working correctly

(define encrypt-key
  (lambda (char pub-key priv-key)
    (cond
      [(null? pub-key) char]
      [(equal? char (car pub-key)) (car priv-key)]
      [else (encrypt-key char (cdr pub-key) (cdr priv-key))])))

;encrypts just one letter, ex: (encrypt-key 'a '(a) '(b)) => b
;works correctly

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

清浅ˋ旧时光 2024-11-03 20:38:41

问题是在 encrypt-helper 内部,您正在调用

[else (cons (encrypt-key (car msg-ls) (car public-ls) (car private-ls)...

(car public-ls) (和 (car private-ls))是一个原子,而在 encrypt-key 内部你也可以执行

[(equal? char (car pub-key) ...

,但你不能在这里 car pub-key 因为 car 只适用于列表,而 pub-key 是一个原子。

在您给出的示例中,

(encrypt-key 'a '(a) '(b)) => b

您会注意到 '(a)'(b) 被指定为列表,正是出于这个原因。提示:

>(cons 'a ())
(a)
> 

我会把它留在那里:)

The problem is that inside encrypt-helper, you're calling

[else (cons (encrypt-key (car msg-ls) (car public-ls) (car private-ls)...

but (car public-ls) (and (car private-ls)) is an atom, while inside encrypt-key you also perform

[(equal? char (car pub-key) ...

and you can't car pub-key here because car only works on a list, while pub-key is an atom.

In the example you give that works, i.e.

(encrypt-key 'a '(a) '(b)) => b

you'll notice that '(a) and '(b) are specified as lists, for exactly this reason. Hint:

>(cons 'a ())
(a)
> 

I'll leave it there :)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文