到目前为止,我已经为此花了两天时间,并梳理了我可以使用的所有资源,所以这是最后的手段。
我有一个 X509 证书,其公钥存储在 iPhone 的钥匙串中(此时仅模拟器)。在 ASP.NET 方面,我在证书存储中使用私钥获得了证书。当我在 iPhone 上加密一个字符串并在服务器上解密它时,我得到一个CryptographicException
“坏数据”。我尝试了页面中的Array.Reverse
建议RSACryptoServiceProvider
,但没有帮助。
我比较了两边的base-64字符串,它们是相等的。我在解码后比较了原始字节数组,它们也是相等的。如果我在服务器上使用公钥加密,字节数组与 iPhone 的版本不同,并且很容易使用私钥解密。原始明文字符串为 115 个字符,因此它在我的 2048 位密钥的 256 字节限制之内。
这是 iPhone 加密方法(从CryptoExercise 示例应用程序的方法中几乎一字不差wrapSymmetricKey
):
+ (NSData *)encrypt:(NSString *)plainText usingKey:(SecKeyRef)key error:(NSError **)err
{
size_t cipherBufferSize = SecKeyGetBlockSize(key);
uint8_t *cipherBuffer = NULL;
cipherBuffer = malloc(cipherBufferSize * sizeof(uint8_t));
memset((void *)cipherBuffer, 0x0, cipherBufferSize);
NSData *plainTextBytes = [plainText dataUsingEncoding:NSUTF8StringEncoding];
OSStatus status = SecKeyEncrypt(key, kSecPaddingNone,
(const uint8_t *)[plainTextBytes bytes],
[plainTextBytes length], cipherBuffer,
&cipherBufferSize);
if (status == noErr)
{
NSData *encryptedBytes = [[[NSData alloc]
initWithBytes:(const void *)cipherBuffer
length:cipherBufferSize] autorelease];
if (cipherBuffer)
{
free(cipherBuffer);
}
NSLog(@"Encrypted text (%d bytes): %@",
[encryptedBytes length], [encryptedBytes description]);
return encryptedBytes;
}
else
{
*err = [NSError errorWithDomain:@"errorDomain" code:status userInfo:nil];
NSLog(@"encrypt:usingKey: Error: %d", status);
return nil;
}
}
这是服务器端 C# 解密方法:
private string Decrypt(string cipherText)
{
if (clientCert == null)
{
// Get certificate
var store = new X509Store(StoreName.My, StoreLocation.LocalMachine);
store.Open(OpenFlags.ReadOnly);
foreach (var certificate in store.Certificates)
{
if (certificate.GetNameInfo(X509NameType.SimpleName, false) == CERT)
{
clientCert = certificate;
break;
}
}
}
using (var rsa = (RSACryptoServiceProvider)clientCert.PrivateKey)
{
try
{
var encryptedBytes = Convert.FromBase64String(cipherText);
var decryptedBytes = rsa.Decrypt(encryptedBytes, false);
var plaintext = Encoding.UTF8.GetString(decryptedBytes);
return plaintext;
}
catch (CryptographicException e)
{
throw(new ApplicationException("Unable to decrypt payload.", e));
}
}
}
我怀疑平台之间存在一些编码问题。我知道一个是大端,另一个是小端,但我不知道哪个是哪个或如何克服差异。Mac OS X、Windows 和 iPhone 都是 little-endian,所以这不是问题。
新理论:如果将 OAEP 填充布尔值设置为 false,则默认为 PKCS#1 1.5 填充。显然,SecKey
只有、、和SecPadding
的定义。也许 Microsoft 的 PKCS#1 1.5 != Apple 的 PKCS1 等填充会影响加密的二进制输出。我尝试与set to一起使用,但仍然无法正常工作。PKCS1
PKCS1MD2
PKCS1MD5
PKCS1SHA1
kSecPaddingPKCS1
fOAEP
false
kSecPaddingPKCS1
相当于PKCS #1 1.5。回到理论的绘图板......
其他新尝试的理论:
- iPhone 上的证书(.cer 文件)与服务器上的 PKCS#12 包(.pfx 文件)并不完全相同,因此它永远无法工作。在不同的证书存储中安装了 .cer 文件,并且服务器加密的字符串往返就好了;
- 转换为 base-64 和 POST 到服务器的行为导致了同一个类往返中不存在的奇怪,所以我首先尝试了一些 URLEncoding/Decoding,然后从 iPhone 发布原始二进制文件,验证它是相等的,并得到相同的坏数据;
- 我的原始字符串是 125 字节,所以我认为它可能会在 UTF-8(长镜头)中被截断,所以我将其裁剪为 44 字节的字符串而没有结果;
- 回顾 System.Cryptography 库以确保我使用了适当的类并发现了“RSAPKCS1KeyExchangeDeformatter”,对新的前景感到高兴,当它的行为完全相同时感到沮丧。
成功!
事实证明,我在 iPhone 模拟器上的钥匙串中有一些东西弄脏了水,可以这么说。我删除了 Keychain DB 以~/Library/Application Support/iPhone Simulator/User/Library/Keychains/keychain-2-debug.db
使其重新创建并且工作正常。感谢您的所有帮助。数字这将是一些简单但不明显的事情。(我学到了两件事:1)从模拟器中卸载应用程序不会清除其钥匙串条目,2)定期重新启动。)
注意:钥匙串文件的通用路径取决于 iOS 版本:~/Library/Application Support/iPhone Simulator/[version]/Library/Keychains/keychain-2-debug.db 例如,~/Library/Application Support/ iPhone模拟器/4.3/Library/Keychains/keychain-2-debug.db