ARC4 Encryption through javascript in iOS

265 views Asked by At

I am not getting any result after writing this code.It returns only blank.

[self.webview loadHTMLString:@"<script src=\"http://crypto-js.googlecode.com/svn/tags/3.1.2/build/rollups/rc4.js\"></script>"
                baseURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] resourcePath]]];

NSString *function = [[NSString alloc] initWithFormat: @"CryptoJS.RC4.encrypt(%@, %@)",@"abhijit",@"TestKey"];
NSString *result = [self.webview stringByEvaluatingJavaScriptFromString:function];

NSLog(@"----%@",result);

Is there are some mistake in implementation on this or anything else?Please suggest..

Trying to implement this http://uttool.com/encryption/RC4/default.aspx in ios app

1

There are 1 answers

1
Tayschrenn On

UIWebView loads asynchronously. The contents of the page you're loading won't have immediately materialized after calling loadHTMLString:baseURL:. You need to evaluate the JavaScript code inside webViewDisFinishLoad: of the web view's delegate.

I would recommend using JavaScriptCore directly in this case, though. Simply create a JSContext, evaluate the contents of rc4.js in it and everything in it will be immediately available in the JSContext. Of course, thus works only if you're targeting iOS 7+.

Seeing as you need to do crypto however, I suggest (and I can't stress this enough) to use the CommonCrypto framework. It seems to support RC4 based on this snippet.