Android.KeyStore getEntry() returning null -


i using keystore generate key encryption. working fine somehow stopped generate key. please find below code:

private void createkey() throws nosuchproviderexception,         nosuchalgorithmexception, invalidalgorithmparameterexception {      // create start , end time, validity range of key pair that's     // generated.     calendar start = new gregoriancalendar();     calendar end = new gregoriancalendar();     end.add(calendar.year, 1);      context context = sncontext.getinstance().getcontext();      // keypairgeneratorspec object how parameters key pair passed     // keypairgenerator.  fun home game, count how many classes in sample     // start phrase "keypair".     keypairgeneratorspec spec =             new keypairgeneratorspec.builder(context)                     // you'll use alias later retrieve key.  it's key key!                     .setalias(fnconstants.alias)                             // subject used self-signed certificate of generated pair                     .setsubject(new x500principal(string.format("cn=%s, ou=%s", fnconstants.alias, context.getpackagename())))                             // serial number used self-signed certificate of                             // generated pair.                     .setserialnumber(biginteger.valueof(1337))                             // date range of validity generated pair.                     .setstartdate(start.gettime())                     .setenddate(end.gettime())                     .build();       // initialize keypair generator using the intended algorithm (in example, rsa     // , keystore.  example uses androidkeystore.     keypairgenerator kpgenerator = keypairgenerator             .getinstance(fnconstants.type_rsa,                     fnconstants.keystore_provider_android);     kpgenerator.initialize(spec);      keypair kp = kpgenerator.generatekeypair(); } 

now when trying retrieve key in encrypt method, getting value of entry null. doing wrong in ks.load(null). had tested earlier , working fine.

public static string encryptdata(string inputstr) throws keystoreexception,         unrecoverableentryexception, nosuchalgorithmexception, invalidkeyexception,         signatureexception, ioexception, certificateexception, nosuchproviderexception, nosuchpaddingexception, illegalblocksizeexception, badpaddingexception {      string result = "";     keystore ks = keystore.getinstance(fnconstants.keystore_provider_android);     // weird artifact of java api.  if don't have inputstream load, still need     // call "load", or it'll crash.     ks.load(null);      // load key pair android key store     keystore.entry entry = ks.getentry(fnconstants.alias, null);      if (entry == null) {         return null;     }      publickey publickey = ks.getcertificate(fnconstants.alias).getpublickey();     byte[] encodedbytes = null;     cipher cipher = cipher.getinstance(fnconstants.transformation, fnconstants.provider);     cipher.init(cipher.encrypt_mode, publickey);     encodedbytes = cipher.dofinal(inputstr.getbytes());      result = base64.encodetostring(encodedbytes, base64.default);     return result; } 


Comments

Popular posts from this blog

javascript - Using jquery append to add option values into a select element not working -

Android soft keyboard reverts to default keyboard on orientation change -

Rendering JButton to get the JCheckBox behavior in a JTable by using images does not update my table -