我想使用模数和指数加密密码。我使用的是.NET RSACryptoServiceProvider,而在PHP中我使用的是seclib。 我有这段C#代码,我想在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方法源代码:
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;
}
我尝试使用seclib在PHP中获得相同的结果,我认为我没有成功。
$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)));
答案 0 :(得分:0)
$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);