ios - 加密一个 NSString
<p><p>我想加密一个 NSString 使其不可读。安全级别并不重要(换句话说,如果有人要解密文本,他们就不会窃取任何敏感信息。</p>
<pre><code>NSString *myTextToEncrypt = @"Hello World!";
;
// myTextToEncrypt is now something unreadable, like '2rwzdn1405'
</code></pre>
<p>那我应该可以解密这个字符串了</p>
<pre><code>; // myTextToEncrypt should now be @"Hello World!" again
</code></pre>
<p>我该怎么做?我读过一些关于 CommonCrypto 和 AES Encryption 的文章,但这对于我正在尝试做的事情来说似乎有点矫枉过正(我读过的加密方法都是针对密码或其他敏感数据)</p></p>
<br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
<p><p>最简单的一种是使用您自己的加密,例如</p>
<p><strong>Utils.h</strong></p>
<pre><code>@interface Utils : NSObject
+(NSString*)encyptString:(NSString*)str;
+(NSString*)decryptString:(NSString*)str;
@end
</code></pre>
<p><strong>Utils.m</strong></p>
<pre><code>#import "Utils.h"
int offset = 15;
@implementation Utils
+(NSString*)encyptString:(NSString*)str
{
NSMutableString *encrptedString = [ init];
for (int i = 0; i < str.length; i++) {
unichar character = ;
character += offset;
;
}
return encrptedString;
}
+(NSString*)decryptString:(NSString*)str
{
NSMutableString *decrptedString = [ init];
for (int i = 0; i < str.length; i++) {
unichar character = ;
character -= offset;
;
}
return decrptedString;
}
@end
</code></pre>
<p>使用方法</p>
<pre><code>NSString *str = @"hello world";
NSString *enr = ;
NSLog(@"Encrypted Text=%@", enr);
NSLog(@"Decrypted Text=%@", );
</code></pre>
<p>日志</p>
<pre><code>2013-08-11 10:44:09.409 DeviceTest Encrypted Text=wt{{~/~{s
2013-08-11 10:44:09.412 DeviceTest Decrypted Text=hello world
</code></pre></p>
<p style="font-size: 20px;">关于ios - 加密一个 NSString,我们在Stack Overflow上找到一个类似的问题:
<a href="https://stackoverflow.com/questions/18168874/" rel="noreferrer noopener nofollow" style="color: red;">
https://stackoverflow.com/questions/18168874/
</a>
</p>
页:
[1]