-
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix: Major restructuring for fixing the linting workflow errors and module style code writing. #266
Changes from 29 commits
f21a883
ec97414
e70d794
d2ff6bc
bf53d3d
203e030
099a88d
f909f02
fab3c25
34e990e
2b33eee
3a8b7c1
a946d5d
f3651c5
07d3444
26a8ebb
c1a6e0a
7f2ee5e
101025b
ead991b
335b995
bb4a2dd
7079327
8536557
f102ef4
83f2667
48d8cce
938f6dd
673b69d
54b009e
ff265cc
3dbc733
a9f1378
d485bde
eff98a2
1b093de
22047fc
c37f598
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,10 @@ | ||
package main | ||
package rsacipher | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This package fails the automated tests for the encryption and decryption. I thought it was failing because the implementation of modular exponentiation was incorrect but the test for modular exponentiation passes without issues so I'm actually not sure what the problem is. My current theory is that the inverse exponentiation function is incorrect but I did't try to test it. |
||
|
||
import ( | ||
//"math/big" | ||
"fmt" | ||
|
||
"math" | ||
"math/rand" | ||
"time" | ||
) | ||
|
||
func generatePrimes(limit int) int { | ||
|
@@ -39,7 +38,7 @@ func prime(limit int) (primes []int) { | |
primes = append(primes, 2, 3, 5) | ||
lastIndex := 2 | ||
for primes[lastIndex] < sqrtLimit { | ||
if exit == true { | ||
if exit { | ||
break | ||
} | ||
for i := primes[lastIndex] + 2; i < primes[lastIndex]*primes[lastIndex]; i += 2 { | ||
|
@@ -105,15 +104,18 @@ func modularExponentiation(b, e, mod int) int { | |
return r | ||
} | ||
|
||
func encryptRSA(message []int, e, n int) []int { | ||
// EncryptRSA main encryption function for RSA | ||
func EncryptRSA(message []int, e, n int) []int { | ||
//runs in O(k*log(n)) where k = len(message) and n = e | ||
var ciphertext []int | ||
for _, v := range message { | ||
ciphertext = append(ciphertext, modularExponentiation(v, e, n)) | ||
} | ||
return ciphertext | ||
} | ||
func decryptRSA(ciphertext []int, d, n int) []int { | ||
|
||
// DecryptRSA main decryption function for RSA | ||
func DecryptRSA(ciphertext []int, d, n int) []int { | ||
//runs in O(k*log(n)) where k = len(ciphertext) and n = d | ||
var message []int | ||
for _, v := range ciphertext { | ||
|
@@ -130,7 +132,8 @@ func toASCII(slice []rune) []int { | |
return converted | ||
} | ||
|
||
func toRune(slice []int) string { | ||
// ToRune convert a string to rune | ||
func ToRune(slice []int) string { | ||
//runs in O(n) where n = len(slice) | ||
var str string | ||
for _, v := range slice { | ||
|
@@ -139,35 +142,35 @@ func toRune(slice []int) string { | |
return str | ||
} | ||
|
||
func main() { | ||
rand.Seed(time.Now().UTC().UnixNano()) | ||
bits := 17 | ||
// func main() { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do let me know if you think that the main function that have been commented out should be removed completely. I kept them here because I think they are useful for making new test cases, but a lot of them already have test files so I wasn't sure I should completely remove these blocks. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes it should be removed completely. |
||
// rand.Seed(time.Now().UTC().UnixNano()) | ||
// bits := 17 | ||
|
||
p := generatePrimes(1 << bits) | ||
q := generatePrimes(1 << bits) | ||
for p == q { | ||
q = generatePrimes(1 << bits) | ||
} | ||
// p := generatePrimes(1 << bits) | ||
// q := generatePrimes(1 << bits) | ||
// for p == q { | ||
// q = generatePrimes(1 << bits) | ||
// } | ||
|
||
n := p * q | ||
// n := p * q | ||
|
||
delta := lcm(p-1, q-1) | ||
// delta := lcm(p-1, q-1) | ||
|
||
e := generatePrimes(delta) | ||
d := modularMultiplicativeInverse(e, delta) | ||
// e := generatePrimes(delta) | ||
// d := modularMultiplicativeInverse(e, delta) | ||
|
||
fmt.Printf("%v \n%v \n%v \n%v\n", p, q, e, d) | ||
// fmt.Printf("%v \n%v \n%v \n%v\n", p, q, e, d) | ||
|
||
str := "I think RSA is really great" | ||
message := []rune(str) | ||
asciiSlice := toASCII(message) | ||
// str := "I think RSA is really great" | ||
// message := []rune(str) | ||
// asciiSlice := toASCII(message) | ||
|
||
fmt.Printf("asciiSlice : %v \n", asciiSlice) | ||
encrypted := encryptRSA(asciiSlice, e, n) | ||
fmt.Printf("encrypted : %v \n", encrypted) | ||
decrypted := decryptRSA(encrypted, d, n) | ||
fmt.Printf("decrypted : %v \n", decrypted) | ||
fmt.Printf("cleartext : %v \n", toRune(decrypted)) | ||
//switched to atom | ||
// fmt.Printf("asciiSlice : %v \n", asciiSlice) | ||
// encrypted := encryptRSA(asciiSlice, e, n) | ||
// fmt.Printf("encrypted : %v \n", encrypted) | ||
// decrypted := decryptRSA(encrypted, d, n) | ||
// fmt.Printf("decrypted : %v \n", decrypted) | ||
// fmt.Printf("cleartext : %v \n", toRune(decrypted)) | ||
// //switched to atom | ||
|
||
} | ||
// } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.