在Windows Mobile 6上使用RSACryptoServiceProvider和公钥串加密

本文关键字:RSACryptoServiceProvider 公钥 加密 Windows Mobile | 更新日期: 2023-09-27 18:16:16

我的Windows Mobile 6应用程序需要将数据发送到公司的php REST web服务。WS有一个方法,该方法返回用于加密移动应用程序用户名和密码的公钥。

他们给了我一个用php编写的示例代码,该代码简单地调用WS以获取公钥,然后调用openssl_public_encrypt,并将web服务调用返回的值作为公钥参数。

function CallAPI($url, $data = false)
{
    $curl = curl_init();
    $url = sprintf("%s?%s", $url, http_build_query($data)); 
    curl_setopt($curl, CURLOPT_URL, $url);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);      
    return curl_exec($curl);
}
$public_key=CallAPI(, "https://***.***.***/index.php/rest/getPKey");
$json = json_encode(array("username"=>"********","psw"=>"*******"));
openssl_public_encrypt($json, $encrypted, $public_key);

从Windows Mobile的角度来看似乎比这更复杂,而且我对RSA加密也不是很感兴趣。

我做的第一件事是调用WS获取公钥,并将其保存为字符串。下面是我用来加密数据的代码。

ASCIIEncoding ByteConverter = new ASCIIEncoding();
byte[] dataToEncrypt = ByteConverter.GetBytes(data_string);
byte[] public_key = ByteConverter.GetBytes(public_key_string);
byte[] encryptedData;
RSACryptoServiceProvider RSA_provider = new RSACryptoServiceProvider();
RSAParameters key_info = RSA_provider.ExportParameters(false);
key_info.Modulus = public_key;
RSA_provider.ImportParameters(key_info);
encryptedData = RSA_provider.Encrypt(dataToEncrypt, false);
string encrypted_string = ByteConverter.GetString(encryptedData, 0, encryptedData.Length);
return encrypted_string;

如果我尝试发送数据到web服务失败,由于身份验证失败,我也注意到,从php代码加密字符串总是256个字符,而。net加密字符串有完全不同的长度。

我做错了什么?我在StackOverflow上看到了很多关于使用。net和RSA加密的问题,但是所使用的功能并没有包含在紧凑框架中。

提前感谢。

在Windows Mobile 6上使用RSACryptoServiceProvider和公钥串加密

我已经解决了使用BouncyCastle API在。net端进行加密/解密,并使用UrlEncode在Windows Mobile客户端和PHP服务器之间发送数据,并将UrlEncoded字符串中的所有'+'替换为客户端和服务器已知的自定义字符串,我决定为"####"。

相关文章:
  • 没有找到相关文章