This encoding is designed so that binary data can be transmitted through a non-pure 8-bit transport layer. For example, the content of an email is transmitted through base64 transcoding. After Base64-encoding, the data takes up about 33% more space than the original data.
Use base64 to encrypt files:
<?php //写文件路径 $file_url = 'upload/iampdf.pdf'; $file_encoded = 'encoded/iampdf.pdf'; //获取文件数据 $data = file_get_contents($file_url); //转码加密 $data_encode = base64_encode($data); //保存加密后的文件 file_put_contents($data_encode,$file_encoded ); ?>
Decrypt base64 encrypted files:
<?php $file_url = 'upload/iampdf.pdf'; $file_encoded = 'encoded/iampdf.pdf'; //读取文件数据 $data_encode = file_get_contents($file_encoded); //解密 $data = base64_decode($data); //保存解密后的文件 file_put_contents($data,$file_url); ?>
In fact, the above operation is not encryption. As long as you know how to decode, you can obtain the original file in minutes. document. Therefore, those who are interested can perform string displacement, conversion and other operations on the transcoded file, so that true encryption can be achieved.
The above is the detailed content of Detailed explanation of using base64 function to transcode and encrypt files. For more information, please follow other related articles on the PHP Chinese website!