我正在iphone上开发一个应用程序。我有一个使用加密(RSA)的Java应用程序,我创建了一个私钥和公钥。我想在iphone上使用Java应用程序中的Public Key。对于Ex:我的公钥是byte [] publicKey = {0x01,0x02}; 如何使用我的publicKey加密iphone上的数据? 我看到了CryptoExercise,但我无法构建它(功能:SecKeyEncrypt错误:EXC_BAD_ACCESS)。我可以使用getPublicKeyBits()或getPublicKeyRef()吗?
这是我的代码:
* (NSData *)getPublicKeyBits {
OSStatus sanityCheck = noErr;
//
const char myByteArray[] = {
0x00, -0x79, 0x7C, 0x34,
0x5C, -0x36, 0x36, 0x75,
0x0E, 0x7F, -0x21, -0x05,
0x41, 0x21, 0x4F, -0x30,
0x2D, 0x5F, 0x08, -0x25,
0x07, -0x08, 0x22, -0x09,
0x32, -0x6C, 0x10, 0x1E, 0x5A,
-0x59, -0x14, -0x55, -0x73, 0x21,
0x5E, -0x54, -0x5E, -0x72, 0x37,
-0x31, -0x25, -0x45, 0x3B, 0x7D, -0x3C,
-0x6F, -0x40, -0x7E, 0x74, -0x68, -0x23,
0x42, 0x12, -0x62, -0x66, 0x4D, 0x20, -0x69,
0x28, -0x28, -0x36, -0x71, 0x21, 0x02, -0x32,
-0x19, 0x66, 0x7D, 0x3E, 0x03, 0x49, -0x66, 0x1F,
-0x38, 0x3C, 0x0A, 0x5F, 0x60, 0x1B, -0x75, 0x41, 0x48,
-0x5F, 0x1F, -0x34, -0x31, -0x09, 0x17, 0x23, 0x11, 0x1E,
-0x68, 0x0B, -0x4D, 0x69, -0x3F, -0x27, 0x13, -0x71, -0x6D,
-0x7A, 0x3A, 0x64, 0x2A, 0x6A, -0x6E, 0x3C, 0x04, -0x70, -0x1C};
NSData *publicKeyBits = NSData dataWithBytes: myByteArray length: sizeof(myByteArray);
//
//NSData * publicKeyBits = {1};
NSMutableDictionary * queryPublicKey = [NSMutableDictionary alloc] init;
// Set the public key query dictionary.
queryPublicKey setObject:(id)kSecClassKey forKey:(id)kSecClass;
queryPublicKey setObject:publicTag forKey:(id)kSecAttrApplicationTag;
queryPublicKey setObject:(id)kSecAttrKeyTypeRSA forKey:(id)kSecAttrKeyType;
[queryPublicKey setObject:NSNumber numberWithBool:YES] forKey:(id)kSecReturnData;
// Get the key bits.
sanityCheck = SecItemCopyMatching((CFDictionaryRef)queryPublicKey, (CFTypeRef *)&publicKeyBits);
if (sanityCheck != noErr)
{
printf("sanitycheck error@@@@@");
publicKeyBits = nil;
}
NSLog(@"** public key bits: %s", &publicKeyBits);
queryPublicKey release;
return publicKeyBits;
}
// encrypt message
* (void)encryptWithPublicKey:(uint8_t *)plainBuffer cipherBuffer:(uint8_t *)cipherBuffer
{
NSLog(@"== encryptWithPublicKey()");
NSData* publicKey= [AppController sharedWrapper] getPublicKeyBits;
OSStatus status = noErr;
NSLog(@"** original plain text 0: %s", plainBuffer);
size_t cipherBufferSize = 1;
uint8_t *pPlainText = (uint8_t*)"This is a test";
uint8_t *aCipherText;
size_t *iCipherLength = (size_t*)"1024";
// Error handling
// Encrypt using the public.
printf("begin ecrypt !!!!");
// status = SecKeyEncrypt(publicKey,
// kSecPaddingNone,
// plainBuffer,
// plainBufferSize,
// &cipherBuffer[0],
// &cipherBufferSize
// );
status = SecKeyEncrypt(publicKey,
kSecPaddingNone,
pPlainText,
strlen( (char*)pPlainText) + 1,
aCipherText,
iCipherLength);
printf("end encrypt !!!!");
NSLog(@"encryption result code: %d (size: %d)", status, cipherBufferSize);
NSLog(@"encrypted text: %s", cipherBuffer);
}
请帮帮我! 非常感谢你
答案 0 :(得分:3)
我不是100%跟随它,但我认为您的问题可能如下:
公钥/私钥对不用于加密数据。它们仅用于加密固定大小数据块,这些数据块很短 - 大小与公钥/私钥本身的大小相同。
因此,使用它们的方式是公钥/私钥用于加密[类似] AES密钥 - 它反过来与模式一起使用(如EBC等)加密流或不规则大小的数据块。
我认为您正在尝试使用公共/私有密钥对来加密您的用户数据,这可能不是公钥/私钥操作的正确大小 - 因此您的泄漏并获得BAD_ACCESS_ERROR。