forked from killvxk/goEncrypt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tripledescbc.go
65 lines (57 loc) · 1.67 KB
/
tripledescbc.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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
package goEncrypt
import (
"crypto/cipher"
"crypto/des"
"fmt"
"runtime"
)
/*
@Time : 2018/11/1 22:50
@Author : wuman
@File : TripleDES_CBC
@Software: GoLand
*/
/**
Triple des encryption and decryption
algorithm : Encryption: key one encryption -> key two decryption -> key three encryption
Decryption: key three decryption -> key two encryption -> key one decryption
*/
func TripleDesEncrypt(plainText []byte,key [24]byte)([]byte,error){
cipherKey:=key[:]
block, err := des.NewTripleDESCipher(cipherKey)
if err != nil{
return nil,err
}
paddingText := PKCS5Padding(plainText, block.BlockSize())
iv :=[]byte(ivdes)
blockMode := cipher.NewCBCEncrypter(block, iv)
cipherText := make([]byte,len(paddingText))
blockMode.CryptBlocks(cipherText,paddingText)
return cipherText,nil
}
func TripleDesDecrypt(cipherText []byte,key [24]byte) ([]byte,error){
cipherKey:=key[:]
// 1. Specifies that the 3des decryption algorithm creates and returns a cipher.Block interface using the TDEA algorithm。
block, err := des.NewTripleDESCipher(cipherKey)
if err!=nil{
return nil,err
}
iv :=[]byte(ivdes)
blockMode := cipher.NewCBCDecrypter(block, iv)
paddingText := make([]byte,len(cipherText)) //
blockMode.CryptBlocks(paddingText,cipherText)
// 2. Delete the filling
// Before deleting, prevent the user from entering different keys twice and causing panic, so do an error handling
defer func(){
if err:=recover();err!=nil{
switch err.(type){
case runtime.Error:
fmt.Println("runtime error:",err,"Check that the key is correct")
default:
fmt.Println("error:",err)
}
}
}()
plainText := PKCS5UnPadding(paddingText)
return plainText,nil
}