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 ...
随机推荐
- MongoDB的安装及安装为windows服务
首先在网上找了一篇教程,于是按着做,下载了最新版的安装包,解压以后目录是这样的: 没有bin目录啊 原因:zip没有经过编译 于是下载安装包,安装包是msi 一直按照next提示就可以了,但是记得选择 ...
- Delphi 10.3.1来了
10.3.1发布了,这个版本可以独自安装,是对Delphi 10.3 Rio,C ++ Builder 10.3 Rio和RAD Studio 10.3 Rio的更新.如果安装了2018年11月发布的 ...
- IDEA PYCHARM USAGE NOTE
初次安装使用PyCharm,在新建.py文件时会发现文件头并没有什么信息,因此,使用模板会比较方便. 方法如下: 1.打开PyCharm,选择File--Settings 2.依次选择Editor-- ...
- SpringBoot(2.0.4.RELEASE)+Elasticsearch(6.2.4)+Gradle简单整合
记录一下SpringBoot(2.0.4.RELEASE)+Elasticsearch(6.2.4)+Gradle整合的一个小例子. 1.在Gradle内加入相关jar包的依赖: compile('o ...
- Spring Boot 揭秘与实战(七) 实用技术篇 - 异步任务
文章目录 1. Spring Boot 集成异步任务 2. 单发服务模式 3. 请求应答模式 4. 源代码 Spring 对异步任务具有很好的支持.这篇文章,我们透过 Spring Boot 来讲解下 ...
- 阿里云CentOS中vsftp安装、配置、卸载
1--卸载 查看当前服务器中的vsftpdrpm -qa|grep vsftpd 例如结果为:vsftpd-2.2.2-13.el6_6.1.x86_64执行卸载rpm -e vsftpd-2.2.2 ...
- Pacman命令介绍
用pacman很久了,一直没有深入去研究到底它包含多少神秘的力量,每次都是pacman -Syu就简单的把所有升级更新问题搞定实在是让笔者懒惰了很多.不过也正是因为pacman的简单让笔者爱不释手,同 ...
- thinkphp+redis实现秒杀功能(转)
1,安装redis,根据自己的php版本安装对应的redis扩展(此步骤简单的描述一下) 1.1,安装 php_igbinary.dll,php_redis.dll扩展此处需要注意你的php版本如图: ...
- 创建small表空间size32G报错ORA-01144
#报错现象 #创建Oracle数据库11g默认标准块大小8K,直接分配32g报错 create tablespace ceshi datafile '+data' size 32g * ERROR a ...
- 在django中进行MySQL入库
在django中进行mysql 入库 需要导入 : from django.db import models 在添加主键时,需要使用: primary_key=True id = models. ...