Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Hi, Java begginer class. I wonder if anyone can help me see my error in the code? Chat Encrypt/Decrypt. import android.util.Log; import java.security.Key; import javax.crypto.Cipher;
Hi, Java begginer class. I wonder if anyone can help me see my error in the code? Chat Encrypt/Decrypt.
import android.util.Log; import java.security.Key; import javax.crypto.Cipher; import javax.crypto.spec.SecretKeySpec; // Encrypt and decrypt string. public class EncDecrypt { String key = "abcdefghijklmnop"; private String s; public String encrypt (String unencryptedText){ String encryptedText = ""; try{ Key aesKey = new SecretKeySpec(key.getBytes(), "AES"); Cipher cipher = Cipher.getInstance("AES"); cipher.init(Cipher.ENCRYPT_MODE, aesKey); byte[] encrypted = cipher.doFinal(unencryptedText.getBytes()); //convert from byte array to hex StringBuilder StringBuilder sb = new StringBuilder(); for (byte b : encrypted){ sb.append(String.format("%02x", b)); } encryptedText = sb.toString(); Log.d("MyMsg", "Encrypted text: " + encryptedText); } catch (Exception e){ Log.d("MyMsg", "In exception " + e); } return encryptedText; } // end method encrypted public String decrypt (String encryptedText){ String decryptedText = ""; byte[] encrypted = hexStringToByteArray(encryptedText); try { Key aesKey=new SecretKeySpec(key.getBytes(), "AES"); Cipher cipher=Cipher.getInstance("AES"); cipher.init(Cipher.DECRYPT_MODE, aesKey); decryptedText=new String(cipher.doFinal(encrypted)); Log.d("MyMsg", "Decrypted text: " + decryptedText); } catch (Exception e){ Log.d("MyMsg", "In exception " + e); } return decryptedText; } // end method decrypt // Helper method to convert String to byte array. protected byte[] hexStringToByteArray(String s) { this.s=s; int len=s.length(); byte[] data=new byte[len/2]; for (int i=0; i byte ) ((Character.digit(s.charAt(i), 16) digit(s.charAt(i + 1), 16)); } return data; } // end helper function04-05 03:29:33.786 4448-4484/com.positiveid.mobilechatrevised D/MyRecMsg: FetchPostsTask AsyncTask 04-05 03:29:33.787 4448-4484/con.positive?d.mobilechatrevised D/MyRecMsg : url = http ./testchat . ri ewater.net/ e ostaandroid 04-05 03:29:33.787 4448-4484/com.positiveid.mobilechatrevised D/NetworkSecurityConfig: No Network Security Config specified, using platform default 04-05 03:29:33.851 4448-4484/com.positiveid.mobilechatrevised D/MyRecMsg: Connection was made 04-05 03:29:34.023 4448-4484/com.positiveid.mobilechatrevised D/MyMg: In exception javax.crypto.IllegalBlocksizeException: last block incomplete in decryption In exception Javax.crypto.IllegalBlocksizeException: last block incomplete in decryption 04-05 03:29:34.024 4448-4484/com.positiveid.mobilechatrevised D/MyMag: In exception Javax.crypto. Iilega1BlocksizeException: last block incomplete in decryption 04-05 03:29:34.029 4448-4484/com.positiveid.mobilechatrevised D/MyMsg: In exception javax.crypto.Illegal1Block5izeException: last block incomplete in decryption 04-05 04-05 03:29:34.030 03:29:34.035 4448-4484/com.positiveid.mobilechatrevised D/MyRecMsg; Connection ?? closed 4448-4484/com . positive id.mobilechatrevised E/AndroidRuntime ; FATAL EXCEPTION; AsyncTask #1 Process: com.positiveid.nobilechatrevised, PID: 4448 java.lang.RuntineException: An error occurred while executing doInBackground () at android.os.Asynclask 3.done ( at java.util.concurrent.FutureTask.finishCorpletion (EutureTask.java:354) at java.util.concurrent.FutureTask.setException (EutureTask.java:223) at java.util.concurrent.FutureTask.run (EutureTask.java: 242 at android.os.AsynclaskeSerialExecutorl.run cTask.java:243) at java.util.concurrent.ThreadPoolExecutor.runHorker (ThreadPoo1Executor.java:1133) at java.util.concurrent.ThreadPoolExecutorfWorker.run (ThreadPoolExecutor.java: 607) at java. lang.Thread.run (Thread.java:761) Caused by: java.lang.StringIndexOutofBoundsException: length-33 index-33 at java.lang.String.charAt (Native Method) at com.positiveid.mobilechatrevised.Encdecrypt.hexstringToByteArray EncDecrypt.java:60) at com.positiveid.mobilechatrevlsed. EncDecrypt.decrypt (Encbecrypt.Java:40 at com.positiveid.mobilechatrevised.MainActivity FetchPostsTask.doInBackground (Mainactivity.1ava: 261) at ???.positiveld.mobilechatrevised . MainActivitySFetchPostsTask.dolnBackground (HairActivity. java: 211) at android.os.AsyncTask$2.call (AsyncTask.java:305) at java.util.concurrent. FutureTask.run (FutureTask.java:237) 04-05 03:29:34.50 4448-4480/com.positiveid.mobilechatrevised D/EGL_enmlation: eglMakeCurrent: 0x9d29e700: ver 2 0 (tinfo 0x9d289630) 04-05 03:29:34.801 4448-4448/com.positiveid.mobilechatrevised D/MyRecMsg: myFetchPostsTimerRunnable 04-05 03:29:34.801 4448-4500/com.positiveid.mobilechatrevised D/MyRecMsg: FetchPostsTask AsyncTask
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started