日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程资源 > 编程问答 >内容正文

编程问答

sm4加密 解密(oc)

發(fā)布時(shí)間:2025/4/16 编程问答 30 豆豆
生活随笔 收集整理的這篇文章主要介紹了 sm4加密 解密(oc) 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

前幾天項(xiàng)目用到sm4加密解密,加密為十六進(jìn)制字符串,再將十六進(jìn)制字符串解密。網(wǎng)上百度了下,sm4是密鑰長(zhǎng)度和加密明文加密密文都為16個(gè)字節(jié)十六進(jìn)制數(shù)據(jù),網(wǎng)上的sm4 c語(yǔ)言算法很容易搜到,筆者剛開(kāi)始沒(méi)怎么理解,以為只能對(duì)16字節(jié)數(shù)據(jù)進(jìn)行加密,并且不論是多少字節(jié)的數(shù)據(jù)加密出來(lái)都是16字節(jié)的。后來(lái)看了下源碼,應(yīng)該是加密的數(shù)據(jù)應(yīng)該是16字節(jié)或者16的整數(shù)倍個(gè)字節(jié)的數(shù)據(jù),若不夠16倍數(shù)字節(jié)應(yīng)該補(bǔ)0x00數(shù)據(jù),最后加密出來(lái)的數(shù)據(jù)和輸入數(shù)據(jù)的長(zhǎng)度應(yīng)該一致,即

密文長(zhǎng)度=明文長(zhǎng)度?

而且密鑰長(zhǎng)度一致,是16字節(jié)的。c的代碼里面輸入輸出都是十六進(jìn)制數(shù)據(jù),需要將字符串轉(zhuǎn)為char類(lèi)型數(shù)組,并且保證長(zhǎng)度是16整數(shù)倍

+(unsigned char*)hexEnc:(NSString*)strInput{NSData* data = [strInput dataUsingEncoding:NSUTF8StringEncoding];NSUInteger length = data.length;NSUInteger plusLength;if(length % 16 == 0){plusLength = 0;}else{plusLength = 16 - length % 16;}NSMutableString* new_str = [[NSMutableString alloc] initWithString:strInput];for (int i =0;i < plusLength;i++) {[new_str appendString:@" "];}NSUInteger new_length = length+plusLength;Uchar *input = (Uchar*)malloc(sizeof(Uchar)*new_length);Uchar *output = (Uchar*)malloc(sizeof(Uchar)*new_length);Uchar key[16] = KEY;const char *utfChar = [new_str UTF8String];memset(input, 0, new_length);memcpy(input, utfChar, new_length);sm4_context ctx;unsigned long i;sm4_setkey_enc(&ctx,key);sm4_crypt_ecb(&ctx,1,new_length,input,output);for(i=0;i<new_length;i++)printf("%02x ", output[i]);printf("\n");unsigned char* c_str = Hex2Str(output,new_length);printf("%s\n", c_str);free(input);free(output);return c_str; }

  

解密時(shí)密文是十六進(jìn)制字符串,需要將字符串先轉(zhuǎn)為int類(lèi)型數(shù)組,再作解密操作,具體代碼  

+(unsigned char*)hexDec:(NSString*)strInput{int inputCharSize = strInput.length/2;Uchar* input = (Uchar*)malloc(sizeof(Uchar)*inputCharSize);Uchar* output = (Uchar*)malloc(sizeof(Uchar)*inputCharSize);for (int i = 0; i<inputCharSize; i++) {NSString* str = [strInput substringWithRange:NSMakeRange(i*2, 2)];NSString* gw = [str substringWithRange:NSMakeRange(0, 1)];NSString* dw = [str substringWithRange:NSMakeRange(1, 1)];int n_gw = [HexToStr str2Int:gw];int n_dw = [HexToStr str2Int:dw];int result = n_gw * 16 + n_dw;input[i] = result;}Uchar key[16] = KEY;sm4_context ctx;sm4_setkey_dec(&ctx,key);sm4_crypt_ecb(&ctx,0,inputCharSize,input,output);int kgPos = 0;for(int i=0;i<inputCharSize;i++){printf("%02x ", output[i]);if (output[i] == 32) {kgPos = i;output[i] = '\0';}}printf("\n");free(input); // free(output);return output; }

  

demo地址:https://github.com/dinner/sm4

  

轉(zhuǎn)載于:https://www.cnblogs.com/symen/p/6085486.html

總結(jié)

以上是生活随笔為你收集整理的sm4加密 解密(oc)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。