zoukankan      html  css  js  c++  java
  • Golang中,Aes加解密

    今天在用Golang解析php那边用Aes加密的一个key。网上大多是用base64将结果编码一下。而且用到了向量。我php

    那边没有用到向量。所以golang这边也是要去掉的。参考网站的改了下。能够和php通用。

    另外,需要注意的是加密的key只能是16,24,32.分别对应的是AES-128,AES-192,AES-256等

    package main
    
    import (
    
    ​	"bytes"
    
    ​	"crypto/aes"
    
    ​	"crypto/cipher"
    
    ​	"encoding/base64"
    
    ​	"encoding/hex"
    
    ​	"errors"
    
    ​	"fmt"
    
    )
    
    
    
    //填充
    
    func pad(src []byte) []byte {
    
    ​	padding := aes.BlockSize - len(src)%aes.BlockSize
    
    ​	padtext := bytes.Repeat([]byte{byte(padding)}, padding)
    
    ​	return append(src, padtext...)
    
    }
    
    
    
    func unpad(src []byte) ([]byte, error) {
    
    ​	length := len(src)
    
    ​	unpadding := int(src[length-1])
    
    
    
    ​	if unpadding > length {
    
    ​		return nil, errors.New("unpad error. This could happen when incorrect encryption key is used")
    
    ​	}
    
    
    
    ​	return src[:(length - unpadding)], nil
    
    }
    
    
    
    func encrypt(key []byte, text string) (string, error) {
    
    ​	block, err := aes.NewCipher(key)
    
    ​	if err != nil {
    
    ​		return "", err
    
    ​	}
    
    
    
    ​	msg := pad([]byte(text))
    
    ​	ciphertext := make([]byte, aes.BlockSize+len(msg))
    
    
    
    ​	//没有向量,用的空切片
    
    ​	iv := make([]byte,aes.BlockSize)
     
    ​	mode := cipher.NewCBCEncrypter(block, iv)
    
    ​	mode.CryptBlocks(ciphertext[aes.BlockSize:], msg)
    
    
    
    ​	finalMsg := (base64.StdEncoding.EncodeToString(ciphertext))
    
    
    ​	fmt.Println(hex.EncodeToString([]byte(ciphertext[aes.BlockSize:])))
    
    
    ​	return finalMsg, nil
    
    }
    
    
    
    func decrypt(key []byte, text string) (string, error) {
    
    ​	block, err := aes.NewCipher(key)
    
    ​	if err != nil {
    
    ​		return "", err
    
    ​	}
     
    
    ​	decodedMsg,_ := hex.DecodeString(text)
    
    
    
    ​	iv  :=make([]byte,aes.BlockSize)
    
    ​	msg := decodedMsg
    
    
    
    ​	mode := cipher.NewCBCDecrypter(block, iv)
    
    ​	mode.CryptBlocks(msg,msg)
    
    
    
    ​	unpadMsg, err := unpad(msg)
    
    ​	if err != nil {
    
    ​		return "", err
    
    ​	}
    
    
    
    ​	return string(unpadMsg), nil
    
    }
    
    
    
    func main() {
    
    ​	key := []byte("0123456789abcdef")
    
    ​	encryptText, _ := encrypt(key, "123456")
    
    ​	rawText, err := decrypt(key, "2994dc19badcd3e820065f4f8211f584")
    
    ​	fmt.Println("text %s 
    ", rawText)
    
    }
    
  • 相关阅读:
    事务四大特征:原子性,一致性,隔离性和持久性(ACID)
    解决“要登录到这台远程计算机,你必须被授予”
    SqlServer_查看SQLServer版本信息
    sed 查找文件的某一行内容
    linux echo命令的-n、-e两个参数
    在.Net中进行跨线程的控件操作(上篇:Control.Invoke)
    .NET性能优化方面的总结
    SQLSERVER2008 显示列信息,包含扩展属性
    C#4.0新特性:可选参数,命名参数,Dynamic
    浅谈.net中的params关键字
  • 原文地址:https://www.cnblogs.com/smartrui/p/10150759.html
Copyright © 2011-2022 走看看