dongmeng1868 2017-08-10 01:47
浏览 55

将VBA加密功能转换为PHP

My first question here, so hopefully this all goes well.

I have an application that is currently running on desktop under MS Access using VBA code. When the user logs in I have a function to encrypt the password. This is:

Public Function EncryptEasy(strIn As String) As String

Dim strChr As String
Dim i As Integer
Dim Salt As Long
Salt = 543214321

For i = 1 To Len(strIn)
  strChr = strChr & CStr(Asc( Mid(strIn, i, 1) ) Xor Salt)
Next I

EncryptEasy = strChr

End Function

To give you an idea, when I run EncryptEasy("test") in VBA, it returns: 543214213543214228543214210543214213

I am now setting up a simple web-app using PHP and was hoping to utilise the same encryption as I'm currently using by coding this function into PHP. I HAVE tried and below is my attempt:

function EncryptEasy($strIn) {

$salt = 543214321;
$strChr = Null;
$strLen = strlen($strIn);

  for ($i = 0; $i <= $strLen; $i++) {
      $strChr = $strChr . chr(ord(substr($strIn, $i, 1)) xor $salt);
  }

  return $strChr;

}

However, this is returning blank. I have tried echoing this:

<?php
echo EncryptEasy("test");
?>

to no avail.

Is anyone able to see where I am going wrong?

  • 写回答

1条回答 默认 最新

  • doushi4795 2017-08-10 02:43
    关注

    I would not suggest creating your own algorithm for encrypting, instead use built-in functions provided by php.

    Although this is not encrypting the password like the one in your original code, using password_hash is easier than creating your own, and to verify use password_verify()

    Hashing Password:

    <?php
        $hashed = password_hash("somePassword", PASSWORD_DEFAULT);
    ?>
    

    Verifying Password

    <?php
        if(password_verify('somePassword', $hashed))
        {
            echo 'Same'; //goes here
        } else {
            echo 'Not same';
        }
        if(password_verify('somePasswordsss', $hashed))
        {
            echo 'Same'; 
        } else {
            echo 'Not same'; //goes here
        }
    ?>
    
    评论

报告相同问题?

悬赏问题

  • ¥15 矩阵加法的规则是两个矩阵中对应位置的数的绝对值进行加和
  • ¥15 活动选择题。最多可以参加几个项目?
  • ¥15 飞机曲面部件如机翼,壁板等具体的孔位模型
  • ¥15 vs2019中数据导出问题
  • ¥20 云服务Linux系统TCP-MSS值修改?
  • ¥20 关于#单片机#的问题:项目:使用模拟iic与ov2640通讯环境:F407问题:读取的ID号总是0xff,自己调了调发现在读从机数据时,SDA线上并未有信号变化(语言-c语言)
  • ¥20 怎么在stm32门禁成品上增加查询记录功能
  • ¥15 Source insight编写代码后使用CCS5.2版本import之后,代码跳到注释行里面
  • ¥50 NT4.0系统 STOP:0X0000007B
  • ¥15 想问一下stata17中这段代码哪里有问题呀