php - swift 钠 使用私钥解密

标签 php ios swift libsodium sodium

我在客户端使用 Swift Sodium,因为我的服务器在通过 API 与我共享数据之前使用 libsodium 来加密数据。

现在我有一个现有的私钥和一个字符串格式的公钥。我现在想在 iOS 上使用 Swift 解密加密数据。

如何使用我拥有的公钥和私钥生成钠 key 对?

理想情况下,我应该仅使用私钥来解密数据。那么我该如何仅使用私钥作为字符串来做到这一点。

我的解密代码如下所示 -

func decryptData(dataString: String) -> String? {
    let sodium = Sodium()
    let privateKey = sodium?.utils.hex2bin("MY_SECRET_KEY")

    let publicKey = sodium?.utils.hex2bin("MY_PUBLIC_KEY")

    let message = dataString.data(using: .utf8)!

    if let decrypted = sodium?.box.open(anonymousCipherText: message, recipientPublicKey: publicKey!, recipientSecretKey: privateKey!){
        // authenticator is valid, decrypted contains the original message
        return String(data: decrypted, encoding: String.Encoding.utf8) as String!
    }
    return nil
}

在上面的代码中,我解密的字符串始终为空。

服务器正在使用以下函数加密数据 -

protected function crypt($response)
{
   $message = new HiddenString($response);

   $repository = \App::make(EncryptionKeysRepository::class);
   $enc = $repository->findOneBy(['device' => 'android']);
   $dir = $enc->getDevice();
   $publicKey = $enc->getPublicKey();
   $storage = storage_path();

   if(!file_exists($storage."/{$dir}/")) {
       mkdir($storage."/{$dir}/");
   }

   // save key pair to key store
   $pubFilename = \tempnam($storage."/{$dir}/", 'pub_key');
   file_put_contents($pubFilename, $publicKey);

   $public = KeyFactory::loadEncryptionPublicKey($pubFilename);
   unlink($pubFilename);
   rmdir($storage."/{$dir}/");
   $message = Crypto::seal($message, $public);

   return $message;
}

在服务器上解密逻辑

protected function deCrypt($response)
{
   $repository = \App::make(EncryptionKeysRepository::class);
   $enc = $repository->findOneBy(['device' => 'android']);
   $dir = $enc->getDevice();
   $publicKey = $enc->getSecretKey();
   $storage = storage_path();

   if(!file_exists($storage."/{$dir}/")) {
       mkdir($storage."/{$dir}/");
   }

   // save key pair to key store
   $secFilename = \tempnam($storage."/{$dir}/", 'sec_key');
   file_put_contents($secFilename, $publicKey);

   $secret = KeyFactory::loadEncryptionSecretKey($secFilename);
   unlink($secFilename);
   rmdir($storage."/{$dir}/");
   $res = Crypto::unseal($response, $secret);

   $message = $res->getString();

   return response()->json(compact('message'));
}

最佳答案

所以,在服务器端,您显然正在使用 PHP,以及一个名为 Halite 的库。

我对 Halite 不是很熟悉,但是看它的代码,Crypto::seal() 使用密封的盒子,所以 Box::open(anonymousCipherText: Data,recipientPublicKey: PublicKey,recipientSecretKey: SecretKey) -> Data? 是在 Swift 中解密的正确方法。

但是,Halite 似乎也将结果编码为 BASE64 字符串(urlsafe 变体)。

因此,您需要先对其进行解码。或者,不要对密文进行编码,这会更有效。除非您必须大声朗读它们,否则按键可能也不需要编码。

关于php - swift 钠 使用私钥解密,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46302597/

相关文章:

swift - 导入单个常量

ios - Swift didSet 已调用但未更新 UILabel - iOS 属性观察器

PHP MySQL 选择查询

php - 如何在 PHP 中比较两个日期?

ios - 如何仅为某些 View Controller 自定义 UINavigationBar

c++ - Objective-c iOS 中的二维码生成器

arrays - 如何在 Swift 中查询一个 UITableViewController 中的两个自定义单元格?

php - 使用 php sleep() 函数是一个好主意,可以通过繁重的脚本降低 CPU 负载吗?

PHP Reforce 搜索引擎

ios - UIAlertView + TextField = 错误?