-
Notifications
You must be signed in to change notification settings - Fork 0
/
triple_des.go
46 lines (37 loc) · 1.3 KB
/
triple_des.go
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
// Copyright 2024 FishGoddess. All rights reserved.
// Use of this source code is governed by a MIT style
// license that can be found in the LICENSE file.
package main
import (
"bytes"
"fmt"
"github.com/FishGoddess/cryptox"
"github.com/FishGoddess/cryptox/des"
)
func main() {
// As you know, key is necessary in triple des.
// However, not all modes need iv, such as ecb.
key := []byte("123456788765432112345678")
iv := []byte("87654321")
msg := []byte("你好,世界")
fmt.Printf("msg: %s\n", msg)
// We use ctr mode and no padding to encrypt data.
// Of course, you can choose another mode if you want.
// Also, you can choose no/zero/pkcs5/pkcs7 to padding data.
encrypted, err := des.EncryptCTRTriple(key, iv, cryptox.PaddingNone, msg)
if err != nil {
panic(err)
}
fmt.Println("encrypted:", encrypted)
fmt.Println("encrypted hex:", encrypted.Hex())
fmt.Println("encrypted base64:", encrypted.Base64())
// We use ctr mode and no padding to decrypt data.
// Of course, you can choose another mode if you want.
// Also, you can choose no/zero/pkcs5/pkcs7 to undo padding data.
decrypted, err := des.DecryptCTRTriple(key, iv, cryptox.PaddingNone, encrypted)
if err != nil {
panic(err)
}
fmt.Printf("decrypted: %s\n", decrypted)
fmt.Println("decrypted == msg", bytes.Equal(decrypted, msg))
}