DES加密算法

因为要做关于硬件设备的开发所以需要加密运算 

这里面加密运算和其它的区别是不需要BASE64转码还有在解密的时候不用kCCOptionPKCS7Padding因为会发生部分数据无法解开

+(NSMutableData*) encryptUseDES:(NSMutableData*)plainText key:(NSMutableData*)key

{

NSString*ciphertext =nil;

NSMutableData*textData = [[NSMutableDataalloc]initWithData:plainText];

NSIntegera =8;

NSIntegerb = textData.length%a;

NSIntegerc = a-b;

if(b!=0) {

for(inti =0; i

{

NSString*string =@"00";

NSMutableData*data = [AES128dataWithHexString:string];

[textDataappendData:data];

}

}

NSUIntegerdataLength = [textDatalength];

unsignedcharbuffer[1024];

memset(buffer,0,sizeof(char));

size_tnumBytesEncrypted =0;

constvoid*keyvoid = (constvoid*)[keybytes];

CCCryptorStatuscryptStatus =CCCrypt(kCCEncrypt,kCCAlgorithmDES,

kCCOptionPKCS7Padding|kCCOptionECBMode,

keyvoid,kCCKeySizeDES,iv,

[textDatabytes], dataLength,

buffer,1024,

&numBytesEncrypted);

if(cryptStatus ==kCCSuccess) {

NSMutableData*data = [NSMutableDatadataWithBytes:bufferlength:(NSUInteger)dataLength];

returndata;

}

returnnil;

}

+(NSMutableData*) decryptUseDES:(NSMutableData*)cipherText key:(NSMutableData*)key

{

NSData* cipherData = [[NSDataalloc]initWithData:cipherText];

unsignedcharbuffer[1024];

memset(buffer,0,sizeof(char));

size_tnumBytesDecrypted =0;

Byteiv[] = {1,2,3,4,5,6,7,8};

constvoid*keyvoid = (constvoid*)[keybytes];

CCCryptorStatuscryptStatus =CCCrypt(kCCDecrypt,

kCCAlgorithmDES,

kCCOptionECBMode,

keyvoid,

kCCKeySizeDES,

iv,

[cipherDatabytes],

[cipherDatalength],

buffer,

1024,

&numBytesDecrypted);

NSString* plainText =nil;

if(cryptStatus ==kCCSuccess) {

NSMutableData* data = [NSMutableDatadataWithBytes:bufferlength:(NSUInteger)cipherText.length];

// NSLog(@"DES%@",data);

returndata;

}

returnnil;

}

你可能感兴趣的:(DES加密算法)