dongzhanlu0658 2015-12-07 14:42
浏览 241

使用PHP中的模数和指数进行RSA加密

I want to encrypt password using modulus and exponent. I'm using .NET RSACryptoServiceProvider and in PHP I'm using seclib. I have this piece of C# code and I want to do the exact same thing in PHP.

        string exponent = "010001";
        string modulus = "A1DFE8C5D4140E46610A4B2B0E0B77F7048A9386F50D5CF0B02C983C0138392F60C5B20C67285A37FABD2E270CB32FDBCB0DB1902EC74A0ADF41C623B3CF250EC4287BF9C3A0477BAEFD5803C07401C3E013BABEE8EE528EF765EB8EA4A82034DF7B0AC2BA9CDA6B0D6B87999D2AF0CA851C4FD6D62EF538EF73F1183658E0D810132DE2F7814C73A14E31B6472027A682EFD938EA6CC7C6E48DFD7F5145A870595E4B9A63909AF3AABD75C5148E276D1D329531CB2F27E45E0F09B076B08D56A310DE5BD2BF1EFD04D9A88BF1A1C0B74C60E04CE2EAB96E9B5BE7F6C42CA72E9D2970031550EB515C4F7EB5C245B3EF141E67864C536D54D3DA10CED3DF63F1";
        string password = "testing";

        byte[] encryptedPasswordBytes;
        using (var rsaEncryptor = new RSACryptoServiceProvider())
        {
            var passwordBytes = Encoding.ASCII.GetBytes(password);
            var rsaParameters = rsaEncryptor.ExportParameters(false);
            rsaParameters.Exponent = HexStringToByteArray(exponent);
            rsaParameters.Modulus = HexStringToByteArray(modulus);
            rsaEncryptor.ImportParameters(rsaParameters);
            encryptedPasswordBytes = rsaEncryptor.Encrypt(passwordBytes, false);
        }

        string encryptedPassword = Convert.ToBase64String(encryptedPasswordBytes);
        Console.WriteLine(WebUtility.UrlEncode(encryptedPassword));
        Console.ReadLine();

HexStringToByteArray method source code:

        public static byte[] HexStringToByteArray(string hex)
        {
            int hexLen = hex.Length;
            byte[] ret = new byte[hexLen / 2];
            for (int i = 0; i < hexLen; i += 2)
                ret[i / 2] = Convert.ToByte(hex.Substring(i, 2), 16);
            return ret;
        }

I'm trying to get same results in PHP using seclib and I think I'm not successfull.

$exponent = '010001';
$modulus = 'A1DFE8C5D4140E46610A4B2B0E0B77F7048A9386F50D5CF0B02C983C0138392F60C5B20C67285A37FABD2E270CB32FDBCB0DB1902EC74A0ADF41C623B3CF250EC4287BF9C3A0477BAEFD5803C07401C3E013BABEE8EE528EF765EB8EA4A82034DF7B0AC2BA9CDA6B0D6B87999D2AF0CA851C4FD6D62EF538EF73F1183658E0D810132DE2F7814C73A14E31B6472027A682EFD938EA6CC7C6E48DFD7F5145A870595E4B9A63909AF3AABD75C5148E276D1D329531CB2F27E45E0F09B076B08D56A310DE5BD2BF1EFD04D9A88BF1A1C0B74C60E04CE2EAB96E9B5BE7F6C42CA72E9D2970031550EB515C4F7EB5C245B3EF141E67864C536D54D3DA10CED3DF63F1';
$password = 'testing';

$rsa = new Crypt_RSA();
$rsa->loadKey(
   array(
       'e' => new Math_BigInteger($exponent,16),
       'n' => new Math_BigInteger($modulus,16)
   )
);
$encryptedPassword = urlencode(base64_encode($rsa->encrypt($password)));
  • 写回答

1条回答 默认 最新

  • doutao4938 2016-09-06 03:29
    关注
    $exponent = '010001';
    $modulus = 'A1DFE8C5D4140E46610A4B2B0E0B77F7048A9386F50D5CF0B02C983C0138392F60C5B20C67285A37FABD2E270CB32FDBCB0DB1902EC74A0ADF41C623B3CF250EC4287BF9C3A0477BAEFD5803C07401C3E013BABEE8EE528EF765EB8EA4A82034DF7B0AC2BA9CDA6B0D6B87999D2AF0CA851C4FD6D62EF538EF73F1183658E0D810132DE2F7814C73A14E31B6472027A682EFD938EA6CC7C6E48DFD7F5145A870595E4B9A63909AF3AABD75C5148E276D1D329531CB2F27E45E0F09B076B08D56A310DE5BD2BF1EFD04D9A88BF1A1C0B74C60E04CE2EAB96E9B5BE7F6C42CA72E9D2970031550EB515C4F7EB5C245B3EF141E67864C536D54D3DA10CED3DF63F1';
    $password = 'testing';
    $rsa = new Crypt_RSA();
    $modulus = new Math_BigInteger(($modulus), 16);
    $exponent = new Math_BigInteger(($exponent), 16);
    $rsa->loadKey(array('n' => $modulus, 'e' => $exponent));
    $rsa->setPublicKey(array('n' => $modulus, 'e' => $exponent));
    $rsa->setEncryptionMode(CRYPT_RSA_ENCRYPTION_PKCS1);
    $encryptedPassword = $rsa->encrypt($password);
    
    评论

报告相同问题?

悬赏问题

  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错
  • ¥15 单片机学习顺序问题!!
  • ¥15 ikuai客户端多拨vpn,重启总是有个别重拨不上
  • ¥20 关于#anlogic#sdram#的问题,如何解决?(关键词-performance)
  • ¥15 相敏解调 matlab
  • ¥15 求lingo代码和思路
  • ¥15 公交车和无人机协同运输
  • ¥15 stm32代码移植没反应
  • ¥15 matlab基于pde算法图像修复,为什么只能对示例图像有效