dpp80346 2012-06-24 08:50
浏览 19
已采纳

Php - 读取文件,更改数据,写入新文件

I'm trying to create a kind of file generator in Php. Basically, it get a default file, read data, decrypt the data (Encrypted in AES), encrypt the data with the user password and paste all these bytes into a new file with the client name.

This sould be easy but I got some problems. Here is my current code:

// Load the original raw file
$data = file_get_contents('default.bin');
if (!$data) exit();

// Decrypt the raw data with the default key
$data = AESDecrypt($data, 'default_16_b_key');

// Encrypt the raw data with the user key
$data = AESEncrypt(pad($data, 16), $user_key);

$fileName = sprintf('client_%s.bin', $client_name);
file_put_contents($fileName, $data);

Here are the functions I'm using:

function AESEncrypt($data, $key)
{
    return mcrypt_encrypt(MCRYPT_RIJNDAEL_128, $key, $data, MCRYPT_MODE_ECB);
}

function AESDecrypt($data, $key)
{
    return mcrypt_decrypt(MCRYPT_RIJNDAEL_128, $key, $data, MCRYPT_MODE_ECB);
}

function pad($data, $blocksize)
{ 
    $pad = $blocksize - (strlen($data) % $blocksize); 
    return $data . str_repeat(chr($pad), $pad); 
}

The AES functions work well as I can decrypt and encrypt a simple string. The pad function seems to do its job to.

The problem is when I try to execute the generated file (which is a jar file after being renamed), I got this message "Unexpected file end" and nothing into my jar file except a blank file (0 byte).

Could you help me to solve this?

  • 写回答

3条回答 默认 最新

  • doubiao9775 2012-06-28 07:48
    关注

    Finally, I solved it without using the first decryption. It's possible to encrypt the raw data using the following code:

    $data = AESEncrypt(pad($data, 16), $user_key);
    

    You can put that line in an infinite loop (no really infinite btw) and this will still work. Then you can decrypt it in the language of your choice.

    Thanks.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 微信会员卡接入微信支付商户号收款
  • ¥15 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?