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 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147
| package main
import ( "crypto" "crypto/rand" "crypto/rsa" "crypto/sha1" "fmt" "io" "os" )
func main() {
privatekey, err := rsa.GenerateKey(rand.Reader, 1024)
if err != nil { fmt.Println(err.Error) os.Exit(1) }
D := privatekey.D Primes := privatekey.Primes PCValues := privatekey.Precomputed
fmt.Println("Private Key : ", privatekey) fmt.Println() fmt.Println("Private Exponent : ", D.String()) fmt.Println() fmt.Printf("Primes : %s %s \n", Primes[0].String(), Primes[1].String()) fmt.Println() fmt.Printf("Precomputed Values : Dp[%s] Dq[%s]\n", PCValues.Dp.String(), PCValues.Dq.String()) fmt.Println() fmt.Printf("Precomputed Values : Qinv[%s]", PCValues.Qinv.String()) fmt.Println()
privatekey.Precompute()
err = privatekey.Validate()
if err != nil { fmt.Println(err.Error) os.Exit(1) }
var publickey *rsa.PublicKey publickey = &privatekey.PublicKey N := publickey.N E := publickey.E
fmt.Println("Public key ", publickey) fmt.Println() fmt.Println("Public Exponent : ", E) fmt.Println() fmt.Println("Modulus : ", N.String()) fmt.Println()
msg := []byte("The secret message!") label := []byte("") sha1hash := sha1.New()
encryptedmsg, err := rsa.EncryptOAEP(sha1hash, rand.Reader, publickey, msg, label)
if err != nil { fmt.Println(err) os.Exit(1) }
fmt.Printf("OAEP encrypted [%s] to \n[%x]\n", string(msg), encryptedmsg) fmt.Println()
decryptedmsg, err := rsa.DecryptOAEP(sha1hash, rand.Reader, privatekey, encryptedmsg, label)
if err != nil { fmt.Println(err) os.Exit(1) }
fmt.Printf("OAEP decrypted [%x] to \n[%s]\n", encryptedmsg, decryptedmsg) fmt.Println()
encryptedPKCS1v15, errPKCS1v15 := rsa.EncryptPKCS1v15(rand.Reader, publickey, msg)
if errPKCS1v15 != nil { fmt.Println(errPKCS1v15) os.Exit(1) }
fmt.Printf("PKCS1v15 encrypted [%s] to \n[%x]\n", string(msg), encryptedPKCS1v15) fmt.Println()
decryptedPKCS1v15, err := rsa.DecryptPKCS1v15(rand.Reader, privatekey, encryptedPKCS1v15)
if err != nil { fmt.Println(err) os.Exit(1) }
fmt.Printf("PKCS1v15 decrypted [%x] to \n[%s]\n", encryptedPKCS1v15, decryptedPKCS1v15) fmt.Println()
var h crypto.Hash message := []byte("This is the message to be signed!") hash := sha1.New() io.WriteString(hash, string(message)) hashed := hash.Sum(nil)
signature, err := rsa.SignPKCS1v15(rand.Reader, privatekey, h, hashed)
if err != nil { fmt.Println(err) os.Exit(1) }
fmt.Printf("PKCS1v15 Signature : %x\n", signature)
err = rsa.VerifyPKCS1v15(publickey, h, hashed, signature)
if err != nil { fmt.Println("VerifyPKCS1v15 failed") os.Exit(1) } else { fmt.Println("VerifyPKCS1v15 successful") } fmt.Println()
}
|