从本地ios应用程序调用javascript库

时间:2017-07-05 08:58:26

标签: javascript ios uiwebview rsa

我必须使用这个库进行RSA加密,到目前为止,我真的找不到任何类似的iOS应用程序库。有没有我可以使用的图书馆?

https://github.com/wwwtyro/cryptico/

因此,我将从本机代码调用javascript方法并像这样写。

NSString *jsPath = [[NSBundle mainBundle] pathForResource:@"cryptico" ofType:@"js"];

NSString *scriptString = [NSString stringWithContentsOfFile:jsPath encoding:NSUTF8StringEncoding error:nil];

JSContext *context = [[JSContext alloc] init];
context = [[JSContext alloc] init];
[context evaluateScript:scriptString];
JSValue *function = context[@"generateRSAKey"];

JSValue* result = [function callWithArguments:@[@"Hello", @1024]]; //pass the string whatever you want.
[result toString]; // This will give the value in string format.

这也不行。我尝试将其放在UIWebview中并调用。

NSString *jsFile = @"cryptico.js";
NSString *jsFilePath = [[NSBundle mainBundle] pathForResource:jsFile ofType:nil];
NSURL *jsURL = [NSURL fileURLWithPath:jsFilePath];
NSString *javascriptCode = [NSString stringWithContentsOfFile:jsURL.path encoding:NSUTF8StringEncoding error:nil];
[webview stringByEvaluatingJavaScriptFromString:javascriptCode];

NSString *functionCall = [NSString stringWithFormat:@"generateRSAKey(%@,%@)", @"Hello", @1024];
NSString *resultStr = [webview stringByEvaluatingJavaScriptFromString:functionCall];


NSLog(@"resultStr = %@", resultStr);

该文件中的javascript方法就是这个。我需要调用此方法并传递参数。

// Generates an RSA key from a passphrase.
my.generateRSAKey = function(passphrase, bitlength)
{
    Math.seedrandom(sha256.hex(passphrase));
    var rsa = new RSAKey();
    rsa.generate(bitlength, "03");
    return rsa;
}

但是,它仍然没有给我任何东西。我该怎么办?

修改

我也尝试这样调用它并且它会给出" undefined"。

JSValue *test = context[[NSString stringWithFormat:@"cryptico.generateRSAKey(%@,%@)", @"Hello", @1024]];

我需要了解它们产生的是什么/如何产生。

来自Swift Library的公钥(https://github.com/soyersoyer/SwCrypt

  

----- BEGIN PUBLIC KEY ----- MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCYPmwHqTcZf9vaeI2ENAsjbjNO   nLzJJyhIzs9xZKXZpE + t4gKYbJgt + PJO + EGDHFGbfC3FcYK / qlfVRDvDGHTAWZfI   kudzz + XXiQSlktod9CzHXaeDz5RhTPTaWEIkAUIRiKyoFddeaAOsuZhXxzv6sTah   VFgSIbDnx0gM6wuBRwIDAQAB   ----- END PUBLIC KEY -----

来自离子库的公钥

  

uXjrkGqe5WuS7zsTg6Z9DuS8cXLFz38ue + xrFzxrcQJCXtVccCoUFP2qH / AQ   4qMvxxvqkSYBpRm1R5a4 / NdQ5ei8sE8gfZEq7dlcR + gOSv3nnS4 / CX1n5Z5m   8bvFPF0lSZnYQ23xlyjXTaNacmV0IuZbqWd4j9LfdAKq5dvDaoE =

0 个答案:

没有答案