AES加解密的简单实现,代码如下。

package main

import (
"crypto/aes"
"crypto/cipher"
"encoding/hex"
"fmt"
) func main(){ nonce := "37b8e8a308c354048d245f6d"
key := "AES256Key-32Characters1234567890"
plainText := "172.10.99.88"
cipherText := ExampleNewGCM_encrypt(plainText, key, nonce)
newPlain := ExampleNewGCM_decrypt(cipherText, key, nonce) fmt.Println("plain:", plainText)
fmt.Println("cipher:", cipherText)
fmt.Println("new plain:", newPlain)
} func ExampleNewGCM_encrypt(src, k, n string)string {
// The key argument should be the AES key, either 16 or 32 bytes
// to select AES-128 or AES-256.
key := []byte(k)
plaintext := []byte(src) block, err := aes.NewCipher(key)
if err != nil {
panic(err.Error())
} nonce, _ := hex.DecodeString(n) aesgcm, err := cipher.NewGCM(block)
if err != nil {
panic(err.Error())
} ciphertext := aesgcm.Seal(nil, nonce, plaintext, nil) return fmt.Sprintf("%x", ciphertext)
} func ExampleNewGCM_decrypt(src, k, n string) string {
// The key argument should be the AES key, either 16 or 32 bytes
// to select AES-128 or AES-256.
key := []byte(k)
ciphertext, _ := hex.DecodeString(src) nonce, _ := hex.DecodeString(n) block, err := aes.NewCipher(key)
if err != nil {
panic(err.Error())
} aesgcm, err := cipher.NewGCM(block)
if err != nil {
panic(err.Error())
} plaintext, err := aesgcm.Open(nil, nonce, ciphertext, nil)
if err != nil {
panic(err.Error())
} return string(plaintext)
}

Output:

plain: 172.10.99.88

cipher: 4456f9258c204906cbb2516e1fc78c3fbbf439e9e7d49189a391ee33

new plain: 172.10.99.88

Go语言加解密--AES简单实践的更多相关文章

  1. Java 加解密 AES DES TripleDes

    package xxx.common.util; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import javax.crypt ...

  2. python全栈开发day115、116-websocket、websocket原理、websocket加解密、简单问答机器人实现

    1.websocket 1.websocket 与轮询 轮询: 不断向服务器发起询问,服务器还不断的回复 浪费带宽,浪费前后端资源 保证数据的实时性 长轮询: 1.客户端向服务器发起消息,服务端轮询, ...

  3. Delphi与JAVA互加解密AES算法

    搞了半天终于把这个对应的参数搞上了,话不多说,先干上代码: package com.bss.util; import java.io.UnsupportedEncodingException; imp ...

  4. Python3 AES加解密(AES/ECB/PKCS5Padding)

    class AesEncry(object): key = "wwwwwwwwwwwwwwww" # aes秘钥 def encrypt(self, data): data = j ...

  5. iOS,信息加解密

    1.AES加解密 AES加解密 // //  AESEncryptAndDecrypt.h //  NSData扩展方法,用于处理aes加解密 // //  Created by Vie on 16/ ...

  6. SWF加解密资源索引之加密混淆篇【转】

    ============================ SWF加解密资源索引之加密混淆篇 ============================ [心得] swf加密混淆器(带源码) http:/ ...

  7. cryptoJS AES 加解密简单使用

    简单记录一下,前端利用 cryptoJS 如何加解密的.主要是关于 AES 加解密. 需求描述:需要对 url 中的参数进行 AES 解密,然后再把该参数进行 MD5 加密通过接口传递. AES AE ...

  8. 学习Java AES加解密字符串和文件方法,然后写个简单工具类

    Reference Core Java Volume Ⅱ 10th Edition 1 对称加密 "Java密码扩展"包含了一个Cipher,它是所有密码算法的超类.通过getIn ...

  9. AES对称加解密

    简介设计思想加密模式ECB模式(电子密码本模式:Electronic codebook)CBC模式(密码分组链接:Cipher-block chaining)CFB模式(密文反馈:Cipher fee ...

随机推荐

  1. ubantu 安装mysql 5.7 解决安装不提示设置密码问题

    1.安装Mysql sudo apt-get install mysql-server sudo apt-get install mysql-client sudo apt-get install l ...

  2. VTP管理交换机的VLAN配置

    实验要求:将Switch1设置为VTPserver.Switch2设置为VTPtransparent.Swtich3和4设置为VTPclient 拓扑如下: 涉及内容: 1.VTP的创建 2.VTP的 ...

  3. Spring Batch 简介

    Spring Batch是一个轻量级的,完全面向Spring的批处理框架,可以应用于企业级大量的数据处理系统.Spring Batch以POJO和大家熟知的Spring框架为基础,使开发者更容易的访问 ...

  4. SpringMVC详细示例实战教程(较全开发教程)

    SpringMVC学习笔记---- 一.SpringMVC基础入门,创建一个HelloWorld程序 1.首先,导入SpringMVC需要的jar包. 2.添加Web.xml配置文件中关于Spring ...

  5. Python学习笔记第十九周

    目录: 一.路由系统URL 1.Django请求生命周期 2.创建Django project 3.配置 4.编写程序 二.视图 三.模板 四.ORM操作 内容: 一.URL 1.Django请求生命 ...

  6. CentOS 使用 yum 更新软件包与系统

    1.CentOS 更新源配置文件说明 CentOS 6.5 更新源配置文件 /etc/yum.repos.d/CentOS-Base.repo 片段 [base] name=CentOS-$relea ...

  7. PTA——时间转换

    PTA 7-14 然后是几点 #include<stdio.h> int main() { int a,b,hour,min; scanf("%d%d",&a, ...

  8. 《DSP using MATLAB》Problem 5.31

    第3小题: 代码: %% ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ %% Out ...

  9. 进入网站自动加自己为QQ好友代码

    <meta http-equiv="refresh" content="0; url=tencent://AddContact/?fromId=50&fro ...

  10. Go Example--常量

    package main import ( "fmt" "math" ) const s string = "constant" //定义字 ...