2012-07-17 57 views
3

請人從指數和彈性模量是創建SecKeyRef幫我Public exponent = 和Public Modulus = 008903fb6d15f352ed3b45add3216f632f7139954a5631337aba7d645ed38482e3a810b4db26aab4d1df58c147230f0c75631a3dd0554b50de44e79f4fcf205c89fd3f80e0ff8d16c2e9f56ed3ab177953d54c9c30357d04e677cedd9912906ef8a046d7b0185b7f2022a8e435b0c6ecaef93f089fc3aa3f3677550b5d842046c7,我想用這個SecKeyEncrypt作爲公鑰如何從公共密鑰的指數和模量創建SeckeyRef和SecKeyEncrypt方法使用

+0

主任Purnima, 你能粘貼完整代碼。我看到代碼轉換指數&模數爲NSData,但我沒有轉換爲SecRefKey(這是首選公鑰)。提前致謝。 -Kiran – 2012-10-01 11:07:17

回答

0

解決了問題NSData *publicTag=[self PublicKeyItems];生成密鑰對和調用方法
嘗試導入BasicEncodingRules.h和.M在文件

- (NSData *)PublicKeyItems 
{ 
NSString *exp = @"010001"; 
NSLog(@"Publickeyexp -%@",exp); 
NSData *publickeyexpdata= [self stringasdata:exp]; 
NSLog(@"publickeyexpdata = %@",publickeyexpdata); 
NSString *mod = @"008903fb6d15f352ed3b45add3216f632f7139954a5631337aba7d645ed38482e3a810b4db26aab4d1df58c147230f0c75631a3dd0554b50de44e79f4fcf205c89fd3f80e0ff8d16c2e9f56ed3ab177953d54c9c30357d04e677cedd9912906ef8a046d7b0185b7f2022a8e435b0c6ecaef93f089fc3aa3f3677550b5d842046c7"; 
NSLog(@"Publickeymod -%@",mod); 
NSData *publickeymoddata= [self stringasdata:mod]; 
NSLog(@"publickeymod = %@",publickeyexpdata); 

NSMutableArray *publicarray = [[NSMutableArray alloc] init]; 
[publicarray addObject:publickeyexpdata]; 
[publicarray addObject:publickeymoddata]; 
NSData *testData = [publicarray berData]; 
NSLog(@"testdata = %@",testData); 
NSMutableArray *testArray2 = [testData berDecode]; 
NSLog(@"testarray = %@",testArray2); 
//STAssertEqualObjects(testArray, testArray2, 
        // @"Big items decode failed"); 

NSData *testData2 = [testArray2 berData]; 
NSLog(@"PublicKeyData using Publickeyitems = %@",testData2); 
return testData2; 
} 
-(NSData *)stringasdata:(NSString *)command { 
command = [command stringByReplacingOccurrencesOfString:@" " withString:@""]; 
command = [command stringByReplacingOccurrencesOfString:@"<" withString:@""]; 
command = [command stringByReplacingOccurrencesOfString:@">" withString:@""]; 
NSLog(@"command= %@",command); 
NSMutableData *commandToSend= [[NSMutableData alloc] init]; unsigned char whole_byte; 
int len = [command length]; 
int n = len/2; 
char byte_chars[3] = {'\0','\0','\0'}; int i; 
for (i=0; i < n; i++) { 
    byte_chars[0] = [command characterAtIndex:i*2]; byte_chars[1] = [command   characterAtIndex:i*2+1]; whole_byte = strtol(byte_chars, NULL, 16); [commandToSend appendBytes:&whole_byte length:1]; 
} 
// [commandToSend setLength:[commandToSend length]-1]; 
return commandToSend; 

} 
+0

我幾乎不是一個Objective-C嚮導,但是我沒有理解你在這段代碼中做什麼。你能解釋一下嗎? – 2012-07-17 21:02:10

+1

我想通過方法 – 012346 2012-07-18 05:33:25

+0

使用指數和模數來獲取公鑰。我不知道這是否重要,你可能應該添加模數,然後指數到數組。它對你有用嗎? – LightMan 2012-12-11 19:43:14