【问题标题】:How to use openssl to output encrypted/decrypted message to stdout如何使用 openssl 将加密/解密消息输出到标准输出
【发布时间】:2019-10-10 09:19:00
【问题描述】:

我只是想用 shell 加密/解密密码(非交互式 - 它用于自动脚本)。我正在关注这个例子: https://superuser.com/a/20552/362669

我尝试将其转换为这个,以便它不使用文件 output.bin 来存储加密文本:

#!/usr/bin/env bash

cd `dirname "$BASH_SOURCE"`

# generate a 2048-bit RSA key and store it in key.txt
openssl genrsa -out key.txt 2048

# encrypt "hello world" using the RSA key in key.txt
encrypted="$(echo "hello world" | openssl rsautl -inkey key.txt -encrypt)"

echo "encrypted: $encrypted"

# decrypt the message and output to stdout
decrypted="$(echo "$encrypted" | openssl rsautl -inkey key.txt -decrypt)"

echo "decrypted: $decrypted";

但我得到的只是这个 garbully-guk:

Generating RSA private key, 2048 bit long modulus
........................................................................................................................+++
............................+++
e is 65537 (0x10001)
��◆J��┌ܥײ��R▒��%⎽F��    1l�}�%��?�0���+��%���C�8|_/!�A"Ꜵ:�������.��W2Pras��1���� ��(�a
��]�[�남␍◆�=┬─�з≤�ɦ�;�└�1MFP��^␋�@D�    �T_⎺F�Eπ�2��U2Ÿ┌π��N│�� ⎽��_\2�� 8V��%��(�^���␍4�#�π���*^D ���/�└�
RSA ⎺⎻␊⎼▒├␋⎺┼ ␊⎼⎼⎺⎼
4662363756:␊⎼⎼⎺⎼:04FFF06B:⎼⎽▒ ⎼⎺┤├␋┼␊⎽:CRYPTO_␋┼├␊⎼┼▒┌:␉┌⎺␌┐ ├≤⎻␊ ␋⎽ ┼⎺├ 02:/B┤␋┌␍R⎺⎺├/L␋␉⎼▒⎼≤/C▒␌␤␊⎽/␌⎺└.▒⎻⎻┌␊.│␉⎽/S⎺┤⎼␌␊⎽/┌␋␉⎼␊⎽⎽┌/┌␋␉⎼␊⎽⎽┌-22.260.1/┌␋␉⎼␊⎽⎽┌-2.6/␌⎼≤⎻├⎺/⎼⎽▒/⎼⎽▒_⎻┐1.␌:185:
4662363756:␊⎼⎼⎺⎼:04FFF072:⎼⎽▒ ⎼⎺┤├␋┼␊⎽:CRYPTO_␋┼├␊⎼┼▒┌:⎻▒␍␍␋┼± ␌␤␊␌┐ °▒␋┌␊␍:/B┤␋┌␍R⎺⎺├/L␋␉⎼▒⎼≤/C▒␌␤␊⎽/␌⎺└.▒⎻⎻┌␊.│␉⎽/S⎺┤⎼␌␊⎽/┌␋␉⎼␊⎽⎽┌/┌␋␉⎼␊⎽⎽┌-22.260.1/┌␋␉⎼␊⎽⎽┌-2.6/␌⎼≤⎻├⎺/⎼⎽▒/⎼⎽▒_␊▒≤.␌:580:
␍␊␌⎼≤⎻├␊␍: 
▒┌␊│⎽-└▒␌:␋┼├␊⎼⎺⎽ ▒┌␊│$ 

我的 shell 会话基本上是一团糟。

有人知道那是什么吗?可能是输出了shell无法处理的字符?

更新:如果我不记录加密值,那么我会得到这个:

Generating RSA private key, 2048 bit long modulus
........................+++
..........+++
e is 65537 (0x10001)
RSA operation error
4558829164:error:04FFF06B:rsa routines:CRYPTO_internal:block type is not 02:/BuildRoot/Library/Caches/com.apple.xbs/Sources/libressl/libressl-22.260.1/libressl-2.6/crypto/rsa/rsa_pk1.c:185:
4558829164:error:04FFF072:rsa routines:CRYPTO_internal:padding check failed:/BuildRoot/Library/Caches/com.apple.xbs/Sources/libressl/libressl-22.260.1/libressl-2.6/crypto/rsa/rsa_eay.c:580:
decrypted: 

【问题讨论】:

    标签: bash shell encryption openssl


    【解决方案1】:

    我认为最好的办法是将二进制文件与 base64 进行转换。

    只需将输出通过“openssl base64”命令启用,“openssl base64 -d”命令进行解码。

    所以:

    encrypted="$(echo "hello world" | openssl rsautl -inkey key.txt -加密 | openssl base64)"

    decrypted="$(echo "$encrypted" | openssl base64 -d | openssl rsautl -inkey key.txt -decrypt)"

    【讨论】:

    • 感谢帮助,因为我需要将加密数据存储为环境变量
    猜你喜欢
    • 2013-10-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-03-22
    • 2020-04-30
    • 1970-01-01
    • 2012-12-04
    • 2013-08-29
    相关资源
    最近更新 更多