1
2
3
4
5 package cipher_test
6
7 import (
8 "bytes"
9 "crypto/aes"
10 "crypto/cipher"
11 "crypto/rand"
12 "encoding/hex"
13 "fmt"
14 "io"
15 "os"
16 )
17
18 func ExampleNewGCM_encrypt() {
19
20
21
22
23
24 key, _ := hex.DecodeString("6368616e676520746869732070617373776f726420746f206120736563726574")
25 plaintext := []byte("exampleplaintext")
26
27 block, err := aes.NewCipher(key)
28 if err != nil {
29 panic(err.Error())
30 }
31
32
33 nonce := make([]byte, 12)
34 if _, err := io.ReadFull(rand.Reader, nonce); err != nil {
35 panic(err.Error())
36 }
37
38 aesgcm, err := cipher.NewGCM(block)
39 if err != nil {
40 panic(err.Error())
41 }
42
43 ciphertext := aesgcm.Seal(nil, nonce, plaintext, nil)
44 fmt.Printf("%x\n", ciphertext)
45 }
46
47 func ExampleNewGCM_decrypt() {
48
49
50
51
52
53 key, _ := hex.DecodeString("6368616e676520746869732070617373776f726420746f206120736563726574")
54 ciphertext, _ := hex.DecodeString("c3aaa29f002ca75870806e44086700f62ce4d43e902b3888e23ceff797a7a471")
55 nonce, _ := hex.DecodeString("64a9433eae7ccceee2fc0eda")
56
57 block, err := aes.NewCipher(key)
58 if err != nil {
59 panic(err.Error())
60 }
61
62 aesgcm, err := cipher.NewGCM(block)
63 if err != nil {
64 panic(err.Error())
65 }
66
67 plaintext, err := aesgcm.Open(nil, nonce, ciphertext, nil)
68 if err != nil {
69 panic(err.Error())
70 }
71
72 fmt.Printf("%s\n", plaintext)
73
74 }
75
76 func ExampleNewCBCDecrypter() {
77
78
79
80
81 key, _ := hex.DecodeString("6368616e676520746869732070617373")
82 ciphertext, _ := hex.DecodeString("73c86d43a9d700a253a96c85b0f6b03ac9792e0e757f869cca306bd3cba1c62b")
83
84 block, err := aes.NewCipher(key)
85 if err != nil {
86 panic(err)
87 }
88
89
90
91 if len(ciphertext) < aes.BlockSize {
92 panic("ciphertext too short")
93 }
94 iv := ciphertext[:aes.BlockSize]
95 ciphertext = ciphertext[aes.BlockSize:]
96
97
98 if len(ciphertext)%aes.BlockSize != 0 {
99 panic("ciphertext is not a multiple of the block size")
100 }
101
102 mode := cipher.NewCBCDecrypter(block, iv)
103
104
105 mode.CryptBlocks(ciphertext, ciphertext)
106
107
108
109
110
111
112
113
114
115 fmt.Printf("%s\n", ciphertext)
116
117 }
118
119 func ExampleNewCBCEncrypter() {
120
121
122
123
124 key, _ := hex.DecodeString("6368616e676520746869732070617373")
125 plaintext := []byte("exampleplaintext")
126
127
128
129
130
131 if len(plaintext)%aes.BlockSize != 0 {
132 panic("plaintext is not a multiple of the block size")
133 }
134
135 block, err := aes.NewCipher(key)
136 if err != nil {
137 panic(err)
138 }
139
140
141
142 ciphertext := make([]byte, aes.BlockSize+len(plaintext))
143 iv := ciphertext[:aes.BlockSize]
144 if _, err := io.ReadFull(rand.Reader, iv); err != nil {
145 panic(err)
146 }
147
148 mode := cipher.NewCBCEncrypter(block, iv)
149 mode.CryptBlocks(ciphertext[aes.BlockSize:], plaintext)
150
151
152
153
154
155 fmt.Printf("%x\n", ciphertext)
156 }
157
158 func ExampleNewCFBDecrypter() {
159
160
161
162
163 key, _ := hex.DecodeString("6368616e676520746869732070617373")
164 ciphertext, _ := hex.DecodeString("7dd015f06bec7f1b8f6559dad89f4131da62261786845100056b353194ad")
165
166 block, err := aes.NewCipher(key)
167 if err != nil {
168 panic(err)
169 }
170
171
172
173 if len(ciphertext) < aes.BlockSize {
174 panic("ciphertext too short")
175 }
176 iv := ciphertext[:aes.BlockSize]
177 ciphertext = ciphertext[aes.BlockSize:]
178
179 stream := cipher.NewCFBDecrypter(block, iv)
180
181
182 stream.XORKeyStream(ciphertext, ciphertext)
183 fmt.Printf("%s", ciphertext)
184
185 }
186
187 func ExampleNewCFBEncrypter() {
188
189
190
191
192 key, _ := hex.DecodeString("6368616e676520746869732070617373")
193 plaintext := []byte("some plaintext")
194
195 block, err := aes.NewCipher(key)
196 if err != nil {
197 panic(err)
198 }
199
200
201
202 ciphertext := make([]byte, aes.BlockSize+len(plaintext))
203 iv := ciphertext[:aes.BlockSize]
204 if _, err := io.ReadFull(rand.Reader, iv); err != nil {
205 panic(err)
206 }
207
208 stream := cipher.NewCFBEncrypter(block, iv)
209 stream.XORKeyStream(ciphertext[aes.BlockSize:], plaintext)
210
211
212
213
214 fmt.Printf("%x\n", ciphertext)
215 }
216
217 func ExampleNewCTR() {
218
219
220
221
222 key, _ := hex.DecodeString("6368616e676520746869732070617373")
223 plaintext := []byte("some plaintext")
224
225 block, err := aes.NewCipher(key)
226 if err != nil {
227 panic(err)
228 }
229
230
231
232 ciphertext := make([]byte, aes.BlockSize+len(plaintext))
233 iv := ciphertext[:aes.BlockSize]
234 if _, err := io.ReadFull(rand.Reader, iv); err != nil {
235 panic(err)
236 }
237
238 stream := cipher.NewCTR(block, iv)
239 stream.XORKeyStream(ciphertext[aes.BlockSize:], plaintext)
240
241
242
243
244
245
246
247
248 plaintext2 := make([]byte, len(plaintext))
249 stream = cipher.NewCTR(block, iv)
250 stream.XORKeyStream(plaintext2, ciphertext[aes.BlockSize:])
251
252 fmt.Printf("%s\n", plaintext2)
253
254 }
255
256 func ExampleNewOFB() {
257
258
259
260
261 key, _ := hex.DecodeString("6368616e676520746869732070617373")
262 plaintext := []byte("some plaintext")
263
264 block, err := aes.NewCipher(key)
265 if err != nil {
266 panic(err)
267 }
268
269
270
271 ciphertext := make([]byte, aes.BlockSize+len(plaintext))
272 iv := ciphertext[:aes.BlockSize]
273 if _, err := io.ReadFull(rand.Reader, iv); err != nil {
274 panic(err)
275 }
276
277 stream := cipher.NewOFB(block, iv)
278 stream.XORKeyStream(ciphertext[aes.BlockSize:], plaintext)
279
280
281
282
283
284
285
286
287 plaintext2 := make([]byte, len(plaintext))
288 stream = cipher.NewOFB(block, iv)
289 stream.XORKeyStream(plaintext2, ciphertext[aes.BlockSize:])
290
291 fmt.Printf("%s\n", plaintext2)
292
293 }
294
295 func ExampleStreamReader() {
296
297
298
299
300 key, _ := hex.DecodeString("6368616e676520746869732070617373")
301
302 encrypted, _ := hex.DecodeString("cf0495cc6f75dafc23948538e79904a9")
303 bReader := bytes.NewReader(encrypted)
304
305 block, err := aes.NewCipher(key)
306 if err != nil {
307 panic(err)
308 }
309
310
311
312 var iv [aes.BlockSize]byte
313 stream := cipher.NewOFB(block, iv[:])
314
315 reader := &cipher.StreamReader{S: stream, R: bReader}
316
317 if _, err := io.Copy(os.Stdout, reader); err != nil {
318 panic(err)
319 }
320
321
322
323
324
325
326
327 }
328
329 func ExampleStreamWriter() {
330
331
332
333
334 key, _ := hex.DecodeString("6368616e676520746869732070617373")
335
336 bReader := bytes.NewReader([]byte("some secret text"))
337
338 block, err := aes.NewCipher(key)
339 if err != nil {
340 panic(err)
341 }
342
343
344
345 var iv [aes.BlockSize]byte
346 stream := cipher.NewOFB(block, iv[:])
347
348 var out bytes.Buffer
349
350 writer := &cipher.StreamWriter{S: stream, W: &out}
351
352 if _, err := io.Copy(writer, bReader); err != nil {
353 panic(err)
354 }
355
356
357
358
359
360
361 fmt.Printf("%x\n", out.Bytes())
362
363 }
364
View as plain text