golang 加密文件

Cryptography is mandatory in the current modern world. In this era of big data and data science, it is important to make sure your data is protected from a malicious attack.

在当前现代世界中,密码术是必不可少的。 在当今大数据和数据科学时代,确保保护您的数据免受恶意攻击非常重要。

There are many algorithms and techniques to ensure that your system is secure and no unauthorized access will be done to your data.

有许多算法和技术可以确保您的系统安全,并且不会对数据进行未经授权的访问。

Symmetric encryption is one of these techniques, and in this tutorial, I will show you how to do it in Go.

对称加密是这些技术之一,在本教程中,我将向您展示如何在Go中进行加密。

Note: The version of Go used in this tutorial is 1.14.7.

注意:本教程中使用的Go版本是1.14.7。

介绍 (Introduction)

The symmetric-key encryption is a technique used to encrypt a message where there is only one secret key. This key is used on both parts of the process, that is, to encrypt and decrypt the message.

对称密钥加密是一种用于加密只有一个秘密密钥的消息的技术。 该密钥在过程的两个部分都使用,即加密和解密消息。

Image by author.
图片由作者提供。

Using any kind of symmetric-key algorithm, a message is converted into a form that cannot be understood. Unless someone has the secret key. In this case, both the sender and the receiver have the same key (exchanged in some secure way) so they can send messages and understand each other.

使用任何一种对称密钥算法,都会将消息转换为无法理解的形式。 除非有人拥有密钥。 在这种情况下,发送方和接收方都具有相同的密钥(以某种安全方式进行了交换),因此它们可以发送消息并相互理解。

The algorithm used determines the size of the key and the mode that the process of encryption or decryption is done. And, usually, longer the key, harder is to hack it and more secure is your encryption. For example, a 128-bits key can take up to billions of years to be broken by a normal computer.

所使用的算法确定密钥的大小以及完成加密或解密过程的方式。 而且,通常,密钥越长,破解就越困难,而加密则更加安全。 例如,一个128位的密钥可能需要数十亿年才能被普通计算机破解。

There are two types of algorithms:

有两种算法:

  • Block: the message is encrypted using fixed-size blocks. Example: DES, AES, etc.

    :使用固定大小的块对消息进行加密。 例如: DES , AES等。

  • Stream: the message is encrypted taking individual characters. Example: RC4, Salsa20, etc.

    :消息采用单个字符进行加密。 例如: RC4 , Salsa20等

模式 (Modes)

Since the block ciphers only operate over a fixed-length group of bits, you have to describe how to repeat the single operation to transform a data that is larger than a block.

由于块密码仅在固定长度的位组上运行,因此您必须描述如何重复单个操作来转换大于块的数据。

Most of the modes require a unique initial sequence, often called an Initialization Vector (IV). The IV has to be non-repeating and sometimes also random.

大多数模式需要唯一的初始序列,通常称为初始化向量(IV)。 IV必须是非重复的,有时还必须是随机的。

If the data length is not multiple of the block size, the last block is padded to a full block. However, some modes do not require it since they use it as a stream cipher. Some common modes:

如果数据长度不是块大小的倍数,则将最后一个块填充为完整块。 但是,某些模式不需要它,因为它们将其用作流密码。 一些常见的模式:

  • ECB: the simplest. Encrypt each block separately. This mode is not safe at all and should not be used.

    ECB :最简单。 分别加密每个块。 此模式根本不安全,不应使用。

WhiteTimberwolf (SVG version) / Public domain
WhiteTimberwolf(SVG版本)/公共领域
  • CBC: in this mode, each block of the original message is XORed with the previous ciphered block before being encrypted.

    CBC :在此模式下,原始消息的每个块在加密之前都会与先前的加密块进行异或。

WhiteTimberwolf (SVG version) / Public domain
WhiteTimberwolf(SVG版本)/公共领域
  • CFB / OFB / CTR: these modes make a block cipher into a stream cipher, by using the message block only after the encryption part.

    CFB / OFB / CTR :这些模式仅在加密部分之后才使用消息块,从而将块密码转换为流密码。

  • XTS: used for encoding random accessible data (like a hard disk or RAM).

    XTS :用于编码随机可访问的数据(例如硬盘或RAM)。

消息认证 (Message authentication)

To increase the security of the message, a short piece of information is added to authenticate it. In other words, it confirms the message really came from the sender and has not been modified.

为了提高消息的安全性,添加了一小段信息以对其进行身份验证。 换句话说,它确认邮件确实来自发件人并且尚未被修改。

This piece of information is called Message Authentication Code (MAC) and protects the message’s data integrity as well as its authenticity. There are many algorithms that implement it, such as HMAC, GCM, etc.

此信息称为消息验证码(MAC) ,可保护消息的数据完整性及其真实性。 实现它的算法很多,例如HMAC , GCM等。

加密 (Encryption)

The process of encrypting a file using Go is simple. Using the package crypto we can do all the necessary steps to encrypt a file.

使用Go加密文件的过程很简单。 使用包crypto我们可以执行所有必要的步骤来加密文件。

The first step is to open the file and read its content.

第一步是打开文件并读取其内容。

The next step is to create the block for our algorithm. This object implements the actual code so we don’t have to worry about it. In this tutorial, we are going to use AES.

下一步是为我们的算法创建块。 该对象实现了实际的代码,因此我们不必担心。 在本教程中,我们将使用AES。

The key must be 16 bytes (AES-128), 24 bytes (AES-192), or 32 bytes (AES-256), which we read from a file.

密钥必须是16字节(AES-128),24字节(AES-192)或32字节(AES-256),这些是我们从文件中读取的。

Note: Never save your key on your code.

注意 : 切勿将密钥保存在代码中。

As mentioned above, there are some modes you can encrypt your message. For our convenience, the package crypto/cipher already implements some of them for us.

如上所述,您可以使用某些模式来加密邮件。 为了我们的方便,软件包crypto/cipher已经为我们实现了其中的一些。

We are going to use the GCM mode, which is a stream mode with authentication. So we don’t have to worry about the padding or doing the authentication, since it is already done by the package.

我们将使用GCM模式,这是带有身份验证的流模式。 因此,我们不必担心填充或进行身份验证,因为它已经由程序包完成了。

This mode requires a nonce array. It works like an IV. Make sure this is never the same value, that is, change it every time you will encrypt, even if it is the same file. You can do this with a random value, using the package crypto/rand.

此模式需要一个随机数数组。 它像IV一样工作。 确保此值永远不会相同,也就是说,即使您是同一文件,也要在每次加密时都对其进行更改。 您可以使用crypto/rand软件包使用随机值来执行此操作。

To encrypt the data, we use the function Seal. It will encrypt the file using the GCM mode, appending the nonce and tag (MAC value) to the final data, so we can use it to decrypt it later.

为了加密数据,我们使用功能Seal 。 它将使用GCM模式加密文件,将noncetag (MAC值)附加到最终数据中,因此我们以后可以使用它解密它。

After this, we just need to save the ciphertext into our destination file.

之后,我们只需要将密文保存到目标文件中即可。

That’s it! You’ve encrypted a file. And the complete code:

而已! 您已加密文件。 以及完整的代码:

解密方式 (Decryption)

To decrypt the file, the process is basically the same. We have now to read the encrypted file instead.

解密文件的过程基本相同。 现在,我们必须读取加密的文件。

Creating the key and the mode block is the same as in the encryption process. Make sure you use the same key as you used to encrypt since this is a symmetric-key algorithm.

创建密钥和模式块与加密过程中的相同。 由于这是对称密钥算法,因此请确保使用与加密相同的密钥。

To decrypt our file, we use the Open function. It is necessary to indicate the nonce value we used in the encryption process. In our encryption process, this value is saved at the beginning of the file.

要解密文件,我们使用“ Open功能。 有必要指出我们在加密过程中使用的nonce值。 在我们的加密过程中,此值保存在文件的开头。

Finally, we just have to save our decrypt content into a file.

最后,我们只需要将解密内容保存到文件中即可。

加密大文件 (Encrypting large files)

Although this is a simple method to encrypt files, it can be a bad option for large files. All the content is read into memory before it is encrypted. To avoid any problem, we have to read and encrypt by chunks.

尽管这是一种加密文件的简单方法,但是对于大文件而言,这可能是一个糟糕的选择。 加密之前,所有内容都会读入内存。 为了避免任何问题,我们必须按块读取和加密。

To do this, we first open the file we are interested in encrypting using the os.Open function.

为此,我们首先使用os.Open函数打开要加密的文件。

The key and block creation is the same as the previous example, so I am not going to repeat here.

密钥和块的创建与前面的示例相同,因此在此不再重复。

We can use the CTR mode since is a stream mode and very close to the GCM. This mode also needs an IV, the same size as the block (for AES, 16 bytes).

我们可以使用CTR模式,因为它是流模式并且非常接近GCM。 此模式还需要一个IV,其大小与块相同(对于AES,为16字节)。

Before entering the actual reading loop, we have to open the destination file to be able to write into it.

在进入实际的读取循环之前,我们必须打开目标文件才能进行写入。

After that, we read the file by chunks. In this tutorial, we choose a buffer of 1024 bytes, but you can choose any size you prefer.

之后,我们按块读取文件。 在本教程中,我们选择一个1024字节的缓冲区,但是您可以选择自己喜欢的任何大小。

For each block read from the input file, we perform the encryption process using the function XORKeyStream.

对于从输入文件读取的每个块,我们使用功能XORKeyStream进行加密过程。

After that, we save the IV that we used into the destination file so we can use it to decrypt. The complete code:

之后,我们将使用的IV保存到目标文件中,以便我们可以使用它进行解密。 完整的代码:

解密大文件 (Decrypting large files)

To decrypt it, we have to change only a thing: read the IV from the file instead of the random value. This value is saved on the final of the file, so it’s easy to read.

要解密它,我们只需要改变一点:从文件中读取IV,而不是随机值。 此值保存在文件的末尾,因此很容易阅读。

In the CTR mode, the encryption and decryption processes are the same, so we actually don’t need to change anything.

在CTR模式下,加密和解密过程相同,因此我们实际上不需要更改任何内容。

The complete code for the decryption part:

解密部分的完整代码:

It is important to note that we did not use any form of authentication. This can be done easily using the crypto/hmac package. After the MAC value is calculated, you can append it to the destination file. To the decryption process, you have to read it and compare it with the new calculation you perform.

重要的是要注意,我们没有使用任何形式的身份验证。 使用crypto/hmac包可以很容易地做到这一点。 计算MAC值后,可以将其附加到目标文件。 在解密过程中,您必须阅读它并将其与执行的新计算进行比较。

结论 (Conclusion)

Encrypting a file using the Go language is not difficult using the crypto package.

使用crypto软件包使用Go语言加密文件并不困难。

If you are also interesting in how to create a hash using Go, check out this other article:

如果您对如何使用Go创建哈希也很感兴趣,请查看另一篇文章:

Follow me on Twitter if you want to receive more articles about programming.

如果您想获得更多有关编程的文章,请在Twitter上关注我。

翻译自: https://levelup.gitconnected.com/a-short-guide-to-encryption-using-go-da97c928259f

golang 加密文件


http://www.taodudu.cc/news/show-2922599.html

相关文章:

  • python暴力破解zip加密文件
  • 免费/中文/功能强大的Modbus调试软件:MThings
  • modbus调试工具 linux,Modbus TCP的模拟器的Windows/Linux的
  • 在虚拟机(centos)配置postgresql数据库(1) - 安装篇
  • 利用指针访问opencv Mat类型的矩阵,以及求椭圆方程的函数
  • nodejs入门04__包的创建和发布
  • CVPR读书笔记[5]:Gabor特征提取之Gabor核的实现
  • php fpm failed,ubuntu环境下启动php-fpm失败Job for php-fpm.service failed...
  • 关于JAVA字符编码:Unicode,ISO-8859-1,GBK,UTF-8编码及相互转换
  • HTTP常用的响应码说明(网页/服务器显示200、302、404、500是什么意思,表示什么)
  • 网页提示404什么意思
  • 403服务器显示什么意思,404、503、403、301状态码是什么意思?
  • 404未找到是什么意思_404错误是什么意思?为什么是404
  • eclipse导入Tomcat出现404错误
  • 什么是404页面?
  • Python爬虫响应码为404错误
  • HTTP 404错误你知道是什么意思吗
  • mysql远程服务器返回错误404_服务器常见页面访问返回错误信息(404 500 400)表示什么意思...
  • 搜网页显示未连接上服务器是什么,【科普君】网页搜索时,出现“404”到底是什么意思?...
  • JavaWeb——404错误
  • HTTP 404 错误 的具体意思
  • 404未找到是什么意思_http404未找到怎么解决,404 未找到常见问题汇总
  • 网页404是不是服务器没开,无法打开的网页出现404错误 知道什么意思吗?
  • html网页启动不了404错误,造成网页 404 错误的几大原因介绍
  • java中404什么意思_java web中关于404问题的根本来源与解决
  • php 404 not found,404 not found nginx是什么意思
  • 404未找到是什么意思_常见的web错误404你知道是什么意思吗?
  • 404究竟是什么意思呢?像404,200,503等数字究竟是什么东西
  • 关于github双因素验证问题解决方案
  • 对数字身份认证安全,是企业的责任还是个人的责任?

golang 加密文件_如何使用Go加密文件相关推荐

  1. java dom xml 换行,dom4j解析xml文件_用DOM解析XML文件,怎么才能让解析出来的文本不用换行_dom解析xml文件...

    网友求助:dom4j解析xml文件_用DOM解析XML文件,怎么才能让解析出来的文本不用换行_dom解析xml文件 问题importjava.text.SimpleDateFormat; import ...

  2. python怎样打开加密的文件_如何用Python 加密文件

    生活中,有时候我们需要对一些重要的文件进行加密,Python 提供了诸如 hashlib,base64 等便于使用的加密库. 但对于日常学习而言,我们可以借助异或操作,实现一个简单的文件加密程序,从而 ...

  3. python怎样打开加密的文件_如何使用python加密多个文件

    我正在尝试搜索指定文件夹中的.txt文件,并对使用我的加密算法找到的每个.txt文件进行加密.不过,我似乎无法能够弄清楚如何将所有的文件夹中找到的.txt文件加密并重新命名如何使用python加密多个 ...

  4. mysql base64 加密解密_烂泥:base64加密与解密

    本文由ilanniweb微信公众号提供友情赞助,首发于烂泥行天下 jenkins技术分享QQ群:571981257 一.什么是base64 base64是网络上最常见的用于传输8Bit字节码的编码方式 ...

  5. ibe加密原理_解析基于身份加密IBE

    基于身份加密(IBE)是一种公共密钥加密方法,在这种加密方法中第三方服务器使用简单的识别符:邮件地址.社会保险号等来生成用于加密和解密电子信息的公共密钥.与传统的公共密钥加密方法相比较,这种加密方法为 ...

  6. php rsa加密乱码_如何解决php加密 乱码问题

    php加密乱码的解决解决办法:首先对密文进行MD5加密:然后通过base64加密来避免乱码,代码语句为"$bs_test = base64_encode($test); ". 本教 ...

  7. keystore文件_如何手动给APK文件签名

    由于您的应用签名密钥用于验证您作为开发者的身份,并确保为您的用户进行无缝而安全的更新,因此,管理和保护您的密钥对于您和您的用户而言都非常重要. 您可以选择使用 Google Play 的 App Si ...

  8. android pak文件_游戏中的Pak文件解析

    Pak 文件的结构 pak 文件就是将多个文件打包为一个单独文件,在这个文件中保存着多个文件的数据, 当然还有一些描述文件结构的数据.所以将 "Pak" 作为文件的后缀是一种常规的 ...

  9. 可疑文件_【国家标准】印刷文件鉴定技术规范点阵式打印文件的同机鉴定

    印刷文件鉴定技术规范(GB/T 37232-2018) 总则 印刷文件同机(同版)鉴定的受理程序.送检材料的标识.检验鉴定程序.送检材料的流转程序及结果报告程序应按GB/T 37234-2018第4章 ...

最新文章

  1. RabbitMQ 学习
  2. ssm配置socket_ssm框架中集成websocket实现服务端主动向客户端发送消息
  3. wxWidgets:构建wxWidgets程序的第一步
  4. 前端学习(586):在元素中动态添加类与伪类
  5. linux 控制终端卡,配置通过串口控制linux操作系统的终端
  6. oracle19c连接MySQL_oracle19c的安装和使用navicat连接oracle数据库
  7. nginx离线安装_做一个属于自己的离线下载服务器原来这么简单
  8. 数据结构与算法学习笔记02-单向链表
  9. Mac 使用命令行工具解压和压缩 rar 文件
  10. 阶段1 语言基础+高级_1-3-Java语言高级_08-JDK8新特性_第3节 两种获取Stream流的方式_8_Stream流中的常用方法_skip...
  11. linux虚拟内存和win,linux下的vm(虚拟内存)和windows下的作用是一样的,均是防止真实内存资源不足准备的. linux的vm相关参数...
  12. jQuery文档就绪事件
  13. 怎样绘制漂亮的统计图表|不一样的折线图
  14. adb工具的安装方法
  15. 排序——使每位学生都有座位的最少移动次数
  16. 谈谈最近管理情绪和时间的心得:真的是破心中贼难
  17. unity 输入框弹出输入法_国产输入法那么多,我为什么选择了「不接地气」的 Gboard?...
  18. 【R语言】使用nnet过程中报错Error in eval(predvars, data, env) : object ‘naulong‘ not found
  19. ddd 访问权限_Lind.DDD.Authorization用户授权介绍
  20. 启明星辰2018年营收达25亿元 同比增长10.58%

热门文章

  1. 【Android】Vibrator的使用
  2. HACKTHEBOX——Nibbles
  3. 微信小程序—点击实现页面跳转
  4. php网页弹出输入对话框,php和js实现弹出对话框实例分享
  5. java execute、executeQuery和executeUpdate之间的区别
  6. GIF图片如何用Photoshop去水印?
  7. oj2448: 分离正整数中的各位数
  8. 【华为OD机试 2023】 最多获得的短信条数/云短信平台优惠活动(C++ Java JavaScript Python)
  9. Unity 之 关于停止协程的五种方式解析
  10. php new object delete,php – S3 DeleteObject – DeleteMarker始终返回空