superdoliant / rhrsautil Goto Github PK
View Code? Open in Web Editor NEWRSA一站式解决办法
License: MIT License
RSA一站式解决办法
License: MIT License
(BOOL)RSAVerifyWithSHA1:(NSData *)signature plainString:(NSString *)plainString publicKey:(NSString *)publickey{
if (!signature || !plainString || !publickey) {
NSLog(@"参数不能为空");
return false;
}
SecKeyRef key = [self addPublicKey:publickey];
size_t signedHashBytesSize = SecKeyGetBlockSize(key);
// 要验证的签名
NSData *signatureData = signature;
const void *signedHashBytes = [signatureData bytes];
// 待验证的字符串
NSData *plainData = [plainString dataUsingEncoding:NSUTF8StringEncoding];
size_t hashBytesSize = CC_SHA1_DIGEST_LENGTH;
uint8_t *hashBytes = malloc(hashBytesSize);
if (!CC_SHA1([plainData bytes], (CC_LONG)[plainData length], hashBytes)) {
return false;
}
OSStatus status = SecKeyRawVerify(key,
kSecPaddingPKCS1SHA1,
hashBytes, // SHA1签名之后的原字符串
hashBytesSize, // SHA1签名的长度
signedHashBytes, // 签名值
signedHashBytesSize); // 公钥长度(签名值不能超过公钥的长度)
if(hashBytes) free(hashBytes);
return status == errSecSuccess;
}
大佬,请指点一下,为什么在这个 size_t signedHashBytesSize = SecKeyGetBlockSize(key);
地方通过key获取size_t的时候崩溃闪退比较多呢。多谢。
(NSData*)rsaDecryptData:(NSData*)data privKey:(NSString * )privKey{
SecKeyRef key = [MDWRSAUtil addPrivateKey:privKey];
size_t cipherBufferSize = SecKeyGetBlockSize(key);
size_t blockSize = cipherBufferSize;
size_t blockCount = (size_t)ceil([data length] / (double)blockSize);
NSMutableData *decryptedData = [[NSMutableData alloc] init];
for (int i = 0; i < blockCount; i++) {
unsigned long bufferSize = MIN(blockSize , [data length] - i * blockSize);
NSData *buffer = [data subdataWithRange:NSMakeRange(i * blockSize, bufferSize)];
size_t cipherLen = [buffer length];
void *cipher = malloc(cipherLen);
[buffer getBytes:cipher length:cipherLen];
size_t plainLen = SecKeyGetBlockSize(key);
void *plain = malloc(plainLen);
OSStatus status = SecKeyDecrypt(key, kSecPaddingPKCS1, cipher, cipherLen, plain, &plainLen);
if (status != noErr) {
return nil;
}
NSData *decryptedBytes = [[NSData alloc] initWithBytes:(const void *)plain length:plainLen];
[decryptedData appendData:decryptedBytes];
}
return decryptedData;
}
在第四行获取blocksize的时候,捕捉到异常崩溃。请大佬指点迷津,可能是哪里的问题。
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.