WAP之家:为您提供最全最新的WAP技术,CP.SP.3G等行业资讯。 WAP之家交流论坛全新开放 点击进入>>
WAP资讯 | 3G动态 | SP动态 | 运营商动态 | 内容商动态 | 制造商动态 | 论坛讨论>> 每次自动访问
WAP技术 | WAP源码 | 手机编程 | 手机源码 | 无线技术 | J2ME技术 | 手机软件 添加到收藏夹
IVR技术 | SP资料 | SMS MMS技术 | 商业方案 | IVR下载 | 书籍教程 | 工具软件 语言:繁體中文

WAP之家技术文章手机编程BlackBerryBlackBerry 应用程序开发者指南 第二卷:高级--第14章 增加对智能卡(smart card)的支持

BlackBerry 应用程序开发者指南 第二卷:高级--第14章 增加对智能卡(smart card)的支持
作者:佚名  来源:本站整理  发布时间:2008-3-15 2:26:19

作者:Confach 发表于 2006-04-28 22:28 pm
版权信息:可以任意转载, 转载时请务必以超链接形式标明文章原始出处 和作者信息.
http://www.cnblogs.com/confach/articles/388978.html

14 增加对智能卡(smart card)的支持



使用智能卡

创建一个智能卡驱动

使用智能卡

智能卡和信用卡一般大,可以安全的存储和传输敏感数据.BlackBerry设备支持下面的智能卡:

  • 普通访问卡(CAC,Common Access Card)
  • SafeNet智能卡(Datakey Model 330)

如果你使用的智能卡不是CACSageNet智能卡,你也可以利用智能卡的API编写一个BlackBerry设备驱动来支持你的智能卡.智能卡API提供了一个组件库,它们在net.rim.device.api.smartcard 包中,用来和智能卡以及智能卡读卡器进行交互.

对智能卡API恰当实现了的驱动,可以和BlackBerry设备上已经启动的S/MIME一起工作.它包括从智能卡导入认证,以及对卡进行私有键操作(签名和解密消息)
  :为获取关于S/MIME的更多信息,参看带有S/MIME支持包的BlackBerry 

创建一个智能卡驱动

为创建一个智能卡驱动,完成下面的步骤:

1.    扩展CryptoSmartCard,实现一个新的智能卡类.

2.    在新的智能卡里,实现libmain()方法,在启动的时候调用它.

3.    扩展CryptoSmartCardSession,未智能卡类实现一个新的对话.

4.    扩展Crypto(token),RSA, DSA, ECC操作实现一个环。

      5.    存储私有键文件位置.
       :为得到更多关于net.rim.device.api.crypto包的信息,参看API参考.

实现一个智能卡

扩展抽象类CryptoSmartCard.此类的扩展允许子类作为一个智能卡驱动在SmartCardFactory里注册.子类必须实现下面SmartCardCryptoSmartCard抽象类的方法:

SmartCard 方法

描述

openSessionImpl(SmartCardReaderSession)

建立一个和智能卡的通信.

checkAnswerToResetImpl(AnswerToReset)

验证智能卡和一个指定的ATR的兼容性.

displaySettingsImpl(Object)

允许智能卡驱动显示设置或属性.

isDisplaySettingsAvailableImpl(Object)

描述驱动是否支持显示设置.

getCapabilitiesImpl()

获取智能卡的能力.

getLabelImpl()

获取智能卡的类型.

toString()

得到智能卡的字符串描述.

 

CryptoSmartCard 方法

描述

getAlgorithms()

得到智能卡支持的所有算法名.例如“RSA”, “DSA”

getCryptoToken(String)

获取一个支持给定算法的加密环.

代码实例

.下面的实例描述了通过扩展CryptoSmartCard,如何创建一个智能卡对象.


: MyCryptoSmartCard.java

 /**

* MyCryptoSmartCard.java

* Copyright (C) 2001-2005 Research In Motion Limited. All rights reserved.

*/

 

package com.rim.samples.device.smartcard;

import net.rim.device.api.smartcard.*;

import net.rim.device.api.util.*;

import net.rim.device.api.crypto.*;

import net.rim.device.api.ui.component.*;

 

/**

* This class represents a kind (or model or family) of a physical smart card.

* There should only be one instance of this class in the system at one time. The instance

* is managed by the SmartCardFactory.

*/

public class MyCryptoSmartCard extends CryptoSmartCard implements Persistable

{

    private final static byte MY_ATR [] = {

       (byte)0x3b, (byte)0x7d, (byte)0x11,

       (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x31,

       (byte)0x80, (byte)0x71, (byte)0x8e, (byte)0x64,

       (byte)0x86, (byte)0xd6, (byte)0x01, (byte)0x00,

       (byte)0x81, (byte)0x90, (byte)0x00 };

   

    private final static AnswerToReset _myATR = new AnswerToReset( MY_ATR );

    private static final String LABEL = "RIM Sample";

    private static final String DISPLAY_SETTINGS = "Show driver properties/settings now";

    private static final String RSA = "RSA";

   

    /**

     *A smart card factory’s list of known smart cards.

     *Called on startup of the device. Register this driver with the

     */

   

    public static void libMain( String args[] )

    {

       SmartCardFactory.addSmartCard( new MyCryptoSmartCard() );

    }

   

    /**

     *Retrieve the session handler for this smart card.

     *Implementations of this method should not bring up UI.

     */

   

    protected SmartCardSession openSessionImpl( SmartCardReaderSession readerSession )

          throws SmartCardException {

       return new MyCryptoSmartCardSession( this, readerSession );

       }

   

    /**

     * Determine if the file system should use this smart card object

     * to communicate with a physical smart card.

     * that has the given AnswerToReset.

     * The system invokes this method to ascertain which smart card implementation it should

     * use to communicate with a physical smart card found in a reader.

     */

    protected boolean checkAnswerToResetImpl( AnswerToReset atr )

    {

       return _myATR.equals( atr );

    }

   

    /**

     *Retrieve a label associated with this smart card.

     *The string should not include the words "smart card", as the file system uses this

     *this method to generate strings such as "Please insert your smart card".

     */

   

    protected String getLabelImpl()

    {

       return LABEL;

    }

   

    /**

     * Retrieves this smart card’s capabilities

     */

    protected SmartCardCapabilities getCapabilitiesImpl()

    {

       return new SmartCardCapabilities( SmartCardCapabilities.PROTOCOL_T0 );

    }

   

    /**

     *Determine if this smart card can display its settings.

     */

    protected boolean isDisplaySettingsAvailableImpl( Object context )

    {

       return true;

    }

   

    /**

     *Display this smart card’s settings.

     *This method will be invoked from the smart card options screen when

     *the user selects the driver and chooses to view the settings of that driver.

     *

     *This method could be called from the event thread. The driver should not block

     *the event thread for long periods of time.

     *

     *@param context Reserved for future use.

     **/

    protected void displaySettingsImpl( Object context )

    {

       Dialog.alert( DISPLAY_SETTINGS );

    }

   

    /**

     * Retrieve the algorithms supported by this smart card.

     *

     * @return one of "RSA", "DSA", or "ECC"

     */

    public String[] getAlgorithms()

    {

       return new String [] { RSA };

    }

   

    /** Retrieve a crypto token that supports the given algorithm.

     *  @param algorithm Name of the algorithm.   * 

     *  @return Crypto Token supporting the named algorithm.

     *  @throws NoSuchAlgorithmException If the specified algorithm is invalid.

     *  @throws CryptoTokenException If there is a token-related problem.

     */

    public CryptoToken getCryptoToken( String algorithm )

    throws NoSuchAlgorithmException, CryptoTokenException

    {

       if ( algorithm.equals( RSA