Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/191.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java RSA算法的密钥不适用于对称加密_Java_Android_Android Studio - Fatal编程技术网

Java RSA算法的密钥不适用于对称加密

Java RSA算法的密钥不适用于对称加密,java,android,android-studio,Java,Android,Android Studio,我使用AES加密密钥,但当我尝试对其进行解密时,它会显示错误 RSA算法的密钥不适用于对称加密 这是埃克比·曼托德 public void RSAEncrypt(String alias) throws NoSuchPaddingException, BadPaddingException, IllegalBlockSizeException, UnrecoverableEntryException, KeyStoreException, InvalidKeyException {

我使用AES加密密钥,但当我尝试对其进行解密时,它会显示错误

RSA算法的密钥不适用于对称加密

这是埃克比·曼托德

    public void RSAEncrypt(String alias) throws NoSuchPaddingException, BadPaddingException, IllegalBlockSizeException, UnrecoverableEntryException, KeyStoreException, InvalidKeyException {
        KeyStore keyStore = null;
        try {
            keyStore = KeyStore.getInstance("AndroidKeyStore");
            keyStore.load(null);
        } catch (Exception e) {
            Log.e("TAG", Log.getStackTraceString(e));
            e.printStackTrace();
        }
        String initialText = startText.getText().toString();
        if (initialText.isEmpty()) {
            Toast.makeText(this, "Enter text in the 'Initial Text' widget", Toast.LENGTH_LONG).show();

        }
        try {
            KeyStore.PrivateKeyEntry privateKeyEntry = (KeyStore.PrivateKeyEntry) keyStore.getEntry(alias, null);
            RSAPublicKey publicKey = (RSAPublicKey) privateKeyEntry.getCertificate().getPublicKey();
            KeyGenerator keyGen = KeyGenerator.getInstance("AES");
            keyGen.init(128);
            SecretKey sessionKey = keyGen.generateKey();

            Cipher rsaCipher = Cipher.getInstance("RSA");
            rsaCipher.init(Cipher.ENCRYPT_MODE, publicKey);
            byte[] encryptedSessionKey = rsaCipher.doFinal(sessionKey.getEncoded());

// 3. Encrypt the data using the session key (unencrypted)
            Cipher aesCipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
            aesCipher.init(Cipher.ENCRYPT_MODE, sessionKey);
            byte[] encryptedBytes = aesCipher.doFinal(initialText.getBytes());
            System.out.println("EEncrypted?????" + encryptedBytes);
//
            encryptedText.setText((Base64.encodeToString(encryptedBytes, Base64.DEFAULT)));
            RSADecrypt(encryptedBytes, alias);
//            decryptString(alias);
        } catch (NoSuchAlgorithmException e) {
            Toast.makeText(this, "Exception " + e.getMessage() + " occured", Toast.LENGTH_LONG).show();
            Log.e("TAG", Log.getStackTraceString(e));
            e.printStackTrace();
        }

    }
这是递减法

    public void RSADecrypt(final byte[] encryptedBytes, String alias) throws NoSuchAlgorithmException, NoSuchPaddingException,
            InvalidKeyException, IllegalBlockSizeException, BadPaddingException {

        KeyStore keyStore = null;
        try {
            keyStore = KeyStore.getInstance("AndroidKeyStore");
            keyStore.load(null);
        } catch (Exception e) {
            Log.e("TAG", Log.getStackTraceString(e));
            e.printStackTrace();
        }
        String initialText = startText.getText().toString();
        if (initialText.isEmpty()) {
            Toast.makeText(this, "Enter text in the 'Initial Text' widget", Toast.LENGTH_LONG).show();

        }
        try {
            KeyStore.PrivateKeyEntry privateKeyEntry = (KeyStore.PrivateKeyEntry) keyStore.getEntry(alias, null);
            RSAPublicKey publicKey = (RSAPublicKey) privateKeyEntry.getCertificate().getPublicKey();
            KeyGenerator keyGen = KeyGenerator.getInstance("AES");
            keyGen.init(128);
            SecretKey sessionKey = keyGen.generateKey();

            Cipher rsaCipher = Cipher.getInstance("RSA");
            rsaCipher.init(Cipher.ENCRYPT_MODE, publicKey);
            byte[] encryptedSessionKey = rsaCipher.doFinal(sessionKey.getEncoded());

// 3. Encrypt the data using the session key (unencrypted)
            Cipher aesCipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
            aesCipher.init(Cipher.DECRYPT_MODE, privateKeyEntry.getPrivateKey());
            byte[] decryptedBytes = aesCipher.doFinal(encryptedBytes);

            String decrypted = new String(decryptedBytes);
            System.out.println("DDecrypted?????" + decrypted);
            decryptedText.setText(decrypted);
        } catch (Exception e) {
            Toast.makeText(this, "Exception Ddecrpt" + e.getMessage() + " occured", Toast.LENGTH_LONG).show();
            Log.e("TAG", Log.getStackTraceString(e));
            e.printStackTrace();
        }

    }
所以,如果有人对衰老有想法,请给我一个帮助