最近項目中用到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------------------------------------------
最後附上本文代碼工程源碼鏈接:
免費下載地址在 http://linux.linuxidc.com/
用戶名與密碼都是www.linuxidc.com
具體下載目錄在 /2014年資料/12月/15日/Object-C與Java通用的AES加密解密
下載方法見 http://www.linuxidc.com/Linux/2013-07/87684.htm
------------------------------------------分割線------------------------------------------
Ubuntu下Object-C開發環境搭建 http://www.linuxidc.com/Linux/2010-08/28064.htm
Object-C 數組使用詳解 http://www.linuxidc.com/Linux/2014-12/110337.htm