网上有现成代码?######给你发一个DES的,你发发,应该就可以当RSA的了吧
#import "DESOpr.h" #import "GTMBase64.h" @implementation DESOpr static Byte iv[] = {1,2,3,4,5,6,7,8}; +(NSString *) encryptUseDES:(NSString *)plainText key:(NSString *)key { NSString *ciphertext = nil; const char *textBytes = [plainText UTF8String]; NSUInteger dataLength = [plainText length]; unsigned char buffer[1024]; memset(buffer, 0, sizeof(char)); size_t numBytesEncrypted = 0; CCCryptorStatus cryptStatus = CCCrypt(kCCEncrypt, kCCAlgorithmDES, kCCOptionPKCS7Padding, [key UTF8String], kCCKeySizeDES, iv, textBytes, dataLength, buffer, 1024, &numBytesEncrypted); if (cryptStatus == kCCSuccess) { NSData *data = [NSData dataWithBytes:buffer length:(NSUInteger)numBytesEncrypted]; data = [GTMBase64 encodeData:data]; //base64加密 ciphertext = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]; } return ciphertext; } +(NSString *) decryptUseDES:(NSString *)plainText key:(NSString *)key { NSString *ciphertext = nil; const char *textBytes = [plainText UTF8String]; NSUInteger dataLength = [plainText length]; unsigned char buffer[1024]; memset(buffer, 0, sizeof(char)); size_t numBytesEncrypted = 0; CCCryptorStatus cryptStatus = CCCrypt(kCCDecrypt, kCCAlgorithmDES, kCCOptionPKCS7Padding, [key UTF8String], kCCKeySizeDES, iv, textBytes, dataLength, buffer, 1024, &numBytesEncrypted); if (cryptStatus == kCCSuccess) { NSData *data = [NSData dataWithBytes:buffer length:(NSUInteger)numBytesEncrypted]; data = [GTMBase64 encodeData:data]; //base64加密 ciphertext = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]; } return ciphertext; } @end
######只在本地加解密?可以用OpenSSL。如果和服务器互相通讯,也用OpenSSL吧~ 上面那个DES的,对RSA没有任何参考意义。
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。