首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >RSA/ECB/PKCS1填充iOS加密

RSA/ECB/PKCS1填充iOS加密
EN

Stack Overflow用户
提问于 2014-12-30 15:20:00
回答 3查看 3.6K关注 0票数 1

我现在遇到了一个涉及iOS加密的问题。

我的委托人给了我公钥

代码语言:javascript
复制
"-----BEGIN PUBLIC KEY-----
xxxx
-----END PUBLIC KEY-----"

需要使用的填充策略是RSA/ECB/PKCS1Padding.对于安卓系统,这看起来非常简单

代码语言:javascript
复制
cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding");
cipher.init(Cipher.ENCRYPT_MODE, publicKey);
encryptedBytes = cipher.doFinal(plain.getBytes());

return encryptedBytes;

我在iOS中看不到任何直接的方法来做到这一点。任何常用的pod,比如公钥密码,都不允许我强制使用PKCS1填充方案。作为一个非常缺乏RSA和加密经验的人,如果你能帮助我理解如何处理这件事并指导我完成这件事,我将非常感激。

EN

回答 3

Stack Overflow用户

发布于 2014-12-30 15:30:50

使用带有kSecPaddingPKCS1参数的标准安全框架- SecKeyEncrypt

票数 2
EN

Stack Overflow用户

发布于 2018-06-05 00:01:18

我的问题是使用非填充解决的:

kSecPaddingNone

票数 0
EN

Stack Overflow用户

发布于 2019-01-28 19:56:32

代码语言:javascript
复制
-(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;
}
票数 -2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/27701194

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档