我现在遇到了一个涉及iOS加密的问题。
我的委托人给了我公钥
"-----BEGIN PUBLIC KEY-----
xxxx
-----END PUBLIC KEY-----"需要使用的填充策略是RSA/ECB/PKCS1Padding.对于安卓系统,这看起来非常简单
cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding");
cipher.init(Cipher.ENCRYPT_MODE, publicKey);
encryptedBytes = cipher.doFinal(plain.getBytes());
return encryptedBytes;我在iOS中看不到任何直接的方法来做到这一点。任何常用的pod,比如公钥密码,都不允许我强制使用PKCS1填充方案。作为一个非常缺乏RSA和加密经验的人,如果你能帮助我理解如何处理这件事并指导我完成这件事,我将非常感激。
发布于 2014-12-30 15:30:50
使用带有kSecPaddingPKCS1参数的标准安全框架- SecKeyEncrypt
发布于 2018-06-05 00:01:18
我的问题是使用非填充解决的:
kSecPaddingNone
发布于 2019-01-28 19:56:32
-(SecKeyRef)getPublicKeyForEncryption
{
NSString *thePath = [MAuthBundle pathForResource:@"certificate" ofType:@"der"];
//2. Get the contents of the certificate and load to NSData
NSData *certData = [[NSData alloc]
initWithContentsOfFile:thePath];
//3. Get CFDataRef of the certificate data
CFDataRef myCertData = (__bridge CFDataRef)certData;
SecCertificateRef myCert;
SecKeyRef aPublicKeyRef = NULL;
SecTrustRef aTrustRef = NULL;
//4. Create certificate with the data
myCert = SecCertificateCreateWithData(NULL, myCertData);
//5. Returns a policy object for the default X.509 policy
SecPolicyRef aPolicyRef = SecPolicyCreateBasicX509();
if (aPolicyRef) {
if (SecTrustCreateWithCertificates((CFTypeRef)myCert, aPolicyRef, &aTrustRef) == noErr) {
SecTrustResultType result;
if (SecTrustEvaluate(aTrustRef, &result) == noErr) {
//6. Returns the public key for a leaf certificate after it has been evaluated.
aPublicKeyRef = SecTrustCopyPublicKey(aTrustRef);
}
}
}
return aPublicKeyRef;
}
-(NSString*) rsaEncryptString:(NSString*) string
{
SecKeyRef publicKey = [self getPublicKeyForEncryption];
NSData* strData = [string dataUsingEncoding:NSUTF8StringEncoding];
CFErrorRef err ;
NSData * data = CFBridgingRelease(SecKeyCreateEncryptedData(publicKey, kSecKeyAlgorithmRSAEncryptionPKCS1, ( __bridge CFDataRef)strData, &err));
NSString *base64EncodedString = [data base64EncodedStringWithOptions:0];
return base64EncodedString;
}https://stackoverflow.com/questions/27701194
复制相似问题