ここには似たような質問と回答がたくさんあることは知っていますが、検索しても自分に合ったものを見つけることができません.
IOS (6) で使用したいサービスがあります。これは、私が制御できないサード パーティによって提供されます。
サービスで認証するには、RSA 公開鍵で暗号化された RSA 暗号化文字列としてユーザー資格情報を送信する必要があります。
次の形式の XML ファイルが提供されました。
<BitStrength>1024</BitStrength>
<RSAKeyValue>
<Modulus>xxxxxxxxxxxxxxxxxxxxx</Modulus>
<Exponent>xxxx</Exponent>
</RSAKeyValue>
文字列を暗号化するにはどうすればよいですか? 私は DOTNET のバックグラウンドを持っているため、これまで複雑さのほとんどはわかりませんでした。
私は次のような例を試しました: Objective C での RSA 実装 ですが、私が持っているものからオブジェクトを構築する方法はありません。証明書が必要なようです。
このツールを使用してPEMファイルに変換しようとしましたが、コードは証明書オブジェクトを構築しません。 https://superdry.apphb.com/tools/online-rsa-key-converter
助けてくれてありがとう。
****編集****これは、提供された例を使用して作成したメソッドの一部です。エラーなしで実行されますが、出力をデコードできません。
SStatus status = noErr;
size_t cipherBufferSize;
uint8_t *cipherBuffer;
// [cipherBufferSize]
size_t dataSize = [plainTextString lengthOfBytesUsingEncoding:NSUTF8StringEncoding];
const uint8_t* textData = [[plainTextString dataUsingEncoding:NSUTF8StringEncoding] bytes];
NSAssert(publicKey, @"The public key being referenced by tag must have been stored in the keychain before attempting to encrypt data using it!");
// Allocate a buffer
cipherBufferSize = SecKeyGetBlockSize(publicKey);
// plain text block size must be 11 less than cipher buffer size because of
// the PKSC1 padding used:
const size_t blockSizeMinusPadding = cipherBufferSize - 11;
cipherBuffer = malloc(cipherBufferSize);
NSMutableData* accumulatedEncryptedData = [NSMutableData dataWithCapacity:0];
for (int ii = 0; ii*blockSizeMinusPadding < dataSize; ii++) {
const uint8_t* dataToEncrypt = (textData+(ii*blockSizeMinusPadding));
const size_t subsize = (((ii+1)*blockSizeMinusPadding) > dataSize) ? blockSizeMinusPadding-(((ii+1)*blockSizeMinusPadding) - dataSize) : blockSizeMinusPadding;
// Encrypt using the public key.
status = SecKeyEncrypt(publicKey,
kSecPaddingOAEP,
dataToEncrypt,
subsize,
cipherBuffer,
&cipherBufferSize
);
[accumulatedEncryptedData appendBytes:cipherBuffer length:cipherBufferSize];
}
if (publicKey) CFRelease(publicKey);
free(cipherBuffer);
// 蓄積された暗号化データを返します。[accumulatedEncryptedData base64EncodedString] を返します。