私のサーバーはlibsodiumを使用してデータを暗号化してからAPIを介して私と共有するので、クライアント側ではSwift Sodiumを使用しています。迅速なナトリウム秘密鍵を使用した復号化
今私は私と一緒にString形式の既存の秘密鍵と公開鍵を持っています。 Swiftを使用してiOS上の暗号化されたデータを解読したいと考えています。
私の持つ公開鍵と秘密鍵を使用してSodium Keyペアを生成するにはどうすればよいですか?
また、理想的には私は秘密鍵だけを使ってデータを復号化するべきです。では、私はStringとしてプライベートキーだけを使用してそれをどうやって行うのですか?
復号化のための私のコードを以下に示します - 上記のコードでは
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 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'));
}
あなたの 'dataString'はどのような形式ですか?多分base64や他の形式なので、解読する前に解読する必要がありますか? – algrid
@algrid暗号化された文字列です。その上にエンコーディングがないので、デコーディングは必要ありません。 –