2014-10-19 36 views
0

我正在关注RSA上的Apple示例代码。我有一切工作,现在我试图用动态分配的字符串(从我的textview)替换他们使用的字符串文字。
苹果的代码是:数组初始化器目标C

const uint8_t dataToEncrypt[] = "the quick brown fox jumps " 
         "over the lazy dog\0"; 

我需要能够动态地设置这一点,但我不能确定如何做到这一点,因为当我尝试,我得到了以下错误:“数组初始化函数必须是一个初始化列表或字符串文字”

static const UInt8 publicKeyIdentifier[] = "com.apple.sample.publickey\0"; 
static const UInt8 privateKeyIdentifier[] = "com.apple.sample.privatekey\0"; 
- (IBAction)encrypt:(id)sender { 
NSString *privateKey = [[NSUserDefaults standardUserDefaults] 
         stringForKey:@"privateKey"]; 



OSStatus status = noErr; 

size_t cipherBufferSize; 
uint8_t *cipherBuffer;      // 1 

// [cipherBufferSize] 
/////////////////// 
NSString *textToEncode = [NSString stringWithFormat:@"%@%@", self.textView.text, @"\0"]; 
////////////////////// 
const uint8_t dataToEncrypt[] = textToEncode;//This throws the error 
//"the quick brown fox jumps over the lazy dog\0"; // 2 


size_t dataLength = sizeof(dataToEncrypt)/sizeof(dataToEncrypt[0]); 

SecKeyRef publicKey = NULL;         // 3 

NSData * publicTag = [NSData dataWithBytes:publicKeyIdentifier 
            length:strlen((const char *)publicKeyIdentifier)]; // 4 

NSMutableDictionary *queryPublicKey = 
[[NSMutableDictionary alloc] init]; // 5 

[queryPublicKey setObject:(__bridge id)kSecClassKey forKey:(__bridge id)kSecClass]; 
[queryPublicKey setObject:publicTag forKey:(__bridge id)kSecAttrApplicationTag]; 
[queryPublicKey setObject:(__bridge id)kSecAttrKeyTypeRSA forKey:(__bridge id)kSecAttrKeyType]; 
[queryPublicKey setObject:[NSNumber numberWithBool:YES] forKey:(__bridge id)kSecReturnRef]; 
// 6 

status = SecItemCopyMatching 
((__bridge CFDictionaryRef)queryPublicKey, (CFTypeRef *)&publicKey); // 7 

// Allocate a buffer 

cipherBufferSize = SecKeyGetBlockSize(publicKey); 
cipherBuffer = malloc(cipherBufferSize); 

// Error handling 

if (cipherBufferSize < sizeof(dataToEncrypt)) { 
    // Ordinarily, you would split the data up into blocks 
    // equal to cipherBufferSize, with the last block being 
    // shorter. For simplicity, this example assumes that 
    // the data is short enough to fit. 
    printf("Could not decrypt. Packet too large.\n"); 
} 

// Encrypt using the public. 
status = SecKeyEncrypt( publicKey, 
         kSecPaddingPKCS1, 
         dataToEncrypt, 
         (size_t) dataLength, 
         cipherBuffer, 
         &cipherBufferSize 
         );        // 8 

// Error handling 
// Store or transmit the encrypted text 

if (publicKey) CFRelease(publicKey); 

//NSData *encryptedData = [NSData dataWithBytes:cipherBuffer length:dataLength]; 
NSData *encryptedData = [NSData dataWithBytes:cipherBuffer length:cipherBufferSize]; 

free(cipherBuffer); 
self.textView.text = [NSString stringWithFormat:@"%@",encryptedData]; 

} 

编辑:

替换:

const uint8_t dataToEncrypt[] 

With:

NSData *someData = [self.textView.text dataUsingEncoding:NSUTF8StringEncoding]; 
    const void *bytes = [someData bytes]; 
    const uint8_t *dataToEncrypt = (uint8_t*)bytes; 
+1

向我们显示您的代码。 – NMK 2014-10-19 03:03:11

+0

请参阅'NSString'上的'UTF8String'属性 – Paulw11 2014-10-19 03:11:27

+0

不要自己进行加密,安全性很强,而小小的错误可能会破坏安全性。搜索GitHub for RNCryptor并使用它,它为Apple的加密系统提供了简单的Objective-C包装。 – 2014-10-19 05:56:29

回答

1

为什么不使用NSString来初始化输入字符串?

NSString *testStr = "This is a test input for RSA"; 
const char *cStr = [testStr UTF8String]; 

然后使用cStr的任何算法你想使用。

0

你可以做的是添加一个指向加密数据的指针。

const uint8_t *pointerToData; 
pointerToData = dataToEncrypt; 

//This dynamically set the dataToEncrypt to the textField's text value 
uint8_t *textFieldStr = (uint8_t *)textFiled.text.UTF8String; 
pointerToData = textFieldStr; 
+0

这不是一个很好的答案,但是我确实将数组改成了一个指针并且工作正常。 – cph2117 2014-10-19 05:38:21