让我们明确一点:此代码的主体不是由我编写的。一切都是从以下 URL 上找到的:
http://blog.iamzsx.me/show.html?id=155002
遗憾的是,我看到了这段代码在 StackOverflow 和博客文章中被分发行。这是 POS。我只是负责将其放入合适的仓库并以 pod 的形式分发。请随意提交错误和发送拉取请求,我将确保处理它们。
如果您想要一个好的教程说明如何使用它,您可以在英文中找到它:
http://jslim.net/blog/2013/01/05/rsa-encryption-in-ios-and-decrypt-it-using-php/
以下是在那里找到的复制粘贴内容:
在 iOS 中进行 RSA 加密
1) 使用 SSL 生成密钥对。
openssl req -x509 -out public_key.der -outform der -new -newkey rsa:1024 -keyout private_key.pem -days 3650
有几个要点需要说明
2) 现在,将 public_key.der 拖到您的 iOS 上
3) 从您的公钥创建一个 XRSA 实例
3.1) 使用与应用程序捆绑的公钥文件
NSString *keyPath = [[NSBundle mainBundle] pathForResource:@"public_key" ofType:@"der"];
XRSA *rsa = [[XRSA alloc] initWithPublicKey:keyPath];
3.2) 使用内存中的密钥(更安全,因为资源可以从 .ipa 文件中轻松恢复)
NSString *publicKey = @"MIICs ... kT0=\n"; // Base64 encoded key
NSData *data = [[NSData alloc] initWithBase64EncodedString:publicKey options:NSDataBase64DecodingIgnoreUnknownCharacters];
XRSA *rsa = [[XRSA alloc] initWithData:data];
4) 加密数据
if (rsa != nil) {
NSLog(@"%@", [rsa encryptToString:@"This is plaintext"]);
} else {
NSLog(@"Error");
}