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)));