anyone can help me ??? 2013/1/10, Ines <belhouchet.i...@gmail.com>: > > > i have some problems when i try to encrypt a string with a public key > stored in res/raw thesis the encrypt method code: > > public static byte[] encryptRSA(Context mContext, byte[] message) throws > Exception { > > // reads the public key stored in a file > InputStream is = > mContext.getResources().openRawResource(R.raw.marchandpkey); > BufferedReader br = new BufferedReader(new InputStreamReader(is)); > List<String> lines = new ArrayList<String>(); > String line = null; > while ((line = br.readLine()) != null) > lines.add(line); > > // removes the first and last lines of the file (comments) > if (lines.size() > 1 && lines.get(0).startsWith("-----") && > lines.get(lines.size()-1).startsWith("-----")) { > lines.remove(0); > lines.remove(lines.size()-1); > } > > // concats the remaining lines to a single String > StringBuilder sb = new StringBuilder(); > for (String aLine: lines) > sb.append(aLine); > String keyString = sb.toString(); > Log.d("log", "keyString:"+keyString); > > // converts the String to a PublicKey instance > byte[] keyBytes = Base64.decode(keyString.getBytes("utf-8"), > DEFAULT_KEYS_DIALER); > X509EncodedKeySpec spec = new X509EncodedKeySpec(keyBytes); > KeyFactory keyFactory = KeyFactory.getInstance("RSA"); > PublicKey key = keyFactory.generatePublic(spec); > > > > byte[] encryptedText = null; > Cipher cipher = Cipher.getInstance("RSA"); > cipher.init(Cipher.ENCRYPT_MODE, key); > encryptedText = cipher.doFinal(Base64.decode(message, > DEFAULT_KEYS_DIALER)); > > return encryptedText; > } > > ` > > and here in "oncreate method" i make a call for the encrypt method: > > final String KEY_BOOK = "mes_item_to_crypt"; > byte[] first_message = KEY_BOOK.getBytes(); > byte[] second_message = null; > try { > second_message=encryptRSA(getApplicationContext(), first_message); > Toast.makeText(getApplicationContext(), > ""+second_message.toString(), Toast.LENGTH_SHORT).show(); > } catch (Exception e1) { > // TODO Auto-generated catch block > e1.printStackTrace(); > } > > i got this in the logcat screen: > > * 01-10 01:03:33.620: W/System.err(672): > java.security.spec.InvalidKeySpecException: > java.lang.IllegalArgumentException: Bad sequence size: 301-10 01:03:33.620: > W/System.err(672): at > org.bouncycastle.jce.provider.JDKKeyFactory.engineGeneratePublic(JDKKeyFactory.java:92)01-10 > 01:03:33.630: W/System.err(672): at > org.bouncycastle.jce.provider.JDKKeyFactory$RSA.engineGeneratePublic(JDKKeyFactory.java:396)01-10 > 01:03:33.630: W/System.err(672): at > java.security.KeyFactory.generatePublic(KeyFactory.java:177)01-10 > 01:03:33.630: W/System.err(672): at > fsg.mpssri.Megabook.Show.MainLivres.encryptRSA(MainLivres.java:441)01-10 > 01:03:33.630: W/System.err(672): at > fsg.mpssri.Megabook.Show.MainLivres.onCreate(MainLivres.java:168)* > > and this's my public key stored in res/raw > > -----BEGIN > CERTIFICATE-----MIIDuDCCAqACCQDxZX5jygcxvzANBgkqhkiG9w0BAQUFADCBnTELMAkGA1UEBhMC > VE4xEDAOBgNVBAgMB1RVTklTSUUxDTALBgNVBAcMBFNGQVgxFTATBgNVBAoMDEluZXNDb21wYWdueTETMBEGA1UECwwKU2VydmV1cldlYjEXMBUGA1UEAwwOaW5lc2Rv > bWFpbmUudG4xKDAmBgkqhkiG9w0BCQEWGWJlbGhvdWNoZXQuaW5lc0BnbWFpbC5j > b20wHhcNMTMwMTA5MTQwMzE1WhcNMTQwMTA5MTQwMzE1WjCBnTELMAkGA1UEBhMC > VE4xEDAOBgNVBAgMB1RVTklTSUUxDTALBgNVBAcMBFNGQVgxFTATBgNVBAoMDEluZXNDb21wYWdueTETMBEGA1UECwwKU2VydmV1cldlYjEXMBUGA1UEAwwOaW5lc2Rv > bWFpbmUudG4xKDAmBgkqhkiG9w0BCQEWGWJlbGhvdWNoZXQuaW5lc0BnbWFpbC5j > b20wggEiMA0GCSqGSIb3DQEBAQUAA4IBDwAwggEKAoIBAQDtGJbgWuUOyQ4KgfbbVsPO+5/xnBSZlPb7CnW/IHfibcQ+29EALFVp/cpZfn4WdPtQGjSU7cFY7wcoOLnl > vDWB5B4JLJHiUnTnvQT3GA1toJFkj0wqOgggfpq/ia5wCREqd7JnVfCJ/kA77M4e > fl5ArmObwe1XQy+DRYk67rx1NZ2c2eiEcwUj/v+puF+xJzVpdCg93dbEhTf9A7t2 > loqESsBiX2CVaMtLLUK3y+g8etKL51m/PZHVXDMwJTkoje9X3cjh133YuxScMqVm > jL40p1/vHAhtkVbnned0yjLPKLH+pCI4mB285WCY7Tn6UupBZG4ttzHFdQpdq0E4UNsFAgMBAAEwDQYJKoZIhvcNAQEFBQADggEBANRPF1TcksXetiscM6qxd9CmgV9/ > atkLghMNd/EU6H3E8HB1WPhvo9bdTO+uz1lUJW3vrDpOhp/cU+P1k9cplD8k9Mml5EK0sJbS63Gl7J3h7U9QDwQ9EIU5eOanhi19wBs775LU6aqXXDE/nzE+XSoEvV0AIMlmqd7Yo0VRqqdD0Pg9ih7MD583ApnRDXzxzvbL6ioHnRyrrtWPfMtA0EqbcwsgPovq7vL7jdqgx4HD/a8mlvKvM4y4khZox4wr8uJq/KtcbFy4BRsk7BwQVcssAyDRLXNr/u/mb7fhMj9fTNh2GGbNPjspvctAp3WIlkZeSLFZhA/QolutsnJ2nKc=-----END > CERTIFICATE----- > > please help me with any idea,i just want to encrypt a string on android > with a public_key ,this public key i make it in the web server side (php). > my point is to encrypt a string in android with the web server public key > and after that i will decrypt that string in php (server side ). please i > need your help . > thanks > <http://stackoverflow.com/questions/tagged/android> > > -- > You received this message because you are subscribed to the Google > Groups "Android Developers" group. > To post to this group, send email to android-developers@googlegroups.com > To unsubscribe from this group, send email to > android-developers+unsubscr...@googlegroups.com > For more options, visit this group at > http://groups.google.com/group/android-developers?hl=en
-- You received this message because you are subscribed to the Google Groups "Android Developers" group. To post to this group, send email to android-developers@googlegroups.com To unsubscribe from this group, send email to android-developers+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/android-developers?hl=en