IOS des ECB mode symmetric encryption and decryption

Source: Internet
Author: User

Recently busy with Android and iOS projects, write the android des ECB mode encryption and decryption (related connection: http://blog.csdn.net/vipa1888/article/details/8086037), and return to the IOS above, because I recently started to study IOS this year, so many things on iOS don't understand. After half a year's research, I finally got it done with symmetric encryption and decryption in the ECB mode of des on IOS, I encountered a very serious problem. Many hexadecimal bytes written on the Internet had problems. After my research, I found that one of them was wrong, the decrypted nsstring
Bit null. My code has fixed this issue. The source code is provided below for your reference:

First, paste the header file of the encryption class:
/// Desutil. h // Author: spring sky // QQ: 840950105 // Email: vipa1888@163.com // # import <Foundation/Foundation. h> @ interface desutil: nsobject/** DES encryption */+ (nsstring *) encryptusedes :( nsstring *) plaintext key :( nsstring *) Key; /** des decryption */+ (nsstring *) decryptusedes :( nsstring *) plaintext key :( nsstring *) Key; @ end

Encryption Class implementation class:

/// Desutil. M // Author: spring sky // QQ: 840950105 // Email: vipa1888@163.com // # import "desutil. H "# import <commoncrypto/commoncryptor. h> # import "converutil. H "@ implementation desutilstatic byte IV [] = {1, 2, 4, 5, 6, 7, 8};/* DES encryption */+ (nsstring *) encryptusedes :( nsstring *) cleartext key :( nsstring *) Key {nsstring * ciphertext = nil; nsdata * textdata = [cleartext datausingencoding: nsutf8stringencoding]; nsuinteger datalength = [cleartext length]; unsigned char buffer [1024]; memset (buffer, 0, sizeof (char); size_t numbytesencrypted = 0; cccryptorstatus cryptstatus = cccrypt (kccencrypt, encrypt, decrypt, [Key utf8string], kcckeysizedes, IV, [textdata bytes], datalength, buffer, 1024, & numbytesencrypted); If (cryptstatus = kccsuccess) {nslog (@ "DES encrypted successfully "); nsdata * Data = [nsdata datawithbytes: Buffer length :( nsuinteger) numbytesencrypted]; byte * BB = (byte *) [data bytes]; ciphertext = [converutil parsebytearray2hexstring: BB]; else {nslog (@ "DES encryption failed");} return ciphertext;}/** des decryption */+ (nsstring *) decryptusedes :( nsstring *) plaintext key :( nsstring *) key {nsstring * cleartext = nil; nsdata * textdata = [converutil parsehextobytearray: plaintext]; nsuinteger datalength = [textdata length]; unsigned char buffer [1024]; memset (buffer, 0, sizeof (char); size_t encrypted = 0; cccryptorstatus cryptstatus = cccrypt (kccdecrypt, encrypted, encrypted, [Key utf8string], kcckeysizedes, IV, [textdata bytes], datalength, buffer, 1024, & numbytesencrypted); If (cryptstatus = kccsuccess) {nslog (@ "des decrypted"); nsdata * Data = [nsdata datawithbytes: Buffer length :( nsuinteger) numbytesencrypted]; cleartext = [[nsstring alloc] initwithdata: data encoding: nsutf8stringencoding];} else {nslog (@ "des decryption failed");} return cleartext;} @ end
The following is a transcoding class, mainly hexadecimal and byte conversion header files:
//// Converutil. h // Author: spring sky // QQ: 840950105 // Email: vipa1888@163.com // # import <Foundation/Foundation. h> @ interface converutil: nsobject/** 64 encoding */+ (nsstring *) base64encoding :( nsdata *) text; /** convert byte to hexadecimal number */+ (nsstring *) parsebyte2hexstring :( byte *) bytes; /** convert byte array to hexadecimal number */+ (nsstring *) parsebytearray2hexstring :( byte []) bytes; /* convert hexadecimal data into an nsdata array */+ (nsdata *) parsehextobytearray :( nsstring *) hexstring; @ end

Implementation class:

//// Converutil. M // Author: spring sky // QQ: 840950105 // Email: vipa1888@163.com // # import "converutil. H "@ implementation converutilstatic const char encodingtable [] =" encoding +/";/** 64 encoding */+ (nsstring *) base64encoding :( nsdata *) text {If (text. length = 0) return @ ""; char * characters = malloc (text. length * 3/2); If (characters = NULL) return @ ""; in T end = text. length-3; int Index = 0; int charcount = 0; int n = 0; while (index <= END) {int d = (INT) (char *) [text bytes]) [Index]) & 0x0ff) <16) | (INT) (char *) [text bytes]) [index + 1]) & 0x0ff) <8) | (INT) (char *) [text bytes]) [index + 2]) & 0x0ff ); characters [charcount ++] = encodingtable [(d> 18) & 63]; characters [charcount ++] = encodingtable [(d> 12) & 63]; characters [charc Ount ++] = encodingtable [(d> 6) & 63]; characters [charcount ++] = encodingtable [D & 63]; index + = 3; if (N ++> = 14) {n = 0; characters [charcount ++] = '';}} if (Index = text. length-2) {int d = (INT) (char *) [text bytes]) [Index]) & 0x0ff) <16) | (INT) (char *) [text bytes]) [index + 1]) & 255) <8 ); characters [charcount ++] = encodingtable [(d> 18) & 63]; characters [charcount ++] = encod Ingtable [(d> 12) & 63]; characters [charcount ++] = encodingtable [(d> 6) & 63]; characters [charcount ++] = ';} else if (Index = text. length-1) {int d = (INT) (char *) [text bytes]) [Index]) & 0x0ff) <16; characters [charcount ++] = encodingtable [(d> 18) & 63]; characters [charcount ++] = encodingtable [(d> 12) & 63]; characters [charcount ++] = '; characters [charcount ++] =';} nsstring * Rtnstr = [[nsstring alloc] initwithbytesnocopy: characters length: charcount encoding: nsutf8stringencoding freewhendone: Yes]; return rtnstr ;} /** convert byte to hexadecimal number */+ (nsstring *) parsebyte2hexstring :( byte *) bytes {nsmutablestring * hexstr = [[nsmutablestring alloc] init]; int I = 0; If (bytes) {While (Bytes [I]! = '\ 0') {nsstring * hexbyte = [nsstring stringwithformat: @ "% x", bytes [I] & 0xff]; /// hexadecimal number if ([hexbyte length] = 1) [hexstr appendformat: @ "0% @", hexbyte]; else [hexstr appendformat: @ "% @", hexbyte]; I ++ ;}} nslog (@ "bytes hexadecimal number: % @", hexstr); Return hexstr ;} /** convert byte array to hexadecimal number */+ (nsstring *) parsebytearray2hexstring :( byte []) bytes {nsmutablestring * hexstr = [[nsmutablestring alloc] init]; int I = 0; If (BYT Es) {While (Bytes [I]! = '\ 0') {nsstring * hexbyte = [nsstring stringwithformat: @ "% x", bytes [I] & 0xff]; /// hexadecimal number if ([hexbyte length] = 1) [hexstr appendformat: @ "0% @", hexbyte]; else [hexstr appendformat: @ "% @", hexbyte]; I ++ ;}} nslog (@ "bytes hexadecimal number: % @", hexstr); return [hexstr uppercasestring];} /* convert hexadecimal data into an nsdata array */+ (nsdata *) parsehextobytearray :( nsstring *) hexstring {Int J = 0; byte bytes [hexstring. length]; for (INT I = 0; I <[hexstring length]; I ++) {int int_ch; /// The hexadecimal number after the two hexadecimal numbers are converted. unichar hex_char1 = [hexstring characteratindex: I]; /// the first (high * 16) int int_second in the two hexadecimal numbers; If (hex_char1> = '0' & hex_char1 <= '9 ') int_fill = (hex_char1-48) * 16; // 0 ascll-48 else if (hex_char1> = 'A' & hex_char1 <= 'F') int_fill = (hex_char1-55) * 16; // A's ascll-65 else int_ch1 = (hex_char1-87) * 16; // A's ascll-97 I ++; unichar hex_char2 = [hexstring characteratindex: I]; // The second (low) int int_ch2 in the two hexadecimal numbers; if (hex_char2> = '0' & hex_char2 <= '9') int_ch2 = (hex_char2-48 ); /// 0 ascll-48 else if (hex_char2> = 'A' & hex_char1 <= 'F') int_ch2 = hex_char2-55; /// A's ascll-65 else int_ch2 = hex_char2-87; // A's ascll-97 int_ch = int_ch + int_ch2; bytes [J] = int_ch; /// put the converted number into the byte array J ++;} nsdata * newdata = [[nsdata alloc] initwithbytes: bytes length: hexstring. length/2]; nslog (@ "newdata = % @", newdata); Return newdata ;}@ end
Test results:

19:58:41. 592 [3356: 707]
Plaintext= 1 palmpay key = 12345678.

19:58:41. 599 [3356: 707] desEncrypted

19:58:41. 601 [3356: 707] bytes
Of16Number of hexadecimal: 0be9d717b1478b32

19:58:41. 603 [3356: 707]
Encrypted data:0be9d717b1478b32

19:58:41. 604 [3356: 707] newdata = <0be9d717 b1478b32>

19:58:41. 607 [3356: 707] desDecrypted

19:58:41. 608 [3356: 707]
Decrypted data:1 palmpay



My work is mainly about banking and finance, so I often use various encryption and decryption methods. If you need help, please contact me. If I can solve this problem, I will do my best!

You are welcome to reprint it, but you must retain the copyright!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.