Use SHA256, MD5, RIPEMD160
import ( "fmt" "crypto/sha256" "os" "io" "crypto/md5" "golang.org/x/crypto/ripemd160")func main() { str := "hello world" sum := sha256.Sum256([]byte(str)) fmt.Printf("SHA256:%x\n", sum) fileSha156() result := md5.Sum([]byte(str)) fmt.Printf("MD5:%x\n", result) hasher := ripemd160.New() // 将加密内容的字节数组拷贝到ripemd160 hasher.Write([]byte(str)) fmt.Printf("RIPEMD160:%x", hasher.Sum(nil))}/** * 使用SHA256加密文件内容 */func fileSha156() { file, err := os.OpenFile("e:/test.txt", os.O_RDONLY, 0777) if err != nil { panic(err) } defer file.Close() h := sha256.New() // 将文件内容拷贝到sha256中 io.Copy(h, file) fmt.Printf("%x\n", h.Sum(nil))}
Using des
Import ("bytes" "Crypto/cipher"//cipher password "crypto/des" "encoding/base64"//Convert object to String "FMT")/** * des encryption method */func Mydesencrypt (orig, key string) string{//Convert encrypted contents and keys to byte array origdata: = []byte (orig) k: = []byte (key)//Secret Key group block, _: = Des. Newcipher (k)//The full text is done by the length of the secret key origdata = Pkcs5padding (Origdata, block. BlockSize ())//Set Encryption mode-CBC Blockmode: = cipher. Newcbcdecrypter (block, k)//Create byte array with clear text length crypted: = Make ([]byte, Len (origdata))//Encrypt plaintext Blockmode.cryptblocks (CR ypted, Origdata)//Convert the byte array to a string, base64 encode return base64. Stdencoding.encodetostring (crypted)}/** * des decryption method */func mydesdecrypt (data string, key String) string {k: = []byte (key )//Convert the encrypted string into a byte array crypted, _: = Base64. base64. Stdencoding.decodestring (data)//Convert the byte secret key to block fast block, _: = Des. Newcipher (k)//Set decryption mode-CBC Blockmode: = cipher. Newcbcencrypter (block, k)//Create cipher size array variable origdata: = Make ([]byte, Len (crypted))//decryption to array Origdata blockmode.cr YptblOcks (Origdata, crypted)//Remove the part of the completion of the encryption Origdata = pkcs5unpadding (origdata) return string (Origdata)}/** * Implementation of the full text complement * If the length of the ciphertext is an integer multiple of blockSize, then no completion is required * Otherwise the difference is several, example: 5 difference 5 5 */func pkcs5padding (ciphertext []byte, BlockSize int) [] byte {padding: = Blocksize-len (ciphertext)%blocksize padtext: = bytes. Repeat ([]byte{byte (padding)}, padding) return append (ciphertext, padtext ...)} /** * Implements de-complement, pkcs5padding's inverse function */func pkcs5unpadding (origdata []byte) []byte {length: = Len (origdata)//Remove last byte UNP Adding times unpadding: = Int (origdata[length-1]) return origdata[:(length-unpadding)]}func main () {orig: = "Hello world! " Fmt. Println ("Original:", orig)//Declaration key, using this key to implement plaintext encryption and ciphertext decryption, the length must be 8 key: = "12345678"//encryption Encyptcode: = Mydesencrypt (Orig, Key) Fmt. Println ("Ciphertext:", Encyptcode)//decryption Decyptcode: = Mydesdecrypt (Encyptcode, key) fmt. Println ("Decryption result:", Decyptcode)}
Use 3DES
Import ("bytes" "Crypto/cipher" "Crypto/des" "Encoding/base64" "FMT") func main () {orig: = "Hello World" "//3DES must have a key length of 24-bit key: =" 123456781234567812345678 "FMT. Println ("Original:", orig) Encryptcode: = Tripledesencrypt (orig, key) fmt. Println ("Ciphertext:", Encryptcode) Decryptcode: = Tipledesdecrypt (Encryptcode, key) fmt. Println ("Decryption result:", Decryptcode)}/** * Encrypt */func tripledesencrypt (orig, key String) string {//goto byte array Origdata: = []by Te (orig) k: = []byte (key)//3DES key length must be 24-bit block, _: = Des. Newtripledescipher (k)//Complement full code Origdata = pkcs5padding (Origdata, block. BlockSize ())//Set encryption mode Blockmode: = cipher. Newcbcencrypter (block, K[:8])//Create ciphertext array crypted: = Make ([]byte, Len (origdata))//Encrypt blockmode.cryptblocks (cry pted, Origdata) return base64. Stdencoding.encodetostring (crypted)}/** * Decrypt */func tipledesdecrypt (crypted string, key String) string {//with Base64 to bytes Array Cryptedbyte, _: = base64. Stdencoding.decodestring (crypted)//key turns into byte array k: = []byte (key) block, _: = Des. Newtripledescipher (k) Blockmode: = cipher. Newcbcdecrypter (block, K[:8]) Origdata: = Make ([]byte, Len (cryptedbyte)) blockmode.cryptblocks (Origdata, Cryptedbyte ) Origdata = pkcs5unpadding (origdata) return string (origdata)}func pkcs5padding (orig []byte, size int) []byte {L Ength: = Len (orig) padding: = size-length%size paddinttext: = bytes. Repeat ([]byte{byte (padding)}, padding) return append (orig, paddinttext ...)} Func pkcs5unpadding (Origdata []byte) []byte {length: = Len (origdata)//Remove Last byte unpadding times unpadding: = Int (or IGDATA[LENGTH-1]) return origdata[:(length-unpadding)]}
Using AES
Import ("bytes" "Crypto/aes" "FMT" "Crypto/cipher" "Encoding/base64") func main () {orig: = "Hello World" "Key: =" 123456781234567812345678 "FMT. Println ("Original:", orig) Encryptcode: = Aesencrypt (orig, key) fmt. Println ("Ciphertext:", Encryptcode) Decryptcode: = Aesdecrypt (Encryptcode, key) fmt. Println ("Decryption result:", Decryptcode)}func aesencrypt (Orig string, key String) string {//goto byte array Origdata: = []byte (orig) K: = []byte (key)//packet key block, _: = AES. Newcipher (k)//Gets the length of the secret key block blockSize: = block. BlockSize ()//full Code Origdata = pkcs7padding (Origdata, BlockSize)//encryption mode Blockmode: = cipher. Newcbcencrypter (block, k[:blocksize])//create array cryted: = Make ([]byte, Len (origdata))//Encrypt Blockmode.cryptblock S (cryted, Origdata) return base64. Stdencoding.encodetostring (cryted)}func aesdecrypt (cryted string, key String) string {//goto byte array Crytedbyte, _: = Ba Se64. Stdencoding.decodestring (cryted) k: = []byte (key)//group secret key BloCK, _: = AES. Newcipher (k)//Gets the length of the secret key block blockSize: = block. BlockSize ()///encryption mode Blockmode: = cipher. Newcbcdecrypter (block, k[:blocksize])//create array orig: = Make ([]byte, Len (crytedbyte))//Decrypt Blockmode.cryptblock S (orig, crytedbyte)//go to complement full code orig = pkcs7unpadding (orig) return string (orig)}//complement func pkcs7padding (ciphertext []by TE, blocksize int) []byte {padding: = Blocksize-len (ciphertext)%blocksize padtext: = bytes. Repeat ([]byte{byte (padding)}, padding) return append (ciphertext, padtext ...)} De-code func pkcs7unpadding (Origdata []byte) []byte {length: = Len (origdata) unpadding: = Int (origdata[length-1]) re Turn origdata[:(length-unpadding)]}
Use of various cryptographic algorithms in the Go language