Home  >  Article  >  Backend Development  >  Detailed explanation of using base64 function to transcode and encrypt files

Detailed explanation of using base64 function to transcode and encrypt files

零下一度
零下一度Original
2018-05-19 10:13:434342browse

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 = &#39;upload/iampdf.pdf&#39;;
$file_encoded = &#39;encoded/iampdf.pdf&#39;;
//获取文件数据
$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 = &#39;upload/iampdf.pdf&#39;;
$file_encoded = &#39;encoded/iampdf.pdf&#39;;
//读取文件数据
$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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn