I'm encrypting using a bouncy castle. I RSA and is stored in the I am using public key PEM file I think that when I run the code in simple console project (Android project), then everything works fine - which means that the private key encrypted string Can be decrypted, however, when I run the same code in the Android app, the encrypted byte array is for the same public key. A is different and is not recognized as valid encryption went the key pair.
Description : Here is the code that uses the booming castle to extract the string from the password and the console project. It does one thing and produces an encrypted string which is valid encryption for the key pair and it can be decrypted.
private static void encrypt (try {// Castle library Security.addProvider bouncy In this example (new BouncyCastleProvider ()); String plaintext = "This needs to be encrypted" string Public_key_file = "PublicKeykpem", // load parse public key PEMParser = new PEMParser (new FileReader (public_key_file)); object key = parser. Reedobjekt (); Parserkclose (); public Key Pbke = nULL; if (main The main installation of PublicKinfoine) {SubjectPublicKeyInfo spki = (SubjectPublicKeyInfo) key; Pubak = Kunjifractorktst Instens ( "RSA"). Generated (new X509 Ancodedkespiisi (spki.getEncoded ()));} // to encrypt the plain text Saifr Saifr = cipher. Gate instance ( "RSA"); Cipher. It ( Cypher. Ansiarwaipi_moiiii, Pbke); byte [] Ankripted_deta = cipher Kdofnl (plain text KgetBytes ()); string Ancoded_deta = new string (base 64knkod (Ankripted_deta)); System.out.println ( "encrypted value:" ); Println (encoded_data);} hold (Exception pre) {Syste M.out.println (ex.getMessage ()); }}
Now here is the code to be removed from the Android app. The only important difference is that the important file is read from the Assets folder. This code also generates an encrypted string but this key is not valid for the pair and it can not be decrypted.
Public Zero Encrypt (Reference Reference) {try // // This example uses booming Castle library security. AddProvider (new BouncyCastleProvider ()); String plain text = "It needs to be encrypted"; String public_key_file = "PublicKey.pem"; // Load Public Key Input inputstream = context.getAssets () Open (public_key_file); InputStreamReader inputStreamReader = New InputStreamReader (inputstream); // Read the input stream reader PEMParser parser = new PEMParser (inputstream reader); Object key = parser Read object (); Parser.close (); Public pub = null; If (main installation of main public keyfon) {SubjectPublicKeyInfo spki = (SubjectPublicKeyInfo) key; Pubke = keyfracker.tastinstance ("RSA"). Generated (new X509 encoded (spki.getEncoded ()); } // Encrypt plain text cipher cipher = cipher Get Instances ("RSA"); Cipher It (cipher NCRYPMYEEE, Pubke); Byte [] Encrypted_Data = Cipher .Doghan (plain text .getBytes ()); String encoded_data = new string (base 64. encode (encrypted_data)); Log D. ("MyApp", "Encrypted Value:"); Log D. ("MyApp", encoded_data); } Hold (Exception Pre) {Log D. ("MyApp", ex.getMessage ()); In both cases, these things are the same: - Public Key
- The actual code written (logging) the asset folder with read gap and Logk)
- JDK version (1.7)
- Booming Castle Libraries (BCPKX-JDK15-152.jar and BCPRO-JDK15-152.jar)
What's different: Environment is a console program and the other is the Android app.
On further investigation during debugging I saw that "Sun protection.RSAPublicKeyImpl" on running the console program. However, while running the Android app, the key provider "Examples of Com.Android Conescript.OpenSSLSApaulica" gives an example of the fact that if this problem is an encrypted byte array is different for the same plain text and public key.
Any help is greatly appreciated.
Thanks in advance, Sandeep
No comments:
Post a Comment