数据加密RSA加密(openSSL)
RSA使用"秘匙对"对数据进行加密解密.在加密解密数据前,需要先生成公钥(public key)和私钥(private key).
- 公钥(public key): 用于加密数据. 用于公开, 一般存放在数据提供方, 例如iOS客户端.
- 私钥(private key): 用于解密数据. 必须保密, 私钥泄露会造成安全问题.

BBRSACRptor文件目录:

生成公私钥对 /** Generate rsa key pair by the key size. @param keySize RSA key bits . The value could be `512`,`1024`,`2048` and so on. Normal is `1024`. */ - (BOOL)generateRSAKeyPairWithKeySize:(int)keySize; ///////返回公钥 /** @abstract export public key, 'generateRSAKeyPairWithKeySize' or 'importRSAPublicKeyBase64' should call before this method @return public key base64 encoded */ - (NSString *)base64EncodedPublicKey; ///////返回私钥 /** @abstract export public key, 'generateRSAKeyPairWithKeySize' or 'importRSAPrivateKeyBase64' should call before this method @return private key base64 encoded */ - (NSString *)base64EncodedPrivateKey;
1 ///////在用公钥进行加密前需要先插入公钥 2 /** 3 4 @abstract import public key, call before 'encryptWithPublicKey' 5 6 @param publicKey with base64 encoded 7 8 @return Success or not. 9 10 */ 11 12 - (BOOL)importRSAPublicKeyBase64:(NSString *)publicKey;
1 ////////////////同上用私钥解密时需先插入私钥 2 /** 3 @abstract import private key, call before 'decryptWithPrivateKey' 4 @param privateKey with base64 encoded 5 @return Success or not. 6 */ 7 - (BOOL)importRSAPrivateKeyBase64:(NSString *)privateKey;
//////用公钥进行明文加密 /** @abstract encrypt text using RSA public key @param padding type add the plain text @return encrypted data */ - (NSData *)encryptWithPublicKeyUsingPadding:(RSA_PADDING_TYPE)padding plainData:(NSData *)plainData; //////用私钥进行密文解密 /** @abstract decrypt text using RSA private key @param padding type add the plain text @return encrypted data */ - (NSData *)decryptWithPrivateKeyUsingPadding:(RSA_PADDING_TYPE)padding cipherData:(NSData *)cipherData;
这里只举出几个常用的API函数
具体的API接口可以看.h文件
一般只要加密即可:
1.插入公钥
2.公钥加密
3.密文上传
4.服务器密文解密
取自苏宁app
1 //RSA公钥 2 #ifdef kReleaseH 3 #define kRegisterRSAPublicKey \ 4 @"MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDE2aunhLVwLp0rsXhRhpkoaKHIt2kaFoaiwVzn98BApxEs3wmyf9w1YrZBFlm9L4JNjzT+X1KFbAEbnHuqZArys06KedkwlhsdSXjFDJgSi7PyN/bmnbXptvL0BNJKatwGRo9I/hVAP42i/HdecWhrlUcmT/TJk2cznKXhoNq2WQIDAQAB" 5 #else 6 #define kRegisterRSAPublicKey \ 7 @"MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCSh6+KnrtF37KHrGbWnfr9qlOsdtxER3CezagsRHbdBD9CLo3aCbRQMjG9f11Dyp0USB7eX0tc/naBvX4qXuKjeu8oPwnqyARRmUkiBHLwCRolSYJgzmSM6wpvd5R95uA/SfPTQgWulHV6b0c5AAT6Ei8klHGtUHOXgXsnLihGWwIDAQAB" 8 #endif
1 #warning -NWl RSA数据加密 2 - (void)beginUserRegisterWithUsername:(NSString *)mobileNum password:(NSString *)password 3 { 4 self.userName = mobileNum; 5 self.password = password; 6 7 NSMutableDictionary *postDataDic = [[NSMutableDictionary alloc] initWithCapacity:1]; 8 9 [postDataDic setObject:mobileNum forKey:@"mobileNum"]; 10 [postDataDic setObject:@"208000201003" forKey:@"accountCreatedChannel"]; 11 12 13 BBRSACryptor *cryptor = [[BBRSACryptor alloc] init]; 14 15 [cryptor importRSAPublicKeyBase64:kRegisterRSAPublicKey];//base64加密的字符串 16 NSData *data = [cryptor encryptWithPublicKeyUsingPadding:RSA_PKCS1_PADDING 17 plainData:[password dataUsingEncoding:NSUTF8StringEncoding]]; 18 19 NSString *passWord = [GTMBase64 stringByEncodingData:data]; 20 21 [postDataDic setObject:passWord forKey:@"password"]; 22 23 NSString *url = [NSString stringWithFormat:@"%@%@",KNewHomeAPIURL,@"mts-web/appbuy/register/doregister.do"]; 24 25 HTTPMSG_RELEASE_SAFELY(userRegisterMsg); 26 27 userRegisterMsg = [[HttpMessage alloc] initWithDelegate:self 28 requestUrl:url 29 postDataDic:postDataDic 30 cmdCode:CC_UserRegister]; 31 32 [self.httpMsgCtrl sendHttpMsg:userRegisterMsg]; 33 }
参考文献:
http://witcheryne.iteye.com/blog/2171850
http://blog.cnbluebox.com/blog/2014/03/25/ios7-dot-1xia-shi-yong-adhocfang-fa-xia-zai-de-jie-jue-fang-an/

浙公网安备 33010602011771号