Base64专题---PHP实现base64加密和解密的方法

algorain

Base加密方法很方便,在保存一些特别数据时可以直接调用方法进行加密和解密的操作,我会发一篇专门介绍base64原理的文章,有兴趣的可以先去查看。还有php和python的使用代码我都会贴在文章最后。

下面是使用php进行base64加密与解密的源码,使用时建议将代码写到一个函数里,方便使用的时候自己调用即可,记住值的传递与接收

加密

1
2
3
4
5
6
7
8
9
10
11
12
13
14
base64_encode
string base64_encode ( string $data )
使用 base64 对 data 进行编码。
参数data要编码的数据。
返回值
编码后的字符串数据, 或者在失败时返回 FALSE。

示例
<?php
$str = 'This is an encoded string';
echo base64_encode($str);
?>
以上会输出:
VGhpcyBpcyBhbiBlbmNvZGVkIHN0cmluZw==

解密

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
base64_decode
string **base64_decode** ( string `$data` [, bool `$strict` = false ] )
对 base64 编码的 `data` 进行解码。
参数data编码过的数据。
如果输入的数据超出了 base64 字母表,则返回 `FALSE`。
返回值
返回原始数据, 或者在失败时返回 **`FALSE`**。返回的数据可能是二进制的。

示例
`<?php
$str = 'VGhpcyBpcyBhbiBlbmNvZGVkIHN0cmluZw==';
echo base64_decode($str);
?>`
以上会输出:
<pre style="white-space: pre-wrap; margin: 0px;">This is an encoded string</pre>
附带我的关于base64的两篇文章

》》》Python实现Base64加密和解密 》》》java中使用Base64加密解密的函数

  • Title: Base64专题---PHP实现base64加密和解密的方法
  • Author: algorain
  • Created at: 2017-02-04 01:10:34
  • Updated at: 2023-05-14 21:39:50
  • Link: http://www.rain1024.com/2017/02/04/php-article0017/
  • License: This work is licensed under CC BY-NC-SA 4.0.
 Comments
On this page
Base64专题---PHP实现base64加密和解密的方法