如何在bash脚本中使用openssl随机密钥?

如何在bash脚本中使用openssl随机密钥?

下面是我的示例代码:

KEY_FILE=/tmp/key.txt
KEY=$(openssl rand 16)
echo $KEY > $KEY_FILE
ID=2345
source .mysqlCredentials.txt
MySqlCommand="mysql -h $IP -u $UserName -p$Password $DBName -e"
$MySqlCommand "INSERT INTO  some_table ( ID , ssl_key ) VALUES (\"$ID\", \"$KEY\") ;"

我得到的错误是:

+ mysql -h X.X.X.X -u root -ppassword database -e 'INSERT INTO  some_table ( ID , ssl_key ) VALUES ("2345", "ïëC;+¹"ë'\''J£èMÑ") ;'
ERROR 1064 (42000) at line 1: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'ë'J£èMÑ")' at line 1

在生成的密钥中,我得到单引号、反引号、双引号和空格...等等...

我怎样才能克服这个错误?

答案1

为什么不尝试对 rand 字符串进行 base64 编码?就像这样: openssl rand -base64 16 这更容易处理。

相关内容