Commit 07dfe17d by Frederik Thor Wind Norup

### added peer program to repo

parents
 package encrypt import ( "crypto/aes" "crypto/cipher" "crypto/rand" "crypto/sha256" "fmt" "io" "io/ioutil" "math/big" "strings" ) type PublicKey struct { n *big.Int e *big.Int } type PrivateKey struct { n *big.Int d *big.Int } // KeyGen generates a new RSA key func KeyGen(k int) (pub *PublicKey, priv *PrivateKey) { publicKey := new(PublicKey) privateKey := new(PrivateKey) publicKey.e = big.NewInt(3) // call generatePQ to get our p q for calculating n and d p, q := generatePQ(k, publicKey) // n = p*q n := big.NewInt(0).Mul(p, q) // add n to our structs publicKey.n = n privateKey.n = n // d = 3^-1 mod (p-1)(q-1) factorMul := big.NewInt(0) factorMul.Mul(p.Sub(p, big.NewInt(1)), q.Sub(q, big.NewInt(1))) d := big.NewInt(0) privateKey.d = d.ModInverse(publicKey.e, factorMul) return publicKey, privateKey } // generatePQ takes k as bit length of n and publicKey exponent func generatePQ(k int, pub *PublicKey) (p *big.Int, q *big.Int) { for { // generate random p and q p, _ := rand.Prime(rand.Reader, k/2) q, _ := rand.Prime(rand.Reader, k/2) pGCD := big.NewInt(0) qGCD := big.NewInt(0) // check if GCD(3, p-1) = GCD(3, q-1) = 1 if so return our p and q pGCD.GCD(nil, nil, pub.e, subOne(p)) qGCD.GCD(nil, nil, pub.e, subOne(q)) //isEqual := big.NewInt(1) if big.NewInt(1).Cmp(pGCD) == 0 { if big.NewInt(1).Cmp(qGCD) == 0 { return p, q } } } } // subOne subtracts 1 from n without saving it in n func subOne(n *big.Int) *big.Int { newN := big.NewInt(0).Sub(n, big.NewInt(1)) return newN } // Encrypt encrypts message func Encrypt(m *big.Int, pub *PublicKey) *big.Int { m.Exp(m, pub.e, pub.n) return m } // Decrypt decrypts message func Decrypt(m *big.Int, priv *PrivateKey) *big.Int { m.Exp(m, priv.d, priv.n) return m } // EncryptToFile encrypts given byte array to given fileName and encrypts with given cypherKey func EncryptToFile(fileName string, cipherstring []byte, cipherKey string) { // generate new cypher based on key key := []byte(cipherKey) block, err := aes.NewCipher(key) if err != nil { panic(err) } // ciphertext byte array that is length of given array + aes.blockSize ciphertext := make([]byte, aes.BlockSize+len(cipherstring)) iv := ciphertext[:aes.BlockSize] if _, err := io.ReadFull(rand.Reader, iv); err != nil { panic(err) } // encrypt ciphertext and write to file stream := cipher.NewCTR(block, iv) stream.XORKeyStream(ciphertext[aes.BlockSize:], cipherstring) ioutil.WriteFile(fileName, []byte(ciphertext), 0777) } // DecryptFromFile decrypts file on given string with a given cypher key func DecryptFromFile(fileName string, cipherKey string) { // read file and convert to bytes cipherstring, err := ioutil.ReadFile(fileName) if err != nil { panic(err) } ciphertext := []byte(cipherstring) // make a new cypherKey from given key key := []byte(cipherKey) block, err := aes.NewCipher(key) if err != nil { panic(err) } // decrypt text and write back to file iv := ciphertext[:aes.BlockSize] stream := cipher.NewCTR(block, iv) plaintext := make([]byte, len(ciphertext[aes.BlockSize:])) stream.XORKeyStream(plaintext, ciphertext[aes.BlockSize:]) ioutil.WriteFile(fileName, plaintext, 0777) } func encryptSign(m *big.Int, priv *PrivateKey) *big.Int { m.Exp(m, priv.d, priv.n) return m } func decryptSign(m *big.Int, pub *PublicKey) *big.Int { m.Exp(m, pub.e, pub.n) return m } func generateHash(m *big.Int) *big.Int { data := m.Bytes() hash := sha256.New() hash.Write(data) intHash := big.NewInt(0) intHash.SetBytes(hash.Sum(nil)) return intHash } func GenerateSign(m *big.Int, priv *PrivateKey) *big.Int { sign := generateHash(m) sign = encryptSign(sign, priv) return sign } func VerifySign(m *big.Int, s *big.Int, pub *PublicKey) bool { calculatedHash := generateHash(m) signedHash := decryptSign(s, pub) verified := false if calculatedHash.Cmp(signedHash) == 0 { verified = true } else { fmt.Println("Unable to verify hash") } return verified } func PrivateKeyToString(key *PrivateKey) string { d := key.d.String() n := key.n.String() encoded := d + ":" + n return encoded } func StringToPrivateKey(encodedKey string) *PrivateKey { splitString := strings.Split(encodedKey, ":") key := new(PrivateKey) d := big.NewInt(0) d.SetString(splitString[0], 10) key.d = d n := big.NewInt(0) n.SetString(splitString[1], 10) key.n = n return key } func PublicKeyToString(key *PublicKey) string { if key.e == nil || key.n == nil { panic("public Key value = nil") } e := key.e.String() n := key.n.String() encoded := e + ":" + n return encoded } func StringToPublicKey(encodedKey string) *PublicKey { splitString := strings.Split(encodedKey, ":") key := new(PublicKey) e := big.NewInt(0) e.SetString(splitString[0], 10) key.e = e n := big.NewInt(0) n.SetString(splitString[1], 10) key.n = n if key.e == nil || key.n == nil { panic("public Key value = nil") } return key } func CreateDummyKey() *PublicKey { dummyKey := new(PublicKey) dummyKey.e = big.NewInt(1) dummyKey.n = big.NewInt(1) return dummyKey } func IsKeyInitialised(key string) bool { if key == "1:1" { return false } return true }
encrypt/go.mod 0 → 100644
 module the-spice-boyz/encrypt go 1.17
go.mod 0 → 100644
 module main go 1.17
peer.go 0 → 100644