I want to encrypt a message using a armored key. I want to accomplish this using OpenPGP.js.
Asked
Active
Viewed 726 times
0
-
Info on ASCII Armor can be found in [RFC4880 section-6.2](http://tools.ietf.org/html/rfc4880#section-6.2) – zaph Jul 28 '14 at 16:41
2 Answers
0
I'm answering my own question. I figured this out a while ago and wanted to share, since I couldn't find anything similar.
Here is how encryption should look like:
+ (NSString *)encryptMessage:(NSString *)message
forKey:(NSString *)key {
NSString *result = nil;
UIWebView *webView = [[UIWebView alloc] init];
NSString *path = [[NSBundle mainBundle] pathForResource:@"openpgp" ofType:@"js"];
NSString *content = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];
NSString *resultOfPGPLibEval = [webView stringByEvaluatingJavaScriptFromString:content];
if ([resultOfPGPLibEval isEqualToString:@"true"]) {//library was loaded successfully
JSContext *context = [webView valueForKeyPath:@"documentView.webView.mainFrame.javaScriptContext"]; // Undocumented access
context[@"key"] = key;
context[@"message"] = message;
[context evaluateScript:@"var openpgp = window.openpgp; var publicKey = openpgp.key.readArmored(key);var pgpMessage = openpgp.encryptMessage(publicKey.keys, message);"];
JSValue *val2 = context[@"pgpMessage"];
result = val2.toString;
}
return result;
}
Notice that you must have the OpenPGP library in the bundle, in this example it is named 'openpgp.js'. Also the key in this example is armored, so keep that in mind.
I feel that while wasteful to create a WebView just for one encryption round, it is more secure since it will fall out of scope as soon as the result is returned with it it's context. Keep in mind that I'm not a security guy, so take that with a grain of salt.
I hope this helps someone.

Cezar
- 690
- 1
- 8
- 17
0
However ff you just want to encrypt without javascript bridge, you may want to give a try to ObjectivePGP library.

Marcin
- 3,694
- 5
- 32
- 52