iOS RSA2 加密、解密 、签名和验证签名

突然发现接收百度资源太多了,我也回馈一下吧。

RSA2 标准算法名称 SHA256WithRSA

1、RSA非对称加密技术

2、SHA256 是SHA-2下细分出的一种算法

SHA-2,名称来自于安全散列算法2(英语:Secure Hash Algorithm 2)的缩写,一种密码散列函数算法标准,由美国国家安全局研发,属于SHA算法之一,是SHA-1的后继者。

详细介绍连接:https://blog.csdn.net/u011583927/article/details/80905740

1、加密和解密

公钥是公开的密钥,有加密方使用。只用于加密无法解密。私钥是不公开的,别人无法获取,用户解密。

注意的是,为什么私钥对同一数据进行签名加密的结果是一样的,使用公钥进行加密就不一样了呢?

详细请参考:https://blog.csdn.net/guyongqiangx/article/details/74930951

2、签名和验证签名

签名是由发送数据的一方发起的,防止传输过程中被篡改数据内容。因此签名使用的是私钥。而验证签名使用的是公钥。

附带:

php RSA2 加密、解密 、签名和验证签名

https://blog.csdn.net/TaLinBoy/article/details/106124132

java:RSA2 加密、解密 、签名和验证签名

https://blog.csdn.net/TaLinBoy/article/details/106124535

1、下载git资源   https://github.com/ideawu/Objective-C-RSA

2、Objective-C-RSA 本身就是 RSA 加密和解密的Demo

3、添加签名和验证方法

      导入头文件:#import <CommonCrypto/CommonDigest.h>  

      否则无法使用 :CC_SHA256_DIGEST_LENGTH

      Objective-C-RSA包含通过base64字符串转私钥和公钥的方法:

     //服务器的公钥获取

     [self getServicePublicKey];

    //app的私钥

    [RSA addPrivateKey:[self getPrivateKey]]

    代码:


// 签名
+ (NSString *) sign:(NSString *)storString{
    // 使用哈希算法获取字符串摘要
    NSLog(@"=0===== [%@]",storString);
    // sha256加密
    NSData *outData = [self sha256:storString];
    SecKeyRef pKey = [RSA addPrivateKey:[self getPrivateKey]];
    size_t siglen = SecKeyGetBlockSize(pKey);
    uint8_t* signedHashBytes = malloc(siglen);
    memset(signedHashBytes, 0x0, siglen);
    SecKeyRawSign(pKey,
                  kSecPaddingPKCS1SHA256,
                  outData.bytes,
                  outData.length,
                  signedHashBytes,
                  &siglen);
    NSData* signedHash = [NSData dataWithBytes:signedHashBytes length:(NSUInteger)siglen];
    NSString *signString = [signedHash base64EncodedStringWithOptions:NSUTF8StringEncoding];
    NSLog(@"=1===== %@",signString);
    if (!signString) {
        return @"";
    }
    return signString;
}

//验签
+(BOOL) verifySign:(NSString *)response signString:(NSString *)signString{
    // sha256加密
    NSData *outData = [self sha256:response];
    // 签名base64解码
    NSData *signData = [[NSData alloc] initWithBase64EncodedString:signString options:NSDataBase64DecodingIgnoreUnknownCharacters];
    // 签名验证
    SecKeyRef pKey = [RSA addPrivateKey:[self getServicePublicKey]];
    size_t siglen = SecKeyGetBlockSize(pKey);
    const void* signedHashBytes = [signData bytes];
    OSStatus status = SecKeyRawVerify(pKey,
                                      kSecPaddingPKCS1SHA256,
                                      outData.bytes,
                                      outData.length,
                                      signedHashBytes,
                                      siglen);

    return status == errSecSuccess;
    
}


+ (NSData *) sha256:(NSString *)str {
    const char *s = [str cStringUsingEncoding:NSUTF8StringEncoding];
    NSData *strData = [NSData dataWithBytes:s length:strlen(s)];
    uint8_t digest [CC_SHA256_DIGEST_LENGTH] = {0};
    CC_SHA256(strData.bytes, (CC_LONG)strData.length, digest);
    NSData *data = [NSData dataWithBytes:digest length:CC_SHA256_DIGEST_LENGTH];
    return data;
}
原创文章 98 获赞 14 访问量 15万+

猜你喜欢

转载自blog.csdn.net/TaLinBoy/article/details/106140526