2014年11月21日 星期五

Android 匯入 使用者憑證的方式

http://networklessons.com/wireless/eap-tls-certificates-for-wireless-on-android/

EAP-TLS Certificates for Wireless on Android


In this tutorial I want to demonstrate to you how to install a user certificate on an Android device so that you can authenticate to a wireless network using EAP-TLS. This is the most secure method of authentication when it comes to wireless networks but it requires some more effort as you require certificates on the server and each client device.
If you don’t have a RADIUS server and Certificate Authority yet then you should take a look at my PEAP and EAP-TLS on Windows Server 2008 tutorial. In this tutorial I will be using a Windows Server 2008 machine running Certificate Services to generate a client certificate for my Android device.
There are three devices that we require for this tutorial:
  • Android Device: I’m using my Samsung Galaxy S3 running Android 4.x
  • Certificate Authority: required to generate the user certificate. I’m using Windows Server 2008 R2.
  • Windows computer: Used to request the user certificate for the Android device. I will be using Windows 7 in my example.
And there are two certificates that we require:
  • User certificate.
  • Root CA certificate.
The user certificate is required to authenticate the user, the root CA certificate is required in case you created your own certificate authority. We’ll start with the creation of a user certificate.

Generate User Certificate on Windows 7

First we will generate a user certificate for the Android device. I will use Windows 7 to connect to the Certificate Authority and request a user certificate. I will use Web enrollment for this as it’s probably the most convenient method of enrolling user certificates.
Open the web browser and open http://your-server-ip/certsrv. Enter the credentials of the user account that requires a certificate. I’m doing this for a user called “Android”.
Windows 7 CertSRV
Click on the request a certificate button to continue.
Windows 7 CertSRV Request Certificate
There is no need for advanced options, click on user certificate to continue.
Windows 7 CertSRV User Certificate
Your web browser will warn you that you are about to request a certificate, just click on Yes to continue.
CertSRV Web Access Confirmation
In the final screen the web enrollment is ready to submit your request. Hit the submit button to continue.
Windows 7 CertSRV Identifying Information
Your user certificate will be ready and the web browser will ask if it should install the user certificate for you. Click on Install this Certificate to continue.
CertSRV Install Certificate
If everything went OK you should get a message that the user certificate has been installed.
CertSRV Certificate Installed
Now we have a user certificate on our Windows 7 computer. We will export this user certificate and import it to our Android device.

Export Certificates

Our Android device will require the user certificate that we just generated but also the root CA certificate in case you are running your own Certificate Authority. I will use my Windows 7 computer to export both certificates. We’ll start with the user certificate…

Export User Certificate

We will use the certificate manager on Windows 7 to view our certificates and export them.
Click on Start > type “CERTMGR.MSC” (without the quotes) and hit enter.
Windows 7 Start Menu Certmgr
The Certificate manager will start. Click on Personal > Certificates and you will see the user certificate that we generated for the Android user.
Windows 7 Certmgr User Certificate
Click on the Android user certificate (right mouse click) and select Export.
Windows 7 Certmgr Export User Certificate
You will see the certificate export wizard, click Next to continue.
Windows Server 2008 Certificate Export Wizard
The wizard will ask you to export the private key for the user certificate. Select Yes, export the private key and click Next to continue.
Windows 7 Export Private Key
In the next screen you will have to choose the file format. Everything is greyed out since this is a user certificate. Make sure you select the “Include all certificates in the certification path if possible” and “Export all extended properties” checkboxes. Click Next to continue.
Windows 7 Personal Information Exchange
The export wizard will ask you to protect the private key with a password. This is a good idea so make up a secure password and enter it. Click Next to continue.
Windows 7 Private Key Password
The export wizard will ask you for a filename. I’ll name my user certificate “Android”. Click Next to continue.
Windows 7 User Certificate Export Filename
Finally the export is done. Click Finish to continue.
Windows 7 User Certificate Export Completed
Now we have the user certificate for user Android stored in a file. We still will need the root CA certificate…

Export Root CA Certificate

If you don’t have the root CA certificate yet you will have to export it from the certificate authority or from your Windows 7 computer. I already covered how to export the root CA in my other tutorial for installing PEAP and EAP-TLS on Windows Server 2008. Take a look at the Configure Wireless Client section to see how you can export the root CA to a filename.
Once you are done you should have two files:
  • The user certificate in PFX file format.
  • The root CA certificate in DER file format.
We are now ready to import the certificates on our Android device.

Import Certificates on Android Device

Most Android devices will only import certificates from an SD card (internal or external). I am using a Samsung Galaxy S3 running Android 4.x but importing the certificates should be similar on other Android 4.x devices.
Some older Android versions might only recognize certain file formats. If your certificate are not recognized, try renaming the “cer” files to “crt” and the “pfx” file to “p12″.
First you need to copy the two certificate files to your Android device. In my case I had to copy it to the internal storage but it’s possible that you need to copy it to an external SD card on other Android devices. Whatever storage “device” it is, you need to copy the files to the rootdon’t use any sub-folders. Here’s an example of where I stored the certificate files on my Samsung Galaxy S3:
Android Galaxy S3 Certificate Files
Above you can see that I stored the following two files on sdcard0 (despite the name, this is the internal storage of the phone):
  • Android.pfx (user certificate).
  • RootCA.cer (root CA certificate).
Once you have the files in place you import them. Go to the settings of your Android device.
Android 4 Settings Button
Select the Security option from the Settings menu.
Android Security Settings
At the bottom you will find the Credentials storage. Select Install from storage device.
Android Install from Device Storage
Your Android device will find the certificate files on your storage device and prompt you with the following screen.
Android Pick a Certificate
We need to install both, let me give you an example of the user certificate. Type in the password for the private key and hit OK.
In case you get the error “No certificate file found on SD card” then A) your Android device doesn’t support the file format for the certificates that you copied or B) you copied the certificates to the wrong storage device.
Android Extract Certificate
Android will ask you for a certificate name. Keep the default or rename it to something else and press OK.
Android Certificate Name
Make sure you import both certificates. You can verify that your Android device recognizes the certificates. Select Trusted credentials if you want to take a look.
Below you can see that this Android device has a user certificate.
Android Trusted Credentials User
Our Android device now knows about the certificates that we require. Time to connect to the wireless network!

 Connect to Wireless Network using EAP-TLS

With the certificates in place we are ready to connect to the Wireless Network. Go to the Wifi settings of your android device and connect to the correct SSID. You will be prompted for some security details. Enter the following items:
  • EAP method: TLS
  • Phase 2 authentication: none
  • CA certificate: your root CA.
  • User certificate: your user certificate.
  • Identity: username
Leave the rest of the items empty and press Save.
Android EAP-TLS Profile
You should now be connected to the wireless network:
Android EAP-TLS Connected
That’s all there is to it! I hope this has been helpful to help you connect your Android devices to the Wireless network using EAP-TLS. If you have any questions feel free to leave a comment!


Read more: http://networklessons.com/wireless/eap-tls-certificates-for-wireless-on-android/#ixzz3Jh1dCEgl

2014年11月20日 星期四

基于Cocos2d-x 3.0集成物理特性的精确点击测试

http://cn.cocos2d-x.org/tutorial/show?id=1249

Cocos2d-x 3.0版本有一个基于chipmunk的集成物理特效。这非常容易用来作为Cocos2d-x游戏的精确点击测试。

源代码请点击这里下载。
1407122334769924.gif

如何运行起来?
克隆源代码,Android的复制Cocos2d文件夹下的Cocos2d-x 3.0版本,文档看起来是这样的:
1407122358136905.png
打开proj.ios_mac/PhysicsDemo.xcodeproj 来创建iOS或Mac,运行proj.android/build_native.py 来建立android项目。

我已经把编译好的文件放置在“prebuilt编译好的”文件夹中,请检查并测试。

基本原则
这里是一个开源物体躯干编辑器, 有了这个开放并强有力的工具我们就能得到一个关于精灵物理躯干的多边形边框。然后我们导入多边形边框的数据到游戏中,让物理引擎处理剩下的事情。

所有我们该做的事就是写一个解析函数,一个能够读取[box2d-editor]导出的文件并载入到cocos2d-x的物理引擎的解析函数。

我们开始吧!

1. 创建新项目
1
cocos new  -p com.example.phy -l cpp -d ~/Documents PhysicsDemo

2. 在Xcode里打开新项目(我是用的是Xcode,你可以选择你自己喜欢的编译环境)
1
open ~/Documents/PhysicsDemo/proj.ios_mac/PhysicsDemo.xcodeproj

3. 制作一张测试用的图像,我是下载了cocos2d-x 的商标,从Cocos2d-x的Logo资源将其命名为 2dx.png,把图像添加在项目里。

4. 下载box2d-editor
找到并打开jar类型文件。
注意:请使用JRE 6 来运行它。

5. 获得躯干数据
1407122525706376.png
打开编辑器,创建一个新的项目,命名为bodies.json并将其保存在项目的Resource文件夹里。

别忘了添加把bodies.json添加到项目。

创建一个新的躯干定义,并命名为2dx。
1407122557457126.png
1407122589911573.png
1407122603638874.png

使用Auto-trace按钮获得图像的边缘。
1407122634797345.png
1407122658915777.png

保存bodies.json
1407122683772434.png

接下来,我们应该写一些代码来带入bodies.json。

6. 修改你的场景
编辑方法HelloWorld::createScene 以此启用物理特效。
1
2
3
auto scene = Scene::createWithPhysics();
//enable debug draw
scene->getPhysicsWorld()->setDebugDrawMask(PhysicsWorld::DEBUGDRAW_ALL);

把我们的精灵添加到层里。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
// add "2dx.png"
    sp_2dx = Sprite::create("2dx.png");
    // position the sprite on the center of the screen
    sp_2dx->setPosition(Point(visibleSize.width/2 + origin.x, visibleSize.height/2 + origin.y));
    //load
    MyBodyParser::getInstance()->parseJsonFile("bodies.json");
    //bind physicsbody to sprite
    auto _body = MyBodyParser::getInstance()->bodyFormJson(sp_2dx, "2dx");
    if (_body != nullptr) {
        _body->setDynamic(false); //set it static body.
        _body->setCollisionBitmask(0x000000); //don't collision with anybody.
        sp_2dx->setPhysicsBody(_body);
    }
    // add the sprite as a child to this layer
    this->addChild(sp_2dx, 0);
MyBodyParser is the helper class to load bodies.json, use rapid json.
MyBodyParser 就是用来读取bodies.json的类,使用快速json。

你能在源码中查询想要的细节,关键函数是MyBodyParser::bodyFormJson。之后添加touchListener,这个过程较为简单并且没有什么值得多费口舌来讲解。

7. 确认是否我们的精灵已经被点击
在函数HelloWorld::nodeUnderTouch中:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
Node* HelloWorld::nodeUnderTouch(cocos2d::Touch *touch)
{
    Node* node = nullptr;
    //translate the touch to layer coordinate.
    auto location = this->convertTouchToNodeSpace(touch);
  
    //retrive all physics shapes under the touch location though cocos2d-x.
    auto scene = Director::getInstance()->getRunningScene();
    auto arr = scene->getPhysicsWorld()->getShapes(location);
  
    //iterate the shapes, find our sprite!
    for (auto& obj : arr)
    {
        //find it!!!!
        if ( obj->getBody()->getNode() == sp_2dx)
        {
            node = obj->getBody()->getNode();
            break;
        }
    }
    return node;
}

如果这个函数返回nullptr,这意味着你并没有碰触到精灵物理躯干的内部。

这些就是这篇教程所有的奇妙之处了,确认源代码并阅读更多的信息。

本文翻译自:《Accurate hit testing with Cocos2d-x 3.0 integrated physics feature》, 欢迎大家在CocoChina论坛中参与本文的讨论。

分享到:
赞(1)

文章评论 (2)

  • 念念不忘2014-11-06 15:57:56
    MyBodyParser类怎么在c++中导入啊 。不认识这个类