base64解码字符串_如何使用Mac OS或Linux编码和解码base64字符串

base64解码字符串

One pretty common task that I need to do during with my day-to-day software engineering is to either encode or decode base64 strings; it comes up when I’m working with things like private keys or Kubernetes secrets, which are base64 encoded. To do so, you can easily memorize one command for each that you can run in your terminal.

在日常软件工程中,我需要完成的一项非常常见的任务是编码或解码base64字符串。 当我使用base64编码的私钥或Kubernetes secrets之类的东西时,它就会出现。 为此,您可以轻松地为每个可在终端中运行的命令记住一个命令。

First of all, I would be remiss if I didn’t mention that you don’t necessarily have to do this in your command line interface. There are two sibling websites, Base64 Decode and Base64 Encode, that achieve this task. I’m not affiliated with them in any way, but I’m quite certain that many, many developers are familiar with them.

首先,如果我没有提到您不一定必须在命令行界面中执行此操作,我将不为所动。 有两个同级网站Base64 DecodeBase64 Encode可以完成此任务。 我与他们没有任何关系,但我可以肯定,很多开发人员都对它们很熟悉。

However, there are various reasons why you may not use those sites; for one, perhaps you’re offline and can’t access them. Second, perhaps you don’t fully trust your secret data going to the server of someone you don’t know. Both are really valid points, and that’s why I suggest the methods I’m about to go over.

但是,出于多种原因,您可能不使用这些网站; 一方面,您可能处于离线状态,无法访问它们。 其次,也许您不完全信任将您的秘密数据发送给您不认识的人的服务器。 两者都是很有效的观点,这就是为什么我建议要使用的方法。

在base64中编码 (Encode in base64)

First, to encode in base64, run the following command in your terminal:

首先,要在base64中进行编码,请在终端中运行以下命令:

echo -n 'username' | base64

Let’s go over the different components to this.

让我们来看一下不同的组件。

  • echo: This command writes a string to standard output, which in less technical terms means that it literally prints whatever you write onto the screen again.

    echo :此命令将字符串写入标准输出,这用较少的技术术语表示它将从字面上再次打印您在屏幕上写入的内容。

  • -n: This flag indicates that you want the output to print on a new line. This is actually pretty useful because otherwise, the base64 encoded string will print right afterward with no space or anything. It’s just harder to copy and paste, and you could even accidentally copy some of the initial command with the string, which would certainly cause headaches down the line. I recommend you get in the habit of including this flag even though it’s not necessary to make the command actually work.

    -n :此标志指示您希望输出在新行上打印。 这实际上非常有用,因为否则,base64编码的字符串将立即打印,没有空格或任何内容。 复制和粘贴更加困难,甚至可能会意外地使用字符串复制一些初始命令,这肯定会引起麻烦。 我建议您养成包括此标志的习惯,即使不必使该命令真正起作用也是如此。

  • 'username': This is where you put the string you want to base64 encode; it doesn’t literally have to be username.

    'username' :这是您要对base64进行编码的字符串的位置; 它实际上不必是username

  • |: This is a pipe which tells the command that you want to redirect the output of the left-hand part into the right-hand part, essentially. In this case, we don’t simply want to just echo what we wrote; we want to encode it! Thus, we’ll provide on the right side the instructions to encode the string.

    | :这是一个管道,它告诉命令您实际上要将左手部分的输出重定向到右手部分。 在这种情况下,我们不只是想回声我们写的东西; 我们要编码! 因此,我们将在右侧提供指令以对字符串进行编码。

  • base64: This is a program that is in the coreutils (or GNU Core Utilities) package that comes pre-packaged with your Mac or Linux OS. Other familiar tools to you that are also in coreutils include ls, rm, and cat. Basically, this will do all the hard work of encoding the string you provided earlier.

    base64 :这是一个程序,位于Mac或Linux OS随附的coreutils (或GNU Core实用程序)软件包中。 您还可以在coreutils其他熟悉的工具,包括lsrmcat 。 基本上,这将完成您之前提供的字符串编码的所有艰苦工作。

The output of the above command is as follows:

上面命令的输出如下:

dXNlcm5hbWU=

在base64中解码 (Decode in base64)

Let’s say that you actually started with the base64 encoded string above, dXNlcm5hbWU=. If you want to decode it, the process is actually as easy as it was in the above section.

假设您实际上是从上面的base64编码字符串dXNlcm5hbWU= 。 如果要对其进行解码,则该过程实际上与上一节中的操作一样简单。

Simply run the following command:

只需运行以下命令:

base64 --decode 

This should be pretty easy to understand; the --decode flag does exactly what you think it does! The output is:

这应该很容易理解。 --decode标志完全按照您的想法执行! 输出为:

username

容易记忆但对生活有用 (Easy to memorize but useful for life)

If you work with software, I don’t think that the above subheading is much of an exaggeration; I use these commands all the time, and I’m so grateful for them.

如果您使用软件,则我认为上面的小标题并不太夸张; 我一直在使用这些命令,我​​非常感谢他们。

翻译自: https://medium.com/swlh/how-to-encode-and-decode-base64-strings-with-mac-os-or-linux-778274dcf967

base64解码字符串

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值