【发布时间】:2010-11-11 03:33:20
【问题描述】:
到目前为止,我已经为此花费了两天时间,并梳理了我可以使用的所有来源,所以这是最后的手段。
我有一个 X509 证书,其公钥已存储在 iPhone 的钥匙串中(此时仅模拟器)。在 ASP.NET 方面,我在证书存储中使用私钥获得了证书。当我在 iPhone 上加密一个字符串并在服务器上解密它时,我得到一个CryptographicException“坏数据”。我尝试了很长一段时间内RSACryptoServiceProvider 页面中建议的Array.Reverse,但没有帮助。
我比较了两边的 base-64 字符串,它们是相等的。我在解码后比较了原始字节数组,它们也是相等的。如果我使用公钥在服务器上加密,字节数组与 iPhone 的版本不同,并且很容易使用私钥解密。原始明文字符串为 115 个字符,因此它在我的 2048 位密钥的 256 字节限制内。
这是 iPhone 的加密方法(与CryptoExercise sample app 的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 定义PKCS1、PKCS1MD2、PKCS1MD5 和PKCS1SHA1。也许 Microsoft 的 PKCS#1 1.5 != Apple 的 PKCS1 等填充会影响加密的二进制输出。我尝试将kSecPaddingPKCS1 与fOAEP 设置为false 一起使用,但它仍然不起作用。kSecPaddingPKCS1 是equivalent PKCS#1 1.5。回到理论的绘图板......
其他新尝试的理论:
- iPhone 上的证书(.cer 文件)与服务器上的 PKCS#12 包(.pfx 文件)不完全相同,因此它永远无法工作。在不同的证书存储中安装了 .cer 文件,并且服务器加密的字符串往返就好了;
- 转换为 base-64 和 POST 到服务器的行为导致了同一个类往返中不存在的奇怪,所以我首先尝试了一些 URLEncoding/Decoding,然后从 iPhone 发布原始二进制文件,验证它是相等的,然后得到相同的不良数据;
- 我的原始字符串是 125 字节,所以我认为它可能在 UTF-8 中被截断(长镜头),所以我将其裁剪为 44 字节的字符串,但没有结果;
- 回顾 System.Cryptography 库以确保我使用了适当的类并发现了 `RSAPKCS1KeyExchangeDeformatter`,对新的前景感到高兴,当它的行为完全相同时感到沮丧。
成功了!
事实证明,我在 iPhone 模拟器上的钥匙串中有一些东西弄脏了水,可以这么说。我在~/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 Simulator/4.3/Library/Keychains/keychain-2-debug.db
【问题讨论】:
标签: c# iphone rsa encryption