对QString进行base64 加密和解密
时间: 2023-11-16 20:45:02 浏览: 61
您可以使用以下方法对QString进行Base64加密和解密:
加密:
```cpp
QString QStringToBase64(const QString& str)
{
QByteArray byteArray = str.toUtf8();
return byteArray.toBase64();
}
```
解密:
```cpp
QString Base64ToQString(const QString& str)
{
QByteArray byteArray = QByteArray::fromBase64(str.toUtf8());
return QString::fromUtf8(byteArray);
}
```
您可以使用`QStringToBase64`函数将QString转换为Base64编码字符串,使用`Base64ToQString`函数将Base64编码字符串转换为QString。
示例用法:
```cpp
QString originalString = "Hello, World!";
QString encodedString = QStringToBase64(originalString);
QString decodedString = Base64ToQString(encodedString);
qDebug() << "Original: " << originalString;
qDebug() << "Encoded: " << encodedString;
qDebug() << "Decoded: " << decodedString;
```
运行上述示例,您会得到以下输出:
```
Original: "Hello, World!"
Encoded: "SGVsbG8sIFdvcmxkIQ=="
Decoded: "Hello, World!"
```
请注意,您需要在源文件中包含`<QByteArray>`和`<QDebug>`头文件。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![cpp](https://img-home.csdnimg.cn/images/20210720083646.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)