Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1097071
  • 博文数量: 80
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 746
  • 用 户 组: 普通用户
  • 注册时间: 2018-06-12 20:01
个人简介

寫写code、调調bug、填填坑,僅此而已。

文章分类

全部博文(80)

文章存档

2019年(30)

2018年(50)

分类: C/C++

2018-11-27 09:08:39


点击(此处)折叠或打开

  1. package main

  2. import (
  3.     "bytes"
  4.     "crypto/cipher"
  5.     "crypto/des"
  6.     "encoding/base64"
  7.     "fmt"
  8.     "reflect"
  9.     "unsafe"
  10. )
  11. const KEYS = "0123456789qwertyuiopasdf" //必须24位

  12. func String(b []byte) (s string) {
  13.     pbytes := (*reflect.SliceHeader)(unsafe.Pointer(&b))
  14.     pstring := (*reflect.StringHeader)(unsafe.Pointer(&s))
  15.     pstring.Data = pbytes.Data
  16.     pstring.Len = pbytes.Len
  17.     return
  18. }
  19. func PKCS5UnPadding(origData []byte) []byte {
  20.     length := len(origData)
  21.     unpadding := int(origData[length-1])
  22.     return origData[:(length - unpadding)]
  23. }
  24. func PKCS5Padding(ciphertext []byte, blockSize int) []byte {
  25.     padding := blockSize - len(ciphertext)%blockSize
  26.     padtext := bytes.Repeat([]byte{byte(padding)}, padding)
  27.     return append(ciphertext, padtext...)
  28. }

  29. func Slice(s string) (b []byte) {
  30.     pbytes := (*reflect.SliceHeader)(unsafe.Pointer(&b))
  31.     pstring := (*reflect.StringHeader)(unsafe.Pointer(&s))
  32.     pbytes.Data = pstring.Data
  33.     pbytes.Len = pstring.Len
  34.     pbytes.Cap = pstring.Len
  35.     return
  36. }

  37. func TripleDesEncrypt(origData, key string) (string, error) {

  38.     borigData := Slice(origData)
  39.     bkey := Slice(key)

  40.     block, err := des.NewTripleDESCipher(bkey)
  41.     if err != nil {
  42.         panic(err)
  43.         return "", err
  44.     }
  45.     borigData = PKCS5Padding(borigData, block.BlockSize())
  46.     blockMode := cipher.NewCBCEncrypter(block, bkey[:8])
  47.     crypted := make([]byte, len(borigData))
  48.     blockMode.CryptBlocks(crypted, borigData)

  49.     encodeString := base64.StdEncoding.EncodeToString(crypted)
  50.     return encodeString, nil
  51. }

  52. func DesDecrypt(crypted, key string) (string, error) {

  53.     bkey := Slice(key)
  54.     decodeBytes, err := base64.StdEncoding.DecodeString(crypted)
  55.     if err != nil {
  56.         panic(err)
  57.     }
  58.     block, err := des.NewTripleDESCipher(bkey)
  59.     if err != nil {
  60.         panic(err)
  61.         return "", err
  62.     }
  63.     blockMode := cipher.NewCBCDecrypter(block, bkey[:8])
  64.     origData := make([]byte, len(decodeBytes))
  65.     blockMode.CryptBlocks(origData, decodeBytes)
  66.     origData = PKCS5UnPadding(origData)
  67.     return String(origData), nil
  68. }

  69. //测试加密解密
  70. func test() {
  71.     byt, _ := TripleDesEncrypt("chinaunix2019", KEYS)
  72.     fmt.Println(byt)
  73.     dbyt, _ := DesDecrypt(byt, KEYS)
  74.     fmt.Println(dbyt)
  75. }
  76. func main() {
  77.     test()
  78. }

阅读(4346) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~