最近项目中用到AES加密,但在网上找了很多的库都是Object-C与Java加密后不能项目解密,因为我们的服务器是用java写的,所以不能通用对于做iOS的就是个大麻烦,Android就比较悠哉用Java写所以没什么事。不过,在把度娘全身搜遍后,还是让我找到了这个库,出处记不清了,之前找了好多好多的库。下面记录下使用方法。
Object-C调用方法:
//
// ViewController.m
// AESTest
//
// Created by 杜甲 on 14-9-22.
// Copyright (c) 2014年 杜甲. All rights reserved.
//
#import "ViewController.h"
#include "NSData+AES256.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
NSString* message = @"神奇的AES";
NSString* str = [NSData AES256EncryptWithPlainText:message];
NSString* res = [NSData AES256DecryptWithCiphertext:str];
NSLog(@"%@",str);
NSLog(@"%@",res);
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
Java调用方法:
package com.test.aesforandroid;
import android.os.Bundle;
import android.app.Activity;
import android.util.Log;
import android.view.Menu;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
AES mAes = new AES();
String mString = "神奇的AES";
byte[] mBytes = null;
try {
mBytes = mString.getBytes("UTF8");
} catch (Exception e) {
// TODO: handle exception
}
String enString = mAes.encrypt(mBytes);
Log.i("aes123", enString);
String deString = mAes.decrypt(enString);
Log.i("aes123", deString);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
------------------------------------------End------------------------------------------
最后附上本文代码工程源码链接:
具体下载目录在 /2014年资料/12月/15日/Object-C与Java通用的AES加密解密
------------------------------------------分割线------------------------------------------
Ubuntu下Object-C开发环境搭建