# [plt-scheme] RSA encryption algorithm

 From: Robby Findler (robby at cs.uchicago.edu) Date: Fri Nov 2 20:52:39 EDT 2007 Previous message: [plt-scheme] RSA encryption algorithm Next message: [plt-scheme] RSA encryption algorithm Messages sorted by: [date] [thread] [subject] [author]

```Would you mind sharing with us who has made up the rules to this game
you must play?

Robby

On 11/2/07, Cristiano Rocha <lionht14 at gmail.com> wrote:
> Hi.
>
>  I need some help here. I'm new to Scheme and now I'm supposed to build a
> RSA encryption algorithm.
>
> I have some guidelines to build it and I'll post them now.
>
> 1. Write a procedure "prime?" that given a natural (positive) number returns
> true if that number is a prime number or false otherwise.
>
> 2. Write a procedure "calc-e" that receives an argument n and returns a
> natural number, between 1 and n, that is a coprime of n.
>
> 3. Write a procedure "calc-d" that given 2 arguments, e and n, returns a
> natural number d, such that there is a number k to which the following
> expression is true:
>
> d . e = 1 + k . n
>
> 4. Write a procedure "RSA" that receives 2 natural (positive) numbers, k and
> l, and generates a public key and it's respective private key that uses de
> k-th and the l-th prime numbers as the initial prime numbers. The keys
> should be displayed in the screen in an adequate form
>
> 5. Write a procedure "encoder" that receives 2 natural numbers that
> correspond to the public and private key and returns a procedure that the
> argument is de encoder or decoder that uses the received key.
>
> I'm not able to use every primitive procedure in Scheme. I'm only able to
> use these procedures:
>
> Numerical operations;
> Predicates;
> Logical operations;
> abs, cond, else, if, lambda, let, let*;
> begin, display, newline;
>
> I guess that's all of them...
>
> So far I have this:
>
> ;Begin prime?
>
> (define (prime? k)
>   (if (<= k 1)
>       "Error: Insert number over 1."
>    (= k (smallest-divisor k))))
>
> (define (smallest-divisor k)
>   (divisor k 2))
>
> (define (divisor k test-divisor)
>   (cond ((< k (sqrt test-divisor)) k)
>         (( divisor? test-divisor k) test-divisor)
>         (else (divisor k (+ test-divisor 1)))))
>
> (define (divisor? a b)
>   (= (remainder b a) 0))
>
> ;End prime?
>
> ;Begin calc-e
>
> (define (a n)
>   (gcd (- n 1) n))
>
> (define (calc-e n)
>   (if (= (a n) 1)
>       (- n 1)
>       (a ( - n 1))))
>
> ;End calc-e
>
> If there's anyone that could help me out here I would appreciate it.
>
> Regards.
>
> _________________________________________________