Проблемы с отправкой RSA SecretKey из одного действия в другое - PullRequest
0 голосов
/ 04 августа 2020

Я пытаюсь сгенерировать ключ шифрования AES и использовать его для шифрования пароля в основном действии (действие входа в систему), а затем отправить тот же ключ ( SecretKey ) второму действию (подписать- up), чтобы использовать его и для шифрования. До сих пор я пытался «закодировать» ключ от SecretKey до String , чтобы иметь возможность отправить его как Intent Extra , затем закодируйте его обратно в SecretKey во втором действии. Вот код активности входа, который содержит кнопки ввода пароля и входа в систему и регистрации:

//KEY GENERATION
        var logpass = login_password.text.toString()

        val plaintext: ByteArray = logpass.toByteArray()

        val keygen = KeyGenerator.getInstance("AES")
        keygen.init(256)

        val key: SecretKey = keygen.generateKey()
        val encodedKey = getEncoder().encodeToString(key.encoded)

//ENCRYPTION
signin_butt.setOnClickListener {
       
        val cipher = Cipher.getInstance("AES/CBC/PKCS5PADDING")
        cipher.init(Cipher.ENCRYPT_MODE, key)

        val ciphertext: ByteArray = cipher.doFinal(plaintext)
        val iv: ByteArray = cipher.iv

        val logpassencripted = String(ciphertext)
//other operations.........
}

//Sending key to sign up activity
signup_butt.setOnClickListener{
            var i=Intent(this,RegAct::class.java)
            intent.putExtra("AES key",encodedKey)
            startActivity(i)
        }

Вот код в активности регистрации, которая содержит ввод пароля и кнопку регистрации (reg_butt):

//Fetching Key from Extra
val encodedKey2=intent.getStringExtra("AES key")

        val signpass= reg_password.text.toString()
        val plaintext: ByteArray = signpass.toByteArray()

//encoding key back to original format
        val decodedKey = getDecoder().decode(encodedKey2)
                // rebuild key using SecretKeySpec
        val originalKey = SecretKeySpec(decodedKey, 0, decodedKey.size, "AES")

//ENCRYPTION
reg_butt.setOnClickListener{
            val cipher = Cipher.getInstance("AES/CBC/PKCS5PADDING")
            cipher.init(Cipher.ENCRYPT_MODE, originalKey)
            val ciphertext: ByteArray = cipher.doFinal(plaintext)
            val iv: ByteArray = cipher.iv
            val logpassencripted = String(ciphertext)
//other operations.........
}

Нажатие кнопки регистрации во втором действии приводит к сбою приложения. Вот полный logcat: (я добавил System.out.println s для отображения значений переменных в журнале)

2020-08-04 13:47:01.639 21056-21056/? I/.costa.safesho: Late-enabling -Xcheck:jni
2020-08-04 13:47:01.677 21056-21056/? E/.costa.safesho: Unknown bits set in runtime_flags: 0x8000
2020-08-04 13:47:01.705 21056-21056/? D/ActivityThread: setConscryptValidator
2020-08-04 13:47:01.706 21056-21056/? D/ActivityThread: setConscryptValidator - put
2020-08-04 13:47:02.225 21056-21056/com.example.costa.safeshop I/MultiWindowDecorSupport: updateCaptionType >> DecorView@dfca87e[], isFloating: false, isApplication: true, hasWindowDecorCaption: false, hasWindowControllerCallback: true
2020-08-04 13:47:02.225 21056-21056/com.example.costa.safeshop D/MultiWindowDecorSupport: setCaptionType = 0, DecorView = DecorView@dfca87e[]
2020-08-04 13:47:02.298 21056-21056/com.example.costa.safeshop W/.costa.safesho: Accessing hidden method Landroid/view/View;->computeFitSystemWindows(Landroid/graphics/Rect;Landroid/graphics/Rect;)Z (greylist, reflection, allowed)
2020-08-04 13:47:02.301 21056-21056/com.example.costa.safeshop W/.costa.safesho: Accessing hidden method Landroid/view/ViewGroup;->makeOptionalFitsSystemWindows()V (greylist, reflection, allowed)
2020-08-04 13:47:02.471 21056-21056/com.example.costa.safeshop D/ViewRootImpl@c56b406[WelcomeAct]: setView = com.android.internal.policy.DecorView@dfca87e TM=true MM=false
2020-08-04 13:47:02.502 21056-21056/com.example.costa.safeshop D/ViewRootImpl@c56b406[WelcomeAct]: Relayout returned: old=(0,0,1080,2340) new=(0,0,1080,2340) req=(1080,2340)0 dur=9 res=0x7 s={true 491486961664} ch=true
2020-08-04 13:47:02.503 21056-21094/com.example.costa.safeshop D/OpenGLRenderer: createReliableSurface : 0x726eea1440, 0x726ee7e000
2020-08-04 13:47:02.536 21056-21094/com.example.costa.safeshop I/mali_winsys: new_window_surface() [1080x2340] return: 0x3000
2020-08-04 13:47:02.541 21056-21094/com.example.costa.safeshop W/Gralloc3: mapper 3.x is not supported
2020-08-04 13:47:02.577 21056-21094/com.example.costa.safeshop I/gralloc: Arm Module v1.0
2020-08-04 13:47:02.771 21056-21056/com.example.costa.safeshop D/ViewRootImpl@c56b406[WelcomeAct]: MSG_WINDOW_FOCUS_CHANGED 1 1
2020-08-04 13:47:02.772 21056-21056/com.example.costa.safeshop D/InputMethodManager: prepareNavigationBarInfo() DecorView@dfca87e[WelcomeAct]
2020-08-04 13:47:02.772 21056-21056/com.example.costa.safeshop D/InputMethodManager: getNavigationBarColor() -855310
2020-08-04 13:47:02.778 21056-21056/com.example.costa.safeshop D/InputMethodManager: prepareNavigationBarInfo() DecorView@dfca87e[WelcomeAct]
2020-08-04 13:47:02.778 21056-21056/com.example.costa.safeshop D/InputMethodManager: getNavigationBarColor() -855310
2020-08-04 13:47:02.778 21056-21056/com.example.costa.safeshop D/InputMethodManager: startInputInner - Id : 0
2020-08-04 13:47:02.778 21056-21056/com.example.costa.safeshop I/InputMethodManager: startInputInner - mService.startInputOrWindowGainedFocus
2020-08-04 13:47:02.796 21056-21056/com.example.costa.safeshop D/ViewRootImpl@c56b406[WelcomeAct]: MSG_RESIZED: frame=(0,0,1080,2340) ci=(0,83,0,39) vi=(0,83,0,39) or=1
2020-08-04 13:47:02.818 21056-21056/com.example.costa.safeshop D/InputMethodManager: prepareNavigationBarInfo() DecorView@dfca87e[WelcomeAct]
2020-08-04 13:47:02.818 21056-21056/com.example.costa.safeshop D/InputMethodManager: getNavigationBarColor() -855310
2020-08-04 13:47:02.818 21056-21056/com.example.costa.safeshop D/InputMethodManager: startInputInner - Id : 0
2020-08-04 13:47:07.499 21056-21056/com.example.costa.safeshop W/ActivityThread: handleWindowVisibility: no activity for token android.os.BinderProxy@e4a3dde
2020-08-04 13:47:07.516 21056-21056/com.example.costa.safeshop I/MultiWindowDecorSupport: updateCaptionType >> DecorView@960028d[], isFloating: false, isApplication: true, hasWindowDecorCaption: false, hasWindowControllerCallback: true
2020-08-04 13:47:07.516 21056-21056/com.example.costa.safeshop D/MultiWindowDecorSupport: setCaptionType = 0, DecorView = DecorView@960028d[]
2020-08-04 13:47:07.617 21056-21056/com.example.costa.safeshop I/TextInputLayout: EditText added is not a TextInputEditText. Please switch to using that class instead.
2020-08-04 13:47:07.630 21056-21056/com.example.costa.safeshop I/TextInputLayout: EditText added is not a TextInputEditText. Please switch to using that class instead.
2020-08-04 13:47:07.699 21056-21056/com.example.costa.safeshop D/ViewRootImpl@e67c003[MainActivity]: setView = com.android.internal.policy.DecorView@960028d TM=true MM=false
2020-08-04 13:47:07.700 21056-21056/com.example.costa.safeshop D/ViewRootImpl@c56b406[WelcomeAct]: MSG_WINDOW_FOCUS_CHANGED 0 1
2020-08-04 13:47:07.700 21056-21056/com.example.costa.safeshop D/InputMethodManager: prepareNavigationBarInfo() DecorView@dfca87e[WelcomeAct]
2020-08-04 13:47:07.700 21056-21056/com.example.costa.safeshop D/InputMethodManager: getNavigationBarColor() -855310
2020-08-04 13:47:07.747 21056-21056/com.example.costa.safeshop D/ViewRootImpl@e67c003[MainActivity]: Relayout returned: old=(0,0,1080,2340) new=(0,0,1080,2340) req=(1080,2340)0 dur=8 res=0x7 s={true 488983486464} ch=true
2020-08-04 13:47:07.747 21056-21094/com.example.costa.safeshop D/OpenGLRenderer: createReliableSurface : 0x726eea2480, 0x71d9afe000
2020-08-04 13:47:07.748 21056-21094/com.example.costa.safeshop I/mali_winsys: new_window_surface() [1080x2340] return: 0x3000
2020-08-04 13:47:07.878 21056-21056/com.example.costa.safeshop D/ViewRootImpl@e67c003[MainActivity]: MSG_WINDOW_FOCUS_CHANGED 1 1
2020-08-04 13:47:07.879 21056-21056/com.example.costa.safeshop D/InputMethodManager: prepareNavigationBarInfo() DecorView@960028d[MainActivity]
2020-08-04 13:47:07.879 21056-21056/com.example.costa.safeshop D/InputMethodManager: getNavigationBarColor() -855310
2020-08-04 13:47:07.885 21056-21056/com.example.costa.safeshop D/InputMethodManager: prepareNavigationBarInfo() DecorView@960028d[MainActivity]
2020-08-04 13:47:07.886 21056-21056/com.example.costa.safeshop D/InputMethodManager: getNavigationBarColor() -855310
2020-08-04 13:47:07.886 21056-21056/com.example.costa.safeshop D/InputMethodManager: startInputInner - Id : 0
2020-08-04 13:47:07.886 21056-21056/com.example.costa.safeshop I/InputMethodManager: startInputInner - mService.startInputOrWindowGainedFocus
2020-08-04 13:47:07.895 21056-21056/com.example.costa.safeshop D/InputTransport: Input channel destroyed: 'ClientS', fd=63
2020-08-04 13:47:07.902 21056-21056/com.example.costa.safeshop D/ViewRootImpl@e67c003[MainActivity]: MSG_RESIZED: frame=(0,0,1080,2340) ci=(0,83,0,39) vi=(0,83,0,39) or=1
2020-08-04 13:47:08.194 21056-21094/com.example.costa.safeshop I/mali_egl: eglDestroySurface() in
2020-08-04 13:47:08.196 21056-21094/com.example.costa.safeshop I/mali_winsys: delete_surface() [1080x2340] return
2020-08-04 13:47:08.196 21056-21094/com.example.costa.safeshop I/mali_egl: eglDestroySurface() out
2020-08-04 13:47:08.197 21056-21094/com.example.costa.safeshop W/libEGL: EGLNativeWindowType 0x726eea1450 disconnect failed
2020-08-04 13:47:08.210 21056-21056/com.example.costa.safeshop D/ViewRootImpl@c56b406[WelcomeAct]: Relayout returned: old=(0,0,1080,2340) new=(0,0,1080,2340) req=(1080,2340)8 dur=13 res=0x5 s={false 0} ch=true
2020-08-04 13:47:08.211 21056-21056/com.example.costa.safeshop D/ViewRootImpl@c56b406[WelcomeAct]: stopped(true) old=false
2020-08-04 13:47:08.220 21056-21056/com.example.costa.safeshop D/ViewRootImpl@c56b406[WelcomeAct]: dispatchDetachedFromWindow
2020-08-04 13:47:08.224 21056-21056/com.example.costa.safeshop D/InputTransport: Input channel destroyed: 'c8c04a3', fd=54
2020-08-04 13:47:13.614 21056-21056/com.example.costa.safeshop D/ViewRootImpl@e67c003[MainActivity]: ViewPostIme pointer 0
2020-08-04 13:47:13.689 21056-21056/com.example.costa.safeshop D/ViewRootImpl@e67c003[MainActivity]: ViewPostIme pointer 1
2020-08-04 13:47:13.699 21056-21056/com.example.costa.safeshop I/System.out: ciphertext= [B@3523d3
2020-08-04 13:47:13.699 21056-21056/com.example.costa.safeshop I/System.out: iv= [B@fbcbb10
2020-08-04 13:47:13.700 21056-21056/com.example.costa.safeshop I/System.out: logpassencripted= ���|��   ���$���
2020-08-04 13:47:13.727 21056-21138/com.example.costa.safeshop D/NetworkSecurityConfig: No Network Security Config specified, using platform default
2020-08-04 13:47:13.733 21056-21138/com.example.costa.safeshop I/System.out: (HTTPLog)-Static: isSBSettingEnabled false
2020-08-04 13:47:13.733 21056-21138/com.example.costa.safeshop I/System.out: (HTTPLog)-Static: isSBSettingEnabled false
2020-08-04 13:47:15.340 21056-21056/com.example.costa.safeshop D/ViewRootImpl@7f6cb63[Toast]: setView = android.widget.LinearLayout@79ef360 TM=true MM=false
2020-08-04 13:47:15.375 21056-21056/com.example.costa.safeshop D/ViewRootImpl@7f6cb63[Toast]: Relayout returned: old=(0,83,1080,2301) new=(388,2017,691,2133) req=(303,116)0 dur=22 res=0x7 s={true 488983285760} ch=true
2020-08-04 13:47:15.378 21056-21094/com.example.costa.safeshop D/OpenGLRenderer: createReliableSurface : 0x726eea1440, 0x71d9acd000
2020-08-04 13:47:15.381 21056-21094/com.example.costa.safeshop I/mali_winsys: new_window_surface() [303x116] return: 0x3000
2020-08-04 13:47:15.448 21056-21056/com.example.costa.safeshop D/ViewRootImpl@7f6cb63[Toast]: MSG_RESIZED: frame=(388,2017,691,2133) ci=(0,0,0,0) vi=(0,0,303,116) or=1
2020-08-04 13:47:18.806 21056-21094/com.example.costa.safeshop I/mali_egl: eglDestroySurface() in
2020-08-04 13:47:18.808 21056-21094/com.example.costa.safeshop I/mali_winsys: delete_surface() [303x116] return
2020-08-04 13:47:18.808 21056-21094/com.example.costa.safeshop I/mali_egl: eglDestroySurface() out
2020-08-04 13:47:18.809 21056-21094/com.example.costa.safeshop W/libEGL: EGLNativeWindowType 0x726eea1450 disconnect failed
2020-08-04 13:47:18.810 21056-21056/com.example.costa.safeshop D/ViewRootImpl@7f6cb63[Toast]: dispatchDetachedFromWindow
2020-08-04 13:47:18.821 21056-21056/com.example.costa.safeshop D/InputTransport: Input channel destroyed: 'b22a05a', fd=70
2020-08-04 13:47:19.617 21056-21056/com.example.costa.safeshop D/ViewRootImpl@e67c003[MainActivity]: ViewPostIme pointer 0
2020-08-04 13:47:19.713 21056-21056/com.example.costa.safeshop D/ViewRootImpl@e67c003[MainActivity]: ViewPostIme pointer 1
2020-08-04 13:47:19.718 21056-21056/com.example.costa.safeshop I/System.out: ciphertext= [B@535a8de
2020-08-04 13:47:19.718 21056-21056/com.example.costa.safeshop I/System.out: iv= [B@b00babf
2020-08-04 13:47:19.718 21056-21056/com.example.costa.safeshop I/System.out: logpassencripted= �>^��tu�m�U�7
2020-08-04 13:47:19.728 21056-21191/com.example.costa.safeshop I/System.out: (HTTPLog)-Static: isSBSettingEnabled false
2020-08-04 13:47:19.728 21056-21191/com.example.costa.safeshop I/System.out: (HTTPLog)-Static: isSBSettingEnabled false
2020-08-04 13:47:19.771 21056-21056/com.example.costa.safeshop D/ViewRootImpl@8db5a24[Toast]: setView = android.widget.LinearLayout@6b4898d TM=true MM=false
2020-08-04 13:47:19.782 21056-21056/com.example.costa.safeshop D/ViewRootImpl@8db5a24[Toast]: Relayout returned: old=(0,83,1080,2301) new=(388,2017,691,2133) req=(303,116)0 dur=7 res=0x7 s={true 488983592960} ch=true
2020-08-04 13:47:19.783 21056-21094/com.example.costa.safeshop D/OpenGLRenderer: createReliableSurface : 0x726eea1440, 0x71d9b18000
2020-08-04 13:47:19.784 21056-21094/com.example.costa.safeshop I/mali_winsys: new_window_surface() [303x116] return: 0x3000
2020-08-04 13:47:19.794 21056-21056/com.example.costa.safeshop D/ViewRootImpl@8db5a24[Toast]: MSG_RESIZED: frame=(388,2017,691,2133) ci=(0,0,0,0) vi=(0,0,303,116) or=1
2020-08-04 13:47:22.448 21056-21056/com.example.costa.safeshop D/ViewRootImpl@e67c003[MainActivity]: ViewPostIme pointer 0
2020-08-04 13:47:22.631 21056-21056/com.example.costa.safeshop D/ViewRootImpl@e67c003[MainActivity]: ViewPostIme pointer 1
2020-08-04 13:47:22.633 21056-21056/com.example.costa.safeshop I/System.out: AES key= fINTHdosRrymcxYI9cdLhmDjY6nurNT9q0e5vwXG+iI=
2020-08-04 13:47:22.661 21056-21056/com.example.costa.safeshop W/ActivityThread: handleWindowVisibility: no activity for token android.os.BinderProxy@7ed3189
2020-08-04 13:47:22.685 21056-21056/com.example.costa.safeshop I/MultiWindowDecorSupport: updateCaptionType >> DecorView@cdb9454[], isFloating: false, isApplication: true, hasWindowDecorCaption: false, hasWindowControllerCallback: true
2020-08-04 13:47:22.685 21056-21056/com.example.costa.safeshop D/MultiWindowDecorSupport: setCaptionType = 0, DecorView = DecorView@cdb9454[]
2020-08-04 13:47:22.730 21056-21056/com.example.costa.safeshop I/TextInputLayout: EditText added is not a TextInputEditText. Please switch to using that class instead.
2020-08-04 13:47:22.758 21056-21056/com.example.costa.safeshop I/chatty: uid=10452(com.example.costa.safeshop) identical 3 lines
2020-08-04 13:47:22.769 21056-21056/com.example.costa.safeshop I/TextInputLayout: EditText added is not a TextInputEditText. Please switch to using that class instead.
2020-08-04 13:47:22.771 21056-21056/com.example.costa.safeshop I/System.out: encodedKey2= null
2020-08-04 13:47:22.772 21056-21056/com.example.costa.safeshop I/System.out: signpass= 
2020-08-04 13:47:22.772 21056-21056/com.example.costa.safeshop I/System.out: plaintext= [B@a550b4d
2020-08-04 13:47:22.772 21056-21056/com.example.costa.safeshop D/AndroidRuntime: Shutting down VM
2020-08-04 13:47:22.775 21056-21056/com.example.costa.safeshop E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.costa.safeshop, PID: 21056
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.costa.safeshop/com.example.costa.safeshop.RegAct}: java.lang.NullPointerException: Attempt to invoke virtual method 'byte[] java.lang.String.getBytes(java.nio.charset.Charset)' on a null object reference
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3448)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3595)
        at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:83)
        at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2147)
        at android.os.Handler.dispatchMessage(Handler.java:107)
        at android.os.Looper.loop(Looper.java:237)
        at android.app.ActivityThread.main(ActivityThread.java:7814)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1068)
     Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'byte[] java.lang.String.getBytes(java.nio.charset.Charset)' on a null object reference
        at java.util.Base64$Decoder.decode(Base64.java:549)
        at com.example.costa.safeshop.RegAct.onCreate(RegAct.kt:36)
        at android.app.Activity.performCreate(Activity.java:7955)
        at android.app.Activity.performCreate(Activity.java:7944)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1307)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3423)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3595) 
        at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:83) 
        at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135) 
        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95) 
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2147) 
        at android.os.Handler.dispatchMessage(Handler.java:107) 
        at android.os.Looper.loop(Looper.java:237) 
        at android.app.ActivityThread.main(ActivityThread.java:7814) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1068) 
2020-08-04 13:47:22.801 21056-21056/com.example.costa.safeshop I/Process: Sending signal. PID: 21056 SIG: 9

Все и любые исправления, альтернативные методы, советы и рекомендации больше чем добро пожаловать, так как я в некоторой степени новичок.

...