github.com/searKing/golang/go@v1.2.117/crypto/aes/example_test.go (about) 1 // Copyright 2021 The searKing Author. All rights reserved. 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 package aes_test 6 7 // Code borrowed from https://github.com/golang/go/blob/master/src/crypto/cipher/example_test.go 8 import ( 9 "encoding/hex" 10 "fmt" 11 12 aes_ "github.com/searKing/golang/go/crypto/aes" 13 ) 14 15 func ExampleGCMEncrypt() { 16 // Load your secret key from a safe place and reuse it across multiple 17 // NewCipher calls. (Obviously don't use this example key for anything 18 // real.) If you want to convert a passphrase to a key, use a suitable 19 // package like bcrypt or scrypt. 20 key, _ := hex.DecodeString("6368616e676520746869732070617373") 21 plaintext := []byte("exampleplaintext") 22 23 ciphertext, err := aes_.GCMEncrypt(key, plaintext, nil) 24 if err != nil { 25 panic(err) 26 } 27 28 // It's important to remember that ciphertexts must be authenticated 29 // (i.e. by using crypto/hmac) as well as being encrypted in order to 30 // be secure. 31 32 fmt.Println("encrypted:") 33 fmt.Printf("%x\n", ciphertext) 34 35 plaintext, err = aes_.GCMDecrypt(ciphertext, key) 36 if err != nil { 37 panic(err) 38 } 39 fmt.Println("decrypted:") 40 fmt.Printf("%s\n", string(plaintext)) 41 // Output: 42 // encrypted: 43 // 0000000000000000000000000b2591cb60a33bdfeb61a0d35207a4196f1e5d1f6fe2b32198e09765ee28bd17d08567996caca50cd7049c07d1db15b5 44 // decrypted: 45 // exampleplaintext 46 } 47 48 func ExampleCBCEncrypt() { 49 // Load your secret key from a safe place and reuse it across multiple 50 // NewCipher calls. (Obviously don't use this example key for anything 51 // real.) If you want to convert a passphrase to a key, use a suitable 52 // package like bcrypt or scrypt. 53 key, _ := hex.DecodeString("6368616e676520746869732070617373") 54 plaintext := []byte("exampleplaintext") 55 56 ciphertext, err := aes_.CBCEncrypt(key, plaintext, nil) 57 if err != nil { 58 panic(err) 59 } 60 61 // It's important to remember that ciphertexts must be authenticated 62 // (i.e. by using crypto/hmac) as well as being encrypted in order to 63 // be secure. 64 65 fmt.Println("encrypted:") 66 fmt.Printf("%x\n", ciphertext) 67 68 plaintext, err = aes_.CBCDecrypt(ciphertext, key) 69 if err != nil { 70 panic(err) 71 } 72 fmt.Println("decrypted:") 73 fmt.Printf("%s\n", string(plaintext)) 74 // Output: 75 // encrypted: 76 // 00000000000000000000000000000000f42512e1e4039213bd449ba47faa1b7408eac45dbf536e5016511f86035707c6 77 // decrypted: 78 // exampleplaintext 79 } 80 81 func ExampleCFBEncryptIV() { 82 // Load your secret key from a safe place and reuse it across multiple 83 // NewCipher calls. (Obviously don't use this example key for anything 84 // real.) If you want to convert a passphrase to a key, use a suitable 85 // package like bcrypt or scrypt. 86 key, _ := hex.DecodeString("6368616e676520746869732070617373") 87 plaintext := []byte("exampleplaintext") 88 89 ciphertext, err := aes_.CFBEncrypt(key, plaintext, nil) 90 if err != nil { 91 panic(err) 92 } 93 94 // It's important to remember that ciphertexts must be authenticated 95 // (i.e. by using crypto/hmac) as well as being encrypted in order to 96 // be secure. 97 98 fmt.Println("encrypted:") 99 fmt.Printf("%x\n", ciphertext) 100 101 plaintext, err = aes_.CFBDecrypt(ciphertext, key) 102 if err != nil { 103 panic(err) 104 } 105 fmt.Println("decrypted:") 106 fmt.Printf("%s\n", string(plaintext)) 107 // Output: 108 // encrypted: 109 // 00000000000000000000000000000000d91399c43f6adaef3d909876e79904a91d84244a42a2a96b67bd9ae936651a2e 110 // decrypted: 111 // exampleplaintext 112 } 113 114 func ExampleCTREncrypt() { 115 // Load your secret key from a safe place and reuse it across multiple 116 // NewCipher calls. (Obviously don't use this example key for anything 117 // real.) If you want to convert a passphrase to a key, use a suitable 118 // package like bcrypt or scrypt. 119 key, _ := hex.DecodeString("6368616e676520746869732070617373") 120 plaintext := []byte("exampleplaintext") 121 122 ciphertext, err := aes_.CTREncrypt(key, plaintext, nil) 123 if err != nil { 124 panic(err) 125 } 126 127 // It's important to remember that ciphertexts must be authenticated 128 // (i.e. by using crypto/hmac) as well as being encrypted in order to 129 // be secure. 130 131 fmt.Println("encrypted:") 132 fmt.Printf("%x\n", ciphertext) 133 134 plaintext, err = aes_.CTRDecrypt(ciphertext, key) 135 if err != nil { 136 panic(err) 137 } 138 fmt.Println("decrypted:") 139 fmt.Printf("%s\n", string(plaintext)) 140 // Output: 141 // encrypted: 142 // 00000000000000000000000000000000d91399c43f6adaef3d909876e79904a93b1144928bc98a4e78f38c23b706610a 143 // decrypted: 144 // exampleplaintext 145 } 146 147 func ExampleOFBEncrypt() { 148 // Load your secret key from a safe place and reuse it across multiple 149 // NewCipher calls. (Obviously don't use this example key for anything 150 // real.) If you want to convert a passphrase to a key, use a suitable 151 // package like bcrypt or scrypt. 152 key, _ := hex.DecodeString("6368616e676520746869732070617373") 153 plaintext := []byte("exampleplaintext") 154 155 ciphertext, err := aes_.OFBEncrypt(key, plaintext, nil) 156 if err != nil { 157 panic(err) 158 } 159 160 // It's important to remember that ciphertexts must be authenticated 161 // (i.e. by using crypto/hmac) as well as being encrypted in order to 162 // be secure. 163 164 fmt.Println("encrypted:") 165 fmt.Printf("%x\n", ciphertext) 166 167 plaintext, err = aes_.OFBDecrypt(ciphertext, key) 168 if err != nil { 169 panic(err) 170 } 171 fmt.Println("decrypted:") 172 fmt.Printf("%s\n", string(plaintext)) 173 // Output: 174 // encrypted: 175 // 00000000000000000000000000000000d91399c43f6adaef3d909876e79904a9729f985422616b9f45b757ac9e7a879e 176 // decrypted: 177 // exampleplaintext 178 }