0

我想在 iphone 中加密一条消息。我有模数和指数。我参考的代码

如何从公钥的指数和模数创建 SeckeyRef 并在 SecKeyEncrypt 方法中使用

+(NSString *)encryptRSA:(NSString *)plainTextString key:(NSString *)key
{

     NSString *publicKeyIdentifier = [NSString stringWithFormat:@"%@.publickey",[[NSBundle   mainBundle] bundleIdentifier]];
     [Crypto setPublicKey:key tag:(NSString *)publicKeyIdentifier];

     SecKeyRef publicKey = NULL;
     NSData * publicTag = [self PublicKeyItems/*publicKeyIdentifier      dataUsingEncoding:NSUTF8StringEncoding*/];
     NSMutableDictionary *queryPublicKey = [[[NSMutableDictionary alloc] init] autorelease];
     [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)kSecReturnRef];

     SecItemCopyMatching((CFDictionaryRef)queryPublicKey, (CFTypeRef *)&publicKey);

     if (!publicKey)
     {
         if(publicKey) CFRelease(publicKey);

         [Exception raise:FAILURE function:__PRETTY_FUNCTION__ line:__LINE__ description:@"Could not decrypt."];

}

我也同时使用 BasicEncodingRules.m 和 .h。但我有一个警告

+ (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 *testData1 = [testData berDecode];
    NSLog(@"testarray = %@",testData1);


    STAssertEqualObjects(testData, testData1, @"Big items decode failed");
// warning at this line, with content "Implicit declaration of function STAssertEqualObjects is invalid in C99"

    NSData *testData2 = [testData1 berData];
    NSLog(@"PublicKeyData using Publickeyitems = %@",testData2);
    return testData2;
}

所以我不能建立 Crypto.o

请帮我!不管怎么说,还是要谢谢你

4

1 回答 1

0

STAssertEqualObjects 用于SenTest(即xcode 附带的单元测试框架)。此代码是否在单元测试中?如果不是,那是你的问题。试试NSAssert*

于 2012-09-13T10:18:58.857 回答