android - Rsa algoritmn generates ? characteres in java -


i'm making android app sending safe sms. have following code (iam using 256 testing)

     public void generatekey() throws nosuchalgorithmexception, nosuchpaddingexception,                 illegalblocksizeexception,badpaddingexception,invalidkeyexception{             try{                 kpg = keypairgenerator.getinstance("rsa");                  kpg.initialize(256);                 kp = kpg.genkeypair();              }catch(exception e){                 e.printstacktrace();                 toast.maketext(getbasecontext(), e.tostring(),toast.length_long).show();              }          }         public byte[] rsaencrypt(final string plain) throws nosuchalgorithmexception, nosuchpaddingexception,                 illegalblocksizeexception, badpaddingexception, invalidkeyexception {         try {             publickey = kp.getpublic();             privatekey = kp.getprivate();              cipher = cipher.getinstance("rsa");             cipher.init(cipher.encrypt_mode, publickey);             encryptedbytes = cipher.dofinal(plain.getbytes("utf-8"));         }catch (exception e){             e.printstacktrace();             toast.maketext(getbasecontext(),e.tostring(),toast.length_long).show();         }             return encryptedbytes;         }  public void enviasms(view view) {         edittext key = (edittext) findviewbyid(r.id.publickey);         edittext phonenumber = (edittext) findviewbyid(r.id.phonenumber);         edittext text = (edittext) findviewbyid(r.id.textmessage);         string keytext = key.gettext().tostring();         string number = phonenumber.gettext().tostring();         string sms = text.gettext().tostring();         if (!keytext.equals("") && !number.equals("") && !sms.equals("")) {             try {                  byte[] encriptedsms= rsaencrypt(sms);                 log.i("teste",new string(encriptedsms));                 log.i("teste",new string(encriptedsms, "utf-8"));                 toast.maketext(getbasecontext(), new string(encriptedsms, "utf-8"), toast.length_short).show(); // ou send?:3                 smsmanager smsmanager = smsmanager.getdefault();                  smsmanager.sendtextmessage(number, null, new string(encriptedsms,"utf-8"), null, null);                  toast.maketext(getapplicationcontext(), "sms sent!", toast.length_long).show();             } catch (exception e) {                 e.printstacktrace();                 toast.maketext(getbasecontext(),e.tostring(),toast.length_long).show();             }          }     } 

when encript string "hello john doe" 2e2??????24sfe??

i'm making utf-8 strings may problem corrupted encryption?

new string(encriptedsms,"utf-8") 

your problem here. encriptedsms not contain utf-8 encoded text, wrong.

there no correct way "convert" byte array string, unless byte array contains encoded text (like somestring.getbytes("utf-8")).

however, there ways encode byte array string. base64 1 such encoding. since android, can use android.util.base64 class:

string encodedsms = base64.encodetostring(encriptedsms, base64.default) 

and decode it, like:

byte[] encriptedsms = base64.decode(encodedsms, base64.default) 

Comments

Popular posts from this blog

javascript - Bootstrap Popover: iOS Safari strange behaviour -

Magento/PHP - Get phones on all members in a customer group -

session - Logging Out Using PHP -