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. ios轮播图片用法

    // // ZQRViewController.m // 04-图片轮播器 // // Created by apple on 17-08-24. // Copyright (c) 2017年 zzq ...

  2. mysql创建用户并授予权限

    MySQL创建数据库与创建用户以及授权   1.create schema [数据库名称] default character set utf8 collate utf8_general_ci;--创 ...

  3. wx小程序使用模板消息

    1.直接搜索一个不存在的模板,则可以添加新模板 2.https://developers.weixin.qq.com/miniprogram/dev/api/notice.html#%E5%8F%91 ...

  4. prototype和_proto_

    __proto__(隐式原型)与prototype(显式原型) 显式原型 explicit prototype property:用来实现基于原型的继承与属性的共享. 每一个函数在创建之后都会拥有一个 ...

  5. Vue 之 element-ui upload组件的文件类型

    在使用element-ui的upload上传组件的时候,有时候会遇到 控制上传文件类型 的需求,只需要配置accept属性为允许的类型即可,比如: <el-upload class=" ...

  6. 河南省第四届ACM省赛(T1) 序号互换

    问题 A: 序号互换 时间限制: 1 Sec  内存限制: 128 MB难度1 题目描述 Dr.Kong设计了一个聪明的机器人卡多,卡多会对电子表格中的单元格坐标快速计算出来.单元格的行坐标是由数字编 ...

  7. 找出n个自然数(1,2,3,……,n)中取r个数的组合

    <?php /** * 对于$n和$r比较小, 可以用这种方法(当n=5, r=3时) */ function permutation1($n, $r) { for($i=1; $i<=$ ...

  8. MATLAB的一些小经验,记下来,facilitate future work

    [转载请注明出处]http://www.cnblogs.com/mashiqi 2016/03/28 0.杂.这个帖子(https://www.zhihu.com/question/24499729) ...

  9. selected标签判断默认选中

    <select name="suggestedType" style="width:280px" > <option value=" ...

  10. debian 配置linuxptp 软件时间戳

    编程之路刚刚开始,错误难免,希望大家能够指出. ntp,ptp,ntp,ptp 本文只说软件时间戳 先上几个推荐的网址,可以更好的了解ptp: https://docs.fedoraproject.o ...