Golang : Encrypt and decrypt data with TripleDES
In this tutorial we will learn how to encrypt and decrypt data with Golang's DES crypto package.
Triple DES (3DES) is the common name for the Triple Data Encryption Algorithm (TDEA or Triple DEA) symmetric-key block cipher, which applies the Data Encryption Standard (DES) cipher algorithm three times to each data block.
Here is the source code for encrypting and decrypting a 8 bytes string message with TripleDES :
package main
import (
"fmt"
"crypto/des"
"crypto/cipher"
"os"
)
func main() {
// because we are going to use TripleDES... therefore we Triple it!
triplekey := "12345678" + "12345678" + "12345678"
// you can use append as well if you want
// plaintext will cause panic: crypto/cipher: input not full blocks
// IF it is not the correct BlockSize. ( des.BlockSize = 8 bytes )
// to fix this issue, plaintext may need to be padded to the whole block
// ( 8 bytes ) for the simplicity of this tutorial, we will just keep
// the plaintext input to 8 bytes
plaintext := []byte("Hello Wo") // Hello Wo = 8 bytes.
block,err := des.NewTripleDESCipher([]byte(triplekey))
if err != nil {
fmt.Printf("%s \n", err.Error())
os.Exit(1)
}
fmt.Printf("%d bytes NewTripleDESCipher key with block size of %d bytes\n", len(triplekey), block.BlockSize)
ciphertext := []byte("abcdef1234567890")
iv := ciphertext[:des.BlockSize] // const BlockSize = 8
// encrypt
mode := cipher.NewCBCEncrypter(block, iv)
encrypted := make([]byte, len(plaintext))
//mode.CryptBlocks(ciphertext[des.BlockSize:], plaintext)
mode.CryptBlocks(encrypted, plaintext)
fmt.Printf("%s encrypt to %x \n", plaintext, encrypted)
//decrypt
decrypter := cipher.NewCBCDecrypter(block, iv)
decrypted := make([]byte, len(plaintext))
decrypter.CryptBlocks(decrypted, encrypted)
fmt.Printf("%x decrypt to %s\n", encrypted, decrypted)
}
Executing the above code will produce the following result :
go run tripledescrypto.go
24 bytes NewTripleDESCipher key with block size of 10144 bytes
Hello Wo encrypt to 5fe6b99beabfbb25
5fe6b99beabfbb25 decrypt to Hello Wo
Reference :
http://golang.org/pkg/crypto/des/#NewTripleDESCipher
See also : Golang : Padding data for encryption and un-padding data for decryption
By Adam Ng
IF you gain some knowledge or the information here solved your programming problem. Please consider donating to the less fortunate or some charities that you like. Apart from donation, planting trees, volunteering or reducing your carbon footprint will be great too.
Advertisement
Tutorials
+8.9k Golang : Populate or initialize struct with values example
+18.9k Golang : Padding data for encryption and un-padding data for decryption
+12.4k Elastic Search : Return all records (higher than default 10)
+43.1k Golang : Convert []byte to image
+11.9k Golang : Find and draw contours with OpenCV example
+27.8k Golang : Decode/unmarshal unknown JSON data type with map[string]interface
+4.2k Golang : Converting individual Jawi alphabet to Rumi(Romanized) alphabet example
+14.5k Golang : Missing Bazaar command
+6k Golang : How to verify input is rune?
+5.9k Golang : Function as an argument type example
+7.1k Ubuntu : connect() to unix:/var/run/php5-fpm.sock failed (13: Permission denied) while connecting to upstream
+5.6k Unix/Linux/MacOSx : Get local IP address