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. 升级ChinaCock 10.3遇到的问题

    1.引用ChinaCockFMX.jar,无法编译 因为专家重新改了fmx.dex.jar中的内容,并集成到ChinaCockFMX.jar中,所以需要去掉fmx.dex.jar的引用. 2.导航无法 ...

  2. Unknown parameter datatype UNKNOW send from server.

    procedure Tmainform.Button7Click(Sender: TObject); begin kbmMWClientQuery3.Query.Text:='insert into ...

  3. kbmMW SmartService控制返回类型

  4. java学习笔记19(Arrays类)

    Arrays类: 此类包含用来操作数组的各种方法(比如升序和搜索): import java.util.Arrays; public class Demo { public static void m ...

  5. Python基础6--函数、类和文件操作

    1 def name(para) def myabs(x): if x>0: return x else: return -x 2 lambda表达式 用于声明匿名函数,既没有名字的小函数 f ...

  6. Spring Boot 揭秘与实战 附录 - Spring Boot 公共配置

    Spring Boot 公共配置,配置 application.properties/application.yml 文件中. 摘自:http://docs.spring.io/spring-boot ...

  7. 【leetcode】67-AddBinary

    problem AddBinary code class Solution { public: string addBinary(string a, string b) { string res; ; ...

  8. OpenCV 自定义任意区域形状及计算平均值 方差

    opencv中有矩形的Rect函数.圆形的circl函数等,那么任意形状怎么取呢?方法1:点乘,将其形状与图像进行点乘,求其形状对应的图像形状:方法2:用findContours函数得对应的形状区域, ...

  9. ccf-170902-公共钥匙盒(模拟)

    这是一道典型的模拟题 首先我们把借钥匙和还钥匙切分成两个事件 保存于两个数组中 然后我对还钥匙的活动按照时间发生次序和还得钥匙序号排序,即按照题意对事件发生的次序排序 最后按照时间的进行 一个一个进行 ...

  10. java实现各种排序算法

    java实现各种排序算法 import java.util.Arrays; public class SomeSort { public static void main(String[] args) ...