ios 两组NSData进行异或

+ (NSData *)xor_EncodeData:(NSData *)sourceData withKey:(NSData *)keyData {
    Byte *keyBytes = (Byte *)[keyData bytes]; //取关键字的Byte数组, keyBytes一直指向头部
    Byte *sourceDataPoint = (Byte *)[sourceData bytes]; //取需要加密的数据的Byte数组
    for (long i = 0; i < [sourceData length]; i++) {
        sourceDataPoint[i] = sourceDataPoint[i] ^ keyBytes[i]; //然后按位进行异或运算
    }
    return sourceData;
}
验证结果:

![图片.png](http://upload-images.jianshu.io/upload_images/2058284-84138472d797d75b.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)


与计算器的计算结果一致

你可能感兴趣的:(ios 两组NSData进行异或)