Go语言加解密--AES简单实践
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简单实践的更多相关文章
- Java 加解密 AES DES TripleDes
package xxx.common.util; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import javax.crypt ...
- python全栈开发day115、116-websocket、websocket原理、websocket加解密、简单问答机器人实现
1.websocket 1.websocket 与轮询 轮询: 不断向服务器发起询问,服务器还不断的回复 浪费带宽,浪费前后端资源 保证数据的实时性 长轮询: 1.客户端向服务器发起消息,服务端轮询, ...
- Delphi与JAVA互加解密AES算法
搞了半天终于把这个对应的参数搞上了,话不多说,先干上代码: package com.bss.util; import java.io.UnsupportedEncodingException; imp ...
- Python3 AES加解密(AES/ECB/PKCS5Padding)
class AesEncry(object): key = "wwwwwwwwwwwwwwww" # aes秘钥 def encrypt(self, data): data = j ...
- iOS,信息加解密
1.AES加解密 AES加解密 // // AESEncryptAndDecrypt.h // NSData扩展方法,用于处理aes加解密 // // Created by Vie on 16/ ...
- SWF加解密资源索引之加密混淆篇【转】
============================ SWF加解密资源索引之加密混淆篇 ============================ [心得] swf加密混淆器(带源码) http:/ ...
- cryptoJS AES 加解密简单使用
简单记录一下,前端利用 cryptoJS 如何加解密的.主要是关于 AES 加解密. 需求描述:需要对 url 中的参数进行 AES 解密,然后再把该参数进行 MD5 加密通过接口传递. AES AE ...
- 学习Java AES加解密字符串和文件方法,然后写个简单工具类
Reference Core Java Volume Ⅱ 10th Edition 1 对称加密 "Java密码扩展"包含了一个Cipher,它是所有密码算法的超类.通过getIn ...
- AES对称加解密
简介设计思想加密模式ECB模式(电子密码本模式:Electronic codebook)CBC模式(密码分组链接:Cipher-block chaining)CFB模式(密文反馈:Cipher fee ...
随机推荐
- 1-log4j2入门
一.入门介绍 log4j apache实现的一个开源的日志组件 slf4j 日志接口即为所有日志框架制定的一种规范的标准接口,并不是一个框架的具体实现,需要配合具体的日志框架使用(如log4j2.lo ...
- vue--http请求的封装--session
export function Fecth (url, data, file, _method) { if (file) { // 需要上传文件 return new Promise((resolve ...
- saliency 2015-2016的论文、代码
https://github.com/ArcherFMY/Paper_Reading_List 另外2013年之前的见chengmingming的benchmark主页.
- MySQL(2)数据库 表的查询操作
来源参考https://www.cnblogs.com/whgk/p/6149009.html 跟着源博客敲一遍可以加深对数据库的理解,同时对其中一些代码做一些改变,可以验证自己的理解. 本文改动了其 ...
- MATLAB的一些小经验,记下来,facilitate future work
[转载请注明出处]http://www.cnblogs.com/mashiqi 2016/03/28 0.杂.这个帖子(https://www.zhihu.com/question/24499729) ...
- I.MX6 使用Ubuntu文件系统
/********************************************************************************* * I.MX6 使用Ubuntu文 ...
- Templates中的macro和include标签
1.macro标签 1.作用:相当于在模板中声名函数 2.使用方法: 语法:{% macro 名称(参数列表) %} xxx {% endmacro %} 创建 macro.html 模板文件 - ...
- Map集合与转化
|--Hashtable:底层是哈希表数据结构,是线程同步的.不可以存储null键,null值. |--HashMap:底层是哈希表数据结构,是线程不同步的.可以存储null键,null值.替代了Ha ...
- hdu 1864 最大报销额 01背包
Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submission(s) ...
- C++学习(三十二)(C语言部分)之 栈
栈测试代码笔记如下: #include<stdio.h> #include<string.h> #include <stdlib.h> #define SIZE 1 ...