IOS-Des algorithm

Source: Internet
Author: User

Algorithm step: des algorithm to the 64-bit plaintext input block into 64-bit cipher output block, it used the key is also 64 bits (actually used to 56 bits, 8th, 16, 24, 32, 40, 48, 56, 64 bits is the check bit, so that each key has an odd number of 1), The algorithm is mainly divided into two steps: 1) The function of the initial permutation is to re-assemble the input 64 bits of data block, and the output is divided into L0, R0 two parts, each length of 32 bits, the substitution rules for the 58th position of the input to the first position, 50th place to 2nd bit ... And so on, the last one is the original 7th place. L0, R0 is the two parts of the output, L0 is the left 32 bits of output, R0 is the right 32 bits, example: set the input value before the change is d1d2d3 ... D64, the result after the initial substitution is: L0=D58D50 ... D8; R0=d57d49 ... D7. The permutation rules are shown in the following table: 58, 50,42,34,26,18,10,2,60,52,44,36,28,20,12,4,62,54,46,38,30,22,14,6,64,56,48,40,32,24,16,8,57,49,41,33,25,17,9,1,59,51,43,3 5,27,19,11,3,61,53,45,37,29,21,13,5,63,55,47,39,31,23,15,7,2) After 16 iterations of the inverse permutation, the L16 and R16 are obtained, which is used as input for inverse substitution, Inverse permutation is exactly the inverse of the initial permutation, which results in a ciphertext output. This algorithm is representative of symmetric encryption algorithm system and widely used in computer network system. OC Code:
#import "Nsstring+mfdes.h" #import <commoncrypto/commoncrypto.h>//key must be 8-bit # define key @ "01234567"//vector static Byte iv[] = {1, 2, 3, 4, 5, 6, 7, 8}; @implementation NSString (Mfdes)//1.    String symmetric encryption (DES)-(NSString *) stringwithdes{//1. Convert the string itself to the C string const char *string = [self utf8string]; 2.    Receiving container unsigned char byte[1024]; 3.    Container size size_t dataoutmoved = 0; 4. DES encryption process//1. Parameter 1. Encrypt or decrypt//2. Parameter 2: Encryption/Decryption method//3. Parameter 3: Options for grouping passwords//4. Parameter 4: Secret key//5. Parameter 5: Secret key size//6. Parameter 6: Vector//7. The argument 7:c the string//8. Parameter 8: Length of string//9. Parameter 9: Receive container//10. Parameter 10: Container size//11. Parameter 11: The final number of containers cccryptorstatus Cryptorstatus = Cccrypt (Kccencrypt, Kccalgorithmdes, kccoptionpkcs7padding, [key UTF8Stri        NG], Kcckeysizedes, IV, String, strlen (String), Byte, 1024x768, &dataoutmoved);        if (Cryptorstatus = = kccsuccess) {//turn to String = = Base64 return NSLog (@ "encryption succeeded");        NSData *desdata = [NSData datawithbytes:byte length:dataoutmoved]; NsstrinG *desbasestring = [Desdata base64encodedstringwithoptions:nsdatabase64encoding64characterlinelength];            return desbasestring;            } else {NSLog (@ "encryption failed");    } return @ ""; }//2. String symmetric decryption (DES)-(NSString *) deswithstring{nsdata *base64data = [[NSData alloc] initwithbase64encodedstring:self Opti        Ons:nsdatabase64decodingignoreunknowncharacters];    const void *byte = Base64data.bytes;    unsigned char buffer[1024];        size_t dataoutmoved = 0;  C language method Cccryptorstatus cryptorstatus = Cccrypt (Kccdecrypt, Kccalgorithmdes, kccoptionpkcs7padding, [key UTF8String],        Kcckeysizedes, IV, Byte, base64data.length, buffer, 1024x768, &dataoutmoved);        if (Cryptorstatus = = kccsuccess) {NSLog (@ "decryption succeeded");        NSData *desdata = [NSData datawithbytes:buffer length:dataoutmoved];        NSString *desbasestring = [[NSString alloc] Initwithdata:desdata encoding:nsutf8stringencoding];           return desbasestring; } else {NSLog (@ "decryption failed");    } return @ ""; }

IOS-Des algorithm

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.