首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >MCrypt类使用simcard序列号作为SecretKey

MCrypt类使用simcard序列号作为SecretKey
EN

Stack Overflow用户
提问于 2017-01-17 17:34:06
回答 1查看 196关注 0票数 0

如何读取Simcard序列号并将前16个号码用作SecretKey

代码语言:javascript
复制
........
     private String SecretKey = "0123456789abcdef";**//Read the Simcard serial number and use the first 16 numbers as a SecretKey**

       public MCrypt()
        {
            ivspec = new IvParameterSpec(iv.getBytes());

            keyspec = new SecretKeySpec(SecretKey.getBytes(), "AES");

           ................

我做错什么了??

代码语言:javascript
复制
public class MCrypt {

static char[] HEX_CHARS = {'0','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f'};

TelephonyManager telephonyManager = (TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);
private String deviceid = telephonyManager.getSimSerialNumber();


//private String deviceid = "894310210159403811111456457657845786485458";
private String serial = new String (deviceid.substring(0, 16));
private String iv = ""+serial;//Dummy iv (CHANGE IT!) 8943102101594038
private static IvParameterSpec ivspec;
private static SecretKeySpec keyspec;
private static Cipher cipher;
private static Context context;
private String SecretKey = "0123456789abcdef";//Dummy secretKey (CHANGE IT!)
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-01-18 19:26:18

好吧。现在我看到你的问题了。活动是从上下文继承的,所以当您在类getSystemService中调用MCrypt时,您确实希望调用super.getSystemService。

如果您希望在另一个类中有一个可用的上下文(如MCrypt),您可以将它作为参数传递给该类的一个方法(构造函数,您已经拥有),并保持对它的引用,等等。

将上下文参数添加到MCrypt中的构造函数中:

代码语言:javascript
复制
public class MCrypt {
Context mContext;
TelephonyManager mtelemamanger; 
    public MCrypt (Context context)//this is a constructor
    {
        mContext = context;
        mtelemamanger = (TelephonyManager) mContext.getSystemService(Context.TELEPHONY_SERVICE);
...

将以下权限添加到Androidmanifest.xml文件中。

代码语言:javascript
复制
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>

然后使用这个:

代码语言:javascript
复制
TelephonyManager telemamanger = (TelephonyManager) mContext.getSystemService(Context.TELEPHONY_SERVICE);
String deviceid = telemamanger.getSimSerialNumber();

getSimSerialNumber()函数返回一个字符串,其中包含提供程序、信息等。如果您想获得纯sim序列号,您必须得到最后14位数字。

代码语言:javascript
复制
String SecretKey = deviceid.substring(Math.max(0, deviceid.length() - 14));
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/41703488

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档