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

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

sm4加密 解密(oc)

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

前幾天項目用到sm4加密解密,加密為十六進制字符串,再將十六進制字符串解密。網上百度了下,sm4是密鑰長度和加密明文加密密文都為16個字節十六進制數據,網上的sm4 c語言算法很容易搜到,筆者剛開始沒怎么理解,以為只能對16字節數據進行加密,并且不論是多少字節的數據加密出來都是16字節的。后來看了下源碼,應該是加密的數據應該是16字節或者16的整數倍個字節的數據,若不夠16倍數字節應該補0x00數據,最后加密出來的數據和輸入數據的長度應該一致,即

密文長度=明文長度?

而且密鑰長度一致,是16字節的。c的代碼里面輸入輸出都是十六進制數據,需要將字符串轉為char類型數組,并且保證長度是16整數倍

+(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; }

  

解密時密文是十六進制字符串,需要將字符串先轉為int類型數組,再作解密操作,具體代碼  

+(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

  

轉載于:https://www.cnblogs.com/symen/p/6085486.html

總結

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

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。