diff --git a/aes/aes.go b/aes/aes.go new file mode 100644 index 0000000..becce25 --- /dev/null +++ b/aes/aes.go @@ -0,0 +1,64 @@ +package main + +import ( + "crypto/aes" + "encoding/hex" + "fmt" +) + +func main() { + + // cipher key + key := "thisis32bitlongpassphraseimusing" + + // plaintext + pt := "This is a secret" + + c := EncryptAES([]byte(key), pt) + + // plaintext + fmt.Println((pt)) + + fmt.Println([]byte(pt)) + + // ciphertext + + fmt.Println((c)) + fmt.Println([]byte(c)) + + key2 := "thisis32bitlongpassphraseimusing" + + // decrypt + DecryptAES([]byte(key2), c) +} + +func EncryptAES(key []byte, plaintext string) string { + + c, err := aes.NewCipher(key) + CheckError(err) + + out := make([]byte, len(plaintext)) + + c.Encrypt(out, []byte(plaintext)) + + return hex.EncodeToString(out) +} + +func DecryptAES(key []byte, ct string) { + ciphertext, _ := hex.DecodeString(ct) + + c, err := aes.NewCipher(key) + CheckError(err) + + pt := make([]byte, len(ciphertext)) + c.Decrypt(pt, ciphertext) + + s := string(pt[:]) + fmt.Println("DECRYPTED:", s) +} + +func CheckError(err error) { + if err != nil { + panic(err) + } +} diff --git a/hashing/hashing-examples.go b/hashing/hashing-examples.go new file mode 100644 index 0000000..9063eac --- /dev/null +++ b/hashing/hashing-examples.go @@ -0,0 +1,28 @@ +package main + +import ( + "crypto/md5" + "crypto/sha256" + "fmt" +) + +func main() { + s := "Hello" + + sha256 := sha256.Sum256([]byte(s)) + md5 := md5.Sum([]byte(s)) + + fmt.Println() + fmt.Println(s) + + fmt.Println() + fmt.Printf("%x", sha256) + fmt.Println() + + fmt.Println() + fmt.Printf("%x", md5) + + fmt.Println() + fmt.Println() + +} diff --git a/rsa/rsa-encrypt-sign.go b/rsa/rsa-encrypt-sign.go new file mode 100644 index 0000000..7c4be63 --- /dev/null +++ b/rsa/rsa-encrypt-sign.go @@ -0,0 +1,137 @@ +// Derived from: https://medium.com/@bobgzm/golang-cryptography-rsa-asymmetric-algorithm-e91363a2f7b3 + +package main + +import ( + "crypto" + "crypto/rand" + "crypto/rsa" + "crypto/sha256" + "fmt" + "os" +) + +func main() { + + alicePrivateKey, err := rsa.GenerateKey(rand.Reader, 2048) + if err != nil { + fmt.Println(err) + os.Exit(1) + } + alicePublicKey := &alicePrivateKey.PublicKey + + trudyPrivateKey, err := rsa.GenerateKey(rand.Reader, 2048) + if err != nil { + fmt.Println(err) + os.Exit(1) + } + //trudyPublicKey := &trudyPrivateKey.PublicKey + + bobPrivateKey, err := rsa.GenerateKey(rand.Reader, 2048) + if err != nil { + fmt.Println(err) + os.Exit(1) + } + bobPublicKey := &bobPrivateKey.PublicKey + + fmt.Println("Alice's Private Key : ", alicePrivateKey) + fmt.Println() + fmt.Println("Alice's Public key ", alicePublicKey) + fmt.Println() + fmt.Println("Bob's Private Key : ", bobPrivateKey) + fmt.Println() + fmt.Println("Bob's Public key ", bobPublicKey) + fmt.Println() + + message := []byte("first rule of cs5970 is that you tell everyone about cs5970") + label := []byte("") + hash := sha256.New() + + // RSA encryption examples + ciphertext, err := rsa.EncryptOAEP( + hash, + rand.Reader, + bobPublicKey, + message, + label) + + if err != nil { + fmt.Println(err) + os.Exit(1) + } + + // OAEP is a padding scheme, used with RSA + fmt.Printf("OAEP encrypted [%s] to \n[%x]\n", string(message), ciphertext) + fmt.Println() + + // Same encryption, but ciphertext2 will be different due to rand.Reader + ciphertext2, err := rsa.EncryptOAEP( + hash, + rand.Reader, + bobPublicKey, + message, + label) + + if err != nil { + fmt.Println(err) + os.Exit(1) + } + + // OAEP is a padding scheme, used with RSA + fmt.Printf("OAEP encrypted [%s] to \n[%x]\n", string(message), ciphertext2) + fmt.Println() + + // Digital signature examples + var opts rsa.PSSOptions + opts.SaltLength = rsa.PSSSaltLengthAuto // for simple example + PSSmessage := message + newhash := crypto.SHA256 + pssh := newhash.New() + pssh.Write(PSSmessage) + + hashed := pssh.Sum(nil) + signature, err := rsa.SignPSS( + rand.Reader, + trudyPrivateKey, + newhash, + hashed, + &opts) + + if err != nil { + fmt.Println(err) + os.Exit(1) + } + + // Probabilistic Signature Scheme (PSS) + fmt.Printf("PSS Signature : %x\n", signature) + fmt.Println() + + plainText, err := rsa.DecryptOAEP( + hash, + rand.Reader, + bobPrivateKey, + ciphertext, + label) + + if err != nil { + fmt.Println(err) + os.Exit(1) + } + + fmt.Printf("OAEP decrypted [%x] to \n[%s]\n", ciphertext, plainText) + fmt.Println() + + err = rsa.VerifyPSS( + alicePublicKey, + newhash, + hashed, + signature, + &opts) + + if err != nil { + fmt.Println("Signature verification failed!") + os.Exit(1) + } else { + fmt.Println("Signature verification successful!") + } +}