这个案例我是从code4app下载的。大家请看
// RSA.h
//
// Copyright (c) 2012 scott ban
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
#import
typedef void (^GenerateSuccessBlock)(void);
@interface RSA : NSObject{
@private
NSData * publicTag;
NSData * privateTag;
NSOperationQueue * cryptoQueue;
GenerateSuccessBlock success;
}
@property (nonatomic,readonly) SecKeyRef publicKeyRef;
@property (nonatomic,readonly) SecKeyRef privateKeyRef;
@property (nonatomic,readonly) NSData *publicKeyBits;
@property (nonatomic,readonly) NSData *privateKeyBits;
- (id)shareInstance;
(void)generateKeyPairRSACompleteBlock:(GenerateSuccessBlock)_success;
(NSData *)RSA_EncryptUsingPublicKeyWithData:(NSData *)data;
(NSData )RSA_EncryptUsingPrivateKeyWithData:(NSData)data;
(NSData *)RSA_DecryptUsingPublicKeyWithData:(NSData *)data;
(NSData )RSA_DecryptUsingPrivateKeyWithData:(NSData)data;
@end
// RSA.m
//
// Copyright (c) 2012 scott ban
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
#import "RSA.h"
uint8_t *plainBuffer;
uint8_t *cipherBuffer;
uint8_t *decryptedBuffer;
const size_t BUFFER_SIZE = 32;
const size_t CIPHER_BUFFER_SIZE = 1024;
const uint32_t PADDING = kSecPaddingPKCS1;
const size_t kSecAttrKeySizeInBitsLength = 2024;
//static const UInt8 publicKeyIdentifier[] = "com.apple.sample.publickey222\0";
//static const UInt8 privateKeyIdentifier[] = "com.apple.sample.privatekey111\0";
/**
- 公钥 */ static const UInt8 publicKeyIdentifier[] = "ba3a3302ed9e00d47775582ced852ac325dd8e79702baf754bd1b0a9e14857f0dee560f8f951b118f3331c661e6fd5ce7c9060a0e3bc2afefacf14dd81797610a074f86a5b725cba12daa8ada22d944d5a30b993a6145b4672f37e5c58e4ce563e79ed7bb485abb129df969ced30051537d5bd6ac70d1be3ddfa834100efc055"; static const UInt8 privateKeyIdentifier[] = "com.apple.sample.privatekey111\0";
#
[privateKeyAttr setObject:privateTag forKey:(__bridge id)kSecAttrApplicationTag];
// See SecKey.h to set other flag values.
cKey length];
NSMutableData *bits = [NSMutableData dataWithLength:keyBufferSize];
OSStatus sanityCheck = SecKeyDecrypt(key,
kSecPaddingPKCS1,
(const uint8_t *) [wrappedSymmetricKey bytes],
cipherBufferSize,
[bits mutableBytes],
&keyBufferSize);
NSAssert(sanityCheck == noErr, @"Error decrypting, OSStatus == %ld.", sanityCheck);
[bits setLength:keyBufferSize];
return bits;
}
(NSData *) RSA_EncryptUsingPublicKeyWithData:(NSData *)data{
return [self rsaEncryptWithData:data usingPublicKey:YES];
}(NSData ) RSA_EncryptUsingPrivateKeyWithData:(NSData)data{
return [self rsaEncryptWithData:data usingPublicKey:NO];
}(NSData *) RSA_DecryptUsingPublicKeyWithData:(NSData *)data{
return [self rsaDecryptWithData:data usingPublicKey:YES];
}(NSData ) RSA_DecryptUsingPrivateKeyWithData:(NSData)data{
return [self rsaDecryptWithData:data usingPublicKey:NO];
}
@end
IOS RSA 加密 这个案例我是从code4app下载的,但是里面不用openssl生成的.pem文件作为公钥,
而是直接用/** * 公钥 */ static const UInt8 publicKeyIdentifier[] = "ba3a3302ed9e00d47775582ced852ac325dd8e79702baf754bd1b0a9e14857f0dee560f8f951b118f3331c661e6fd5ce7c9060a0e3bc2afefacf14dd81797610a074f86a5b725cba12daa8ada22d944d5a30b993a6145b4672f37e5c58e4ce563e79ed7bb485abb129df969ced30051537d5bd6ac70d1be3ddfa834100efc055"; static const UInt8 privateKeyIdentifier[] = "com.apple.sample.privatekey111\0";为什么
这个用公钥么,我表示疑惑。。。这个案例可行么,,。。