gpt4 book ai didi

ios - RNCryptor:获取公钥作为 NSString

转载 作者:塔克拉玛干 更新时间:2023-11-02 09:42:09 26 4
gpt4 key购买 nike

我在 iOS 中使用 RNCryptor 成功加密/解密数据。

我正在尝试获取要发送到服务器的公钥,以便它可以加密一些数据。

NSString *saltString = @"salt'n'peppa";
NSData *salt = [saltString dataUsingEncoding:NSUTF8StringEncoding];
NSData *key = [RNCryptor keyForPassword:password
salt:salt
settings:kRNCryptorAES256Settings.keySettings];

此时,key 中有一些数据。但是,我似乎无法弄清楚如何将公钥作为字符串获取:

NSString *publicKey = [[NSString alloc] initWithData:key encoding:NSUTF8StringEncoding];

我尝试了不同的编码,但似乎没有任何效果。

这是 RNCryptor 的 keyForPassword 方法:

+ (NSData *)keyForPassword:(NSString *)password salt:(NSData *)salt settings:(RNCryptorKeyDerivationSettings)keySettings
{
NSMutableData *derivedKey = [NSMutableData dataWithLength:keySettings.keySize];

// See Issue #77. V2 incorrectly calculated key for multi-byte characters.
NSData *passwordData;
if (keySettings.hasV2Password) {
passwordData = [NSData dataWithBytes:[password UTF8String] length:[password length]];
}
else {
passwordData = [password dataUsingEncoding:NSUTF8StringEncoding];
}

// 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;
}

我觉得自己做错了一些大事,因为谷歌搜索搜不到什么东西。

最佳答案

键不是字符串,而是数据。只是一个随机(某种)系列的字节。将其转换为字符串以发送到服务器的唯一真正方法是对字节进行编码。一种常见的方法是使用 base 64 编码。然后服务器可以将 base 64 编码字符串转换回 key 的原始字节。

关于ios - RNCryptor:获取公钥作为 NSString,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26153768/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com