Go lang 3DES encryption/Decryption

by Anish

Posted on Wednesday April 3 , 2019

This sample chapter extracted from the book, Go Lang Cryptography for Developers . The Book theme isCryptography is for EveryOne. Learn from Crypto Principle to Applied Cryptography With Practical Example


DES is the old "Data Encryption Standard" from the seventies. Its key size is too short for proper security (56 effective bits; this can be brute-forced, as has been demonstrated more than ten years ago). Also, DES uses 64-bit blocks, which raises some potential issues when encrypting several gigabytes of data with the same key (a gigabyte is not that big nowadays).

3DES is a trick to reuse DES implementations, by cascading three instances of DES (with distinct keys). 3DES is believed to be secure up to at least "2112" security (which is quite a lot, and quite far in the realm of "not breakable with today's technology").

Go lang builtin Package des implements the Data Encryption Standard (DES) and the Triple Data Encryption Algorithm (TDEA) as defined in U.S. Federal Information Processing Standards Publication 46-3.

fun NewCipher creates and returns a new cipher.Block.

func NewCipher(key []byte) (cipher.Block, error)

fun NewTripleDESCipher creates and returns a new cipher.Block.

func NewTripleDESCipher(key []byte) (cipher.Block, error)

The following example will show how to perform 3DES encryption and decryption of the given plain text message

  • The Input key length should be 24
package main  
  
import (  
   "crypto/des"  
   "encoding/hex" 
   "fmt"
   )  
  
func main() {  
  
   // Mimimum Key Size of Length 24  
  key := "mysecretPasswordkeySiz24"  
  plainText := "https://8gwifi.org"  
  ct := EncryptTripleDES([]byte(key), plainText)  
   fmt.Printf("Original Text:  %s\n", plainText)  
   fmt.Printf("3DES Encrypted Text:  %s\n", ct)  
   DecryptTripleDES([]byte(key), ct)  
  
}  
  
func EncryptTripleDES(key []byte, plaintext string) string {  
   c, err := des.NewTripleDESCipher(key)  
   if err != nil {  
      fmt.Errorf("NewTripleDESCipher(%d bytes) = %s", len(key), err)  
      panic(err)  
   }  
  
   out := make([]byte, len(plaintext))  
   c.Encrypt(out, []byte(plaintext))  
   return hex.EncodeToString(out)  
  
}  
  
func DecryptTripleDES(key []byte, ct string) {  
  
   ciphertext, _ := hex.DecodeString(ct)  
   c, err := des.NewTripleDESCipher([]byte(key))  
   if err != nil {  
      fmt.Errorf("NewTripleDESCipher(%d bytes) = %s", len(key), err)  
      panic(err)  
   }  
   plain := make([]byte, len(ciphertext))  
   c.Decrypt(plain, ciphertext)  
   s := string(plain[:])  
   fmt.Printf("3DES Decrypyed Text:  %s\n", s)  
}

Thanku for reading !!! Give a Share for Support

Asking for donation sound bad to me, so i'm raising fund from by offering all my Nine book for just $9



python Cryptography Topics
Topics
For Coffee/ Beer/ Amazon Bill and further development of the project Support by Purchasing, The Modern Cryptography CookBook for Just $9 Coupon Price

Kubernetes for DevOps

Hello Dockerfile

Cryptography for Python Developers

Cryptography for JavaScript Developers

Go lang ryptography for Developers

Here