Giter Site home page Giter Site logo

muzipiao / gmobjc Goto Github PK

View Code? Open in Web Editor NEW
340.0 6.0 87.0 79.07 MB

SM2/SM3/SM4/ECDH crypto library based on OpenSSL.

Home Page: https://muzipiao.github.io/2019/08/iOS-%E4%BD%BF%E7%94%A8-SM2-SM4-%E5%8A%A0%E8%A7%A3%E5%AF%86-SM2-%E7%AD%BE%E5%90%8D%E9%AA%8C%E7%AD%BE%E5%8F%8A-SM3-%E6%91%98%E8%A6%81/

License: MIT License

Objective-C 99.96% Ruby 0.03% Swift 0.01%
sm2 sm3 sm4 objective-c ecdh sm2-encryption sm2-signature-verification sm3-digest-algorithm swift ios

gmobjc's Introduction

你好,世界 👋

  • 📙 Focusing on Swift & iOS
  • 🌱 I’m currently learning flutter
  • 🔨 Creator of applications and frameworks
  • 📫 Reach me by email([email protected])

gmobjc's People

Contributors

muzipiao avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

gmobjc's Issues

求问sm2加签验签

感谢muzipiao终于让我找到一个靠谱的国密oc库和demo。
但关于sm2的加签验签,这里有问题求问。
这边我们希望使用的是sm3withsm2(类似sha256withRSA),
demo里面的例子,我看到digest使用的是EVP_sm3。
这里我可否理解这个加签验签的事例其实就是 sm3withsm2?
我理解上有什么纰漏,也烦请指出。

Framework真机编译报错

因为pod后发现和现有项目百度地图sdk的ssl冲突,所以打算打成framework,但是真机编译报错

could not reparse object file in bitcode bundle: 'Invalid bitcode version (Producer: '1103.0.32.59.0_0' Reader: '1100.0.33.8_0')', using libLTO version 'LLVM version 11.0.0, (clang-1100.0.33.8)' for architecture arm64

模拟器编译可以成功

java侧sm2验签失败

公、私钥、userID、椭圆曲线参数都一致,java端也对z项做了摘要,本地签名验签成功,服务端失败,什么原因

iOS使用secp256k1出现校验失败

单独使用demo里的testECDH,出现校验失败,加密算法用的k256k1EllipticCurveType
EC_GROUP *group = EC_GROUP_new_by_curve_name(k256k1EllipticCurveType); // 椭圆曲线

openssl冲突

由于老项目用到RSA加密 依赖的库用到RSA_size等方法 我把GMObjC.framework在Link Binary With Libraries中拖到最后

编译运行的时候没问题 但是Archive的时候 打包没问题 运行还是报错

或者有无办法将RSA相关的方法干掉?

这个是不是Other Linker Flag -ObjC的功能 有无方法单独GMObjC.framework不添加-ObjC进行编译?

有空帮忙看看吗?谢谢!

你好,请教关于证书读取出现的问题

你好,多有打搅,请多见谅。
我读取.key文件的公钥,解析读取后,得到格式为
-----BEGIN PUBLIC KEY-----
MFkwEwYHK...(中间忽略)....QbJMgIT
PzAYl4qN...(中间忽略)....1JxIw+TXf/R9/jSkkkA==
-----END PUBLIC KEY-----
但是安卓读取.key文件的公钥得到:
EC Public Key [6c:37:39:4e:bd:...(中间忽略)....2:9b:3d:37:4d:5d:b0:93:db]
X: 8f6432e54a5c...(中间忽略)....18978e2a34f873d1ad
Y: 163f98a462fd...(中间忽略)....35dffd1f7f8d292490
我使用安卓的XY两字符串的公钥加04开头,完全没问题,所以应该是我读取.key文件有问题,所以我想请教应该怎么获取安卓这种格式的公钥,非常感谢。

编译动态库,获取的framework无法使用

我在工程里的GMObjC.framework里command+b得到的framework时,使用sm2加密,加密出的都是nil,但是使用xcframework是可以使用sm2加密的。
由于我们的项目无法使用xcframwork,并且openssl版本冲突,只能使用framework。所以我该如何获得正确的framework呢

missing required architecture armv7

ld: warning: ignoring file /Users/aiquantong/Documents/me/code/jianhua.git/code/Pods/GMOpenSSL/GMOpenSSL/openssl.framework/openssl, missing required architecture armv7 in file /Users/aiquantong/Documents/me/code/jianhua.git/code/Pods/GMOpenSSL/GMOpenSSL/openssl.framework/openssl (3 slices)

项目崩溃

你好,通过你说的编译后导入openssl.framework文件,然后手动导入GMObjC,正常情况下是没事的,但是当我们集成BaiduMapKit之后,会报错,请问这该怎样解决呢?QQ:1587557282。麻烦加我一下qq吧,有些问题想请教。

if (!EC_KEY_generate_key(key))
地方崩溃
Thread 1: EXC_BAD_ACCESS (code=2, address=0x106ccd3a0)

///MARK: - 创建公私钥对

+ (NSArray<NSString *> *)createKeyPair{
    NSArray<NSString *> *keyArray = @[@"", @""];
    EC_GROUP *group = EC_GROUP_new_by_curve_name(NID_sm2); // 椭圆曲线
    EC_KEY *key = NULL; // 密钥对
    do {
        key = EC_KEY_new();
        if (!EC_KEY_set_group(key, group)) {
            break;
        }
        if (!EC_KEY_generate_key(key)) {
            break;
        }
        const EC_POINT *pub_key = EC_KEY_get0_public_key(key);
        const BIGNUM *pri_key = EC_KEY_get0_private_key(key);

        char *hex_pub = EC_POINT_point2hex(group, pub_key, EC_KEY_get_conv_form(key), NULL);
        char *hex_pri = BN_bn2hex(pri_key);
        
        NSString *pubHex = [NSString stringWithCString:hex_pub encoding:NSUTF8StringEncoding];
        NSString *priHex = [NSString stringWithCString:hex_pri encoding:NSUTF8StringEncoding];
        if (pubHex.length > 0 && priHex.length > 0) {
            NSString *priHexWithPadding = [self bnToHexPadding:priHex];
            keyArray = @[pubHex, priHexWithPadding];
        }
        OPENSSL_free(hex_pub);
        OPENSSL_free(hex_pri);
    } while (NO);
    
    if (group != NULL) EC_GROUP_free(group);
    EC_KEY_free(key);
    
    return keyArray;
}

SM4 ECB 模式解密时程序崩溃

16 进制编码
密文 410cc52868d787be3374dc9d27e37e26dc26dc7d2653883d615b0d1dd707325e
秘钥 D4E06ACBEC6240E78802E5A910CD9589
调用 GMSm4Utils + (nullable NSString *)ecbDecryptText:(NSString *)ciphertext key:(NSString *)key; 函数解密时程序奔溃了
麻烦帮忙看下, 万分感谢

xcode pod GMObjc 打包出错

项目中 pod GMObjc 库后,在真机上 和模拟器上都正常运行,但是打包(Archive)后出错,

pod fail

pod search GMObjC
[!] Unable to find a pod with name, author, summary, or description matching GMObjC

pod install
Analyzing dependencies
[!] CocoaPods could not find compatible versions for pod "GMObjC":
In Podfile:
GMObjC (= 3.0.0)

None of your spec sources contain a spec satisfying the dependency: GMObjC (= 3.0.0).

You have either:

  • out-of-date source repos which you can update with pod repo update or with pod install --repo-update.
  • mistyped the name or version.
  • not added the source repo that hosts the Podspec to your Podfile.

Note: as of CocoaPods 1.0, pod repo update does not happen on pod install by default.

手动引入的时候,找不到sm2.h,sm3.h,sm4.h中的方法

因为某些原因,只能手动引入,不能使用cocopods。
我需要将您的GMObjC以及GMOppenSSL手动引入,并封装在我自己的SDK内, 手动引入后,编译打包成framework库没有问题,但是项目中引入这个framework库后,报找不到sm2.h,sm3.h,sm4.h中的方法。
路径配置都看了,是没有问题的。能帮忙看下是什么问题吗?
截屏2020-02-11下午1 24 17

pod运行项目直接崩溃

pod 'AFNetworking', '> 4.0.1'
pod 'SDWebImage', '
> 5.7.3'
pod 'SDWebImageWebPCoder'
pod 'Masonry', '> 1.1.0'
pod 'SDCycleScrollView', '
> 1.80'

MJRefresh

pod 'MJRefresh', '> 3.4.1'
pod 'BaiduMapKit', '
> 5.0.0'
pod 'BMKLocationKit', '> 1.8.0'
pod 'FMDB', '
> 2.7.5'
pod 'TZImagePickerController', '> 3.3.1'
pod 'Bugly', '
> 2.5.2'
pod 'MJExtension', '> 3.2.1'
pod 'IQKeyboardManager', '
> 6.5.5'
pod 'MBProgressHUD', '> 1.2.0'
pod 'VehicleKeyboard-swift'
pod 'UMCCommon'
pod 'UMCAnalytics'
pod 'XDCaptureService', '
> 1.2.1'
pod 'FTPopOverMenu', '1.3.2'
pod 'AMapLocation'
pod 'GMObjC','3.0.0'
source 'https://github.com/aliyun/aliyun-specs.git'
pod 'AliyunOSSiOS', '> 2.10.8'
pod 'AlicloudPush', '
> 1.9.9.1'
pod 'AlicloudCrash' , '~> 1.1.0'

我项目中存在这些三方,是存在冲突了吗?

GMObjC.framework 动态库

将 GMObjC.framework 设置为 Embed & Sign,编译运行项目时,xcode提示Unable to install,将动态库删除后项目正常运行,希望大神给个解决方案。
xcode版本:12.3

请教,后台提供key文件的公钥无法用于加密

后台提供.key文件的公钥,解析读取后,得到格式为
-----BEGIN PUBLIC KEY-----
MFkwEwYHK...(中间忽略)....QbJMgIT
PzAYl4qN...(中间忽略)....1JxIw+TXf/R9/jSkkkA==
-----END PUBLIC KEY-----
我把上下两端字符串按照教程传输进去:
NSArray *pubKey = @"MFkwEwYHK...(中间忽略)....QbJMgIT""PzAYl4qN...(中间忽略)....1JxIw+TXf/R9/jSkkkA==";
感觉格式明显对不上。
果然,加密@"123456"的结果返回为nil。
后来进行key的Hex编码处理:
NSString *keyHex = [GMUtils stringToHex:pubKey];
然而,加密@"123456"的结果依然返回为nil。
请问还要处理什么吗?

pod导入后 调用sm2加密 结果返回nil

你好,首先表达感谢,可以有这么好的工具用来使用国密相关加解密。
现在遇到问题,我在导入 GMObjc 后,使用sm2加密 ,返回结果为nil,通过断点调试,发现EC_GROUP *group = EC_GROUP_new_by_curve_name(NID_sm2); // 椭圆曲线
返回结果 group 为null,但是我单独新建demo调试的时候,是可以加密成功的
不知道什么原因,请大神解惑

SM4加密后结果为null

NSString *sm4Key = @"8888888888888888";
NSString *plaintext = @"{"idserial":"123456"}";
NSString *ecbCipertext = [GMSm4Utils ecbEncryptText:plaintext key:sm4Key];
NSLog(@"打印SM4加密:%@", ecbCipertext);

image

关于ECDHE的使用问题

你好
我在使用ECDHE过程中遇到一个问题:
我们使用的椭圆曲线是:

基于 ECC NIST P-256 曲线(即 secp256r1)进行计算,该曲线的参数详情见:https://www.secg.org/SEC2-
Ver-1.0.pdf
  
"secp256r1": {
"p": 0xffffffff00000001000000000000000000000000ffffffffffffffffffffffff,
"a": 0xffffffff00000001000000000000000000000000fffffffffffffffffffffffc,
"b": 0x5ac635d8aa3a93e7b3ebbd55769886bc651d06b0cc53b0f63bce3c3e27d2604b,
"g": (0x6b17d1f2e12c4247f8bce6e563a440f277037d812deb33a0f4a13945d898c296,
0x4fe342e2fe1a7f9b8ee7eb4a7c0f9e162bce33576b315ececbb6406837bf51f5),
"n": 0xffffffff00000000ffffffffffffffffbce6faada7179e84f3b9cac2fc632551,
"h": 0x1 }

我修改了您的代码

EC_GROUP *group = EC_GROUP_new_by_curve_name(NID_secp256k1); // 椭圆曲线
//我看着NID_secp256k1 很像 , 就替换了椭圆曲线, 

验证也是正确的.

但是我们有一个测试文档

    //A私钥85c888a8c5aaeba1f1a056a093ff874e3e01524f3571320680a13bdedab3db56
    //A公钥1c4ad6faf196e616c28832a540c78af8e0c3e4c6e27fcd82d479ae6497050db645c7debe0efc508e4f0d8a2fef5496c1174a4aa149e10cd5b2f02a5e7eb4faa6
    
    //B私钥1d9bec7294e2a7426acff1c48e69d2dc995d471d3fe045b1bc3fa21298813fc8
    //B公钥8d413fedaa2d0fd1036c27a3cc08e1776b951918513bbd5d8d22286ade63cfbccc4ae4bfcf69efb580409c056850429bbb6453227aa0fc0670c0f23c53593bee
    
    //结果
//59e7866e936b8f4f4bcca03fe7de81483ae78f9939cd8084a3bdca6f813dfd63762892918be716e7a169c717eb9dc3a92d777a47f2dcc620c0130f2906a18ac6

我现在把这个数据传到API里面,
计算秘钥的时候
int ret = ECDH_compute_key(ecdh_text, outlen, pub_point, key, 0);
ret = 0;

我这边给的ECDHE文档.zip
结合我这边的数据, 在demo里面计算秘钥失败.
能帮忙看下是什么原因吗?
如果可以, 希望能得到您的帮助.

新版本不支持armv7,引入工程后,可以模拟器运行,但是打包link报错怎么处理啊,报错日志如下

Undefined symbols for architecture armv7:
"OBJC_CLASS$_GMSm4Utils", referenced from:
objc-class-ref in URLSingleton.o
"OBJC_CLASS$_GMSm3Utils", referenced from:
objc-class-ref in URLSingleton.o
"OBJC_CLASS$_GMSm2Utils", referenced from:
objc-class-ref in URLSingleton.o
ld: symbol(s) not found for architecture armv7
clang: error: linker command failed with exit code 1 (use -v to see invocation)

关于sm2keyexchange

最近和java同学联调,关于密钥协商这块,我用的ecdh,但java端亮出了SM2keyExchange的神秘算法,说是jce自带的。
乍一看和ecdhe还有点像,这块儿求教@muzipiao,openssl也提供类似的接口吗?

R+S加签

加签后的数据长度是固定的128位吗?

error read memory failed

1、百度地图6.0.0版本,解决OpenSSL冲突问题
2、GMObjC 3.1.2版本
集成以上两个SDK版本,运行程序直接报error read memory failed,请作者看一下什么原因

OpenSSL 冲突

作者你好,我这是和百度地图openssl冲突,按照你说的升级第三方opebsll库,然后公用一个可以解决。但是我这 和百度地图冲突,不是很明白要怎么和百度地图解决冲突。可以麻烦你指点一下吗?谢谢。我的QQ:1587557282. 如果可以,想加你qq详细了解一下。谢谢!

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.