ios - RNCryptor:获取公钥作为 NSString
<p><p>我正在使用 RNCryptor 在 iOS 中成功加密/解密数据。</p>
<p>我正在尝试将公钥发送到服务器,以便它可以加密一些数据。</p>
<pre><code>NSString *saltString = @"salt'n'peppa";
NSData *salt = ;
NSData *key = [RNCryptor keyForPassword:password
salt:salt
settings:kRNCryptorAES256Settings.keySettings];
</code></pre>
<p>此时,<code>key</code>里面有一些数据。但是,我似乎无法弄清楚如何将公钥作为字符串获取:</p>
<pre><code>NSString *publicKey = [ initWithData:key encoding:NSUTF8StringEncoding];
</code></pre>
<p>我尝试了不同的编码,但似乎没有任何效果。</p>
<p>这是来自 RNCryptor 的 keyForPassword 方法:</p>
<pre><code>+ (NSData *)keyForPassword:(NSString *)password salt:(NSData *)salt settings:(RNCryptorKeyDerivationSettings)keySettings
{
NSMutableData *derivedKey = ;
// See Issue #77. V2 incorrectly calculated key for multi-byte characters.
NSData *passwordData;
if (keySettings.hasV2Password) {
passwordData = length:];
}
else {
passwordData = ;
}
// Use the built-in PBKDF2 if it's available. Otherwise, we have our own. Hello crazy function pointer.
int result;
int (*PBKDF)(CCPBKDFAlgorithm algorithm, const char *password, size_t passwordLen,
const uint8_t *salt, size_t saltLen,
CCPseudoRandomAlgorithm prf, uint rounds,
uint8_t *derivedKey, size_t derivedKeyLen);
PBKDF = CCKeyDerivationPBKDF ?: RN_CCKeyDerivationPBKDF;
result = PBKDF(keySettings.PBKDFAlgorithm, // algorithm
passwordData.bytes, // password
passwordData.length, // passwordLength
salt.bytes, // salt
salt.length, // saltLen
keySettings.PRF, // PRF
keySettings.rounds, // rounds
derivedKey.mutableBytes, // derivedKey
derivedKey.length); // derivedKeyLen
// Do not log password here
NSAssert(result == kCCSuccess, @"Unable to create AES key for password: %d", result);
return derivedKey;
}
</code></pre>
<p>我觉得我做错了什么,因为谷歌搜索的结果很少。 </p></p>
<br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
<p><p>键不是字符串,而是数据。只是一个随机(某种)系列的字节。将其转换为字符串以发送到服务器的唯一真正方法是对字节进行编码。一种常见的方法是使用 base 64 编码。然后服务器可以将 base 64 编码的字符串转换回 key 的原始字节。</p></p>
<p style="font-size: 20px;">关于ios - RNCryptor:获取公钥作为 NSString,我们在Stack Overflow上找到一个类似的问题:
<a href="https://stackoverflow.com/questions/26153768/" rel="noreferrer noopener nofollow" style="color: red;">
https://stackoverflow.com/questions/26153768/
</a>
</p>
页:
[1]