summaryrefslogtreecommitdiffstats
path: root/challenge/c39.go
blob: 6261c1300b7e5e21c2b962dabe4cfb735b57200b (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
// Copyright © 2022 siddharth ravikumar <s@ricketyspace.net>
// SPDX-License-Identifier: ISC

package challenge

import (
	"fmt"

	"ricketyspace.net/cryptopals/lib"
)

func C39() {
	rsa, err := lib.RSAGenKey()
	if err != nil {
		fmt.Printf("gen key failed: %v", err)
		return
	}

	msg := []byte("42")
	enc := rsa.Public.Encrypt(msg)
	if len(enc) < 1 {
		fmt.Printf("encrypt failed: %v", enc)
		return
	}
	dec := rsa.Private.Decrypt(enc)
	if !lib.BytesEqual(msg, dec) {
		fmt.Printf("decrypt failed: %v", dec)
		return
	}

	msg = []byte("0x42")
	enc = rsa.Public.Encrypt(msg)
	if len(enc) < 1 {
		fmt.Printf("encrypt failed: %v", enc)
		return
	}
	dec = rsa.Private.Decrypt(enc)
	if !lib.BytesEqual(msg, dec) {
		fmt.Printf("decrypt failed: %v", dec)
		return
	}

	msg = []byte("68f13a29c10617c2c87cccb8db2d40ba05191f75f5a08978e84d829a543fa933")
	enc = rsa.Public.Encrypt(msg)
	if len(enc) < 1 {
		fmt.Printf("encrypt failed: %v", enc)
		return
	}
	dec = rsa.Private.Decrypt(enc)
	if !lib.BytesEqual(msg, dec) {
		fmt.Printf("decrypt failed: %v", dec)
		return
	}
}