首页 > 代码库 > AES加密在IOS中的使用
AES加密在IOS中的使用
现在做的App,因为考虑到安全性,所以用到了AES加密,以及配对安卓使用的AES加密。
.h文件
#import <Foundation/Foundation.h>#import <CommonCrypto/CommonCryptor.h>#import <CommonCrypto/CommonKeyDerivation.h>#define AES_KEY @"cx@hy!*&y.)x#[;>"#define AES_IV @"0102030405060708"@interface NSData (AES)+ (NSString *) AESEncryptWithData:(NSData *) data;+ (NSData *) AESDecryptWithText:(NSString *) text;
.m文件
#import "NSData+AES.h"@implementation NSData (AES)static const char encodingTable[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";+ (NSString *) AESEncryptWithData:(NSData *)data{ // ‘key‘ should be 32 bytes for AES256, will be null-padded otherwise char keyPtr[kCCKeySizeAES256+1]; // room for terminator (unused) bzero(keyPtr, sizeof(keyPtr)); // fill with zeroes (for padding) NSUInteger dataLength = [data length]; size_t bufferSize = dataLength + kCCBlockSizeAES128; void *buffer = malloc(bufferSize); bzero(buffer, sizeof(buffer)); size_t numBytesEncrypted = 0; CCCryptorStatus cryptStatus = CCCrypt(kCCEncrypt, kCCAlgorithmAES128,kCCOptionPKCS7Padding, [[AES_KEY dataUsingEncoding:NSUTF8StringEncoding] bytes], kCCKeySizeAES128, [[AES_IV dataUsingEncoding:NSUTF8StringEncoding] bytes] /* initialization vector (optional) */, [data bytes], dataLength, /* input */ buffer, bufferSize, /* output */ &numBytesEncrypted); if (cryptStatus == kCCSuccess) { NSData *encryptData = http://www.mamicode.com/[NSData dataWithBytesNoCopy:buffer length:numBytesEncrypted];""; char *characters = malloc((([self length] + 2) / 3) * 4); if (characters == NULL) return nil; NSUInteger length = 0; NSUInteger i = 0; while (i < [self length]) { char buffer[3] = {0,0,0}; short bufferLength = 0; while (bufferLength < 3 && i < [self length]) buffer[bufferLength++] = ((char *)[self bytes])[i++]; // Encode the bytes in the buffer to four characters, including padding "=" characters if necessary. characters[length++] = encodingTable[(buffer[0] & 0xFC) >> 2]; characters[length++] = encodingTable[((buffer[0] & 0x03) << 4) | ((buffer[1] & 0xF0) >> 4)]; if (bufferLength > 1) characters[length++] = encodingTable[((buffer[1] & 0x0F) << 2) | ((buffer[2] & 0xC0) >> 6)]; else characters[length++] = ‘=‘; if (bufferLength > 2) characters[length++] = encodingTable[buffer[2] & 0x3F]; else characters[length++] = ‘=‘; } return [[NSString alloc] initWithBytesNoCopy:characters length:length encoding:NSASCIIStringEncoding freeWhenDone:YES];}@end
java文件
package com.yuxin168.library.util;import java.io.UnsupportedEncodingException;import java.security.InvalidAlgorithmParameterException;import java.security.InvalidKeyException;import java.security.NoSuchAlgorithmException;import javax.crypto.BadPaddingException;import javax.crypto.Cipher;import javax.crypto.IllegalBlockSizeException;import javax.crypto.NoSuchPaddingException;import javax.crypto.spec.IvParameterSpec;import javax.crypto.spec.SecretKeySpec;import android.util.Base64;/** * AESº”√‹ * * @author Baoyz * */public class AES { public static final String VIPARA = "0102030405060708"; public static final String bm = "UTF-8"; /** * º”√‹ * * @param content * –Ë“™º”√‹µƒƒ⁄»› * @param password * º”√‹√‹¬Î * @return */ public static String encrypt(String content) { try { IvParameterSpec zeroIv = new IvParameterSpec(VIPARA.getBytes()); SecretKeySpec key = new SecretKeySpec(getKey().getBytes(), "AES"); Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding"); cipher.init(Cipher.ENCRYPT_MODE, key, zeroIv); byte[] encryptedData = http://www.mamicode.com/cipher.doFinal(content.getBytes(bm));"AES"); Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding"); cipher.init(Cipher.DECRYPT_MODE, key, zeroIv); byte[] decryptedData = http://www.mamicode.com/cipher.doFinal(byteMi);"cx@hy!*&y.)x#[;>"; }}
下载地址如下:
http://pan.baidu.com/s/1kT1bzSV
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。