如何安全地将数据从android mobile发送到android tv?

如何安全地将数据从android mobile发送到android tv?,android,encryption,android-tv,google-nearby,Android,Encryption,Android Tv,Google Nearby,我有一个android移动应用程序,它是一个NVR软件的客户端。它使用基本身份验证和api密钥进行服务器身份验证。我现在正在为android tv构建一个应用程序。我正在添加一个允许使用移动设备进行身份验证的功能。为此,我使用谷歌图书馆,电视成为广告商,手机成为发现者。我能够将数据(凭证)从手机发送到电视,没有任何问题。但是,我想确保数据的安全。我曾想过使用加密保护数据,并已读取数据,但存在诸如“javax.crypto.AEADBadTagException:data too short”或“

我有一个android移动应用程序,它是一个NVR软件的客户端。它使用基本身份验证和api密钥进行服务器身份验证。我现在正在为android tv构建一个应用程序。我正在添加一个允许使用移动设备进行身份验证的功能。为此,我使用谷歌图书馆,电视成为广告商,手机成为发现者。我能够将数据(凭证)从手机发送到电视,没有任何问题。但是,我想确保数据的安全。我曾想过使用加密保护数据,并已读取数据,但存在诸如“javax.crypto.AEADBadTagException:data too short”或“javax.crypto.BadPaddingException:pad block corrupted”之类的问题。我认为问题是因为我使用了一个短字符串(6个字符)作为密钥

我要做的是让电视生成一个随机的6位数代码,并在屏幕上显示出来。现在在移动应用程序上,我要求用户输入6位代码。然后,我使用此代码加密凭据。当电视接收到数据时,我需要使用相同的数字代码对其进行解密

这是我的加密和解密代码。现在不行

public class DoCrypto {

    private static final String ENCRYPT_ALGO = "AES/GCM/NoPadding";
    private static final int TAG_LENGTH_BIT = 128;
    private static final int IV_LENGTH_BYTE = 12;
    private static final int AES_KEY_BIT = 256;

    private static final Charset UTF_8 = StandardCharsets.UTF_8;

    public static String encrypt(String clearText, String code) throws Exception {

        KeySpec keySpec = new PBEKeySpec(code.toCharArray(), code.getBytes(UTF_8), 1000, 256);
        SecretKey secretKey = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1").generateSecret(keySpec);
        return new String(encrypt(clearText.getBytes(), secretKey));

    }

    public static String decrypt(String encryptedText, String code) throws Exception {

        KeySpec keySpec = new PBEKeySpec(code.toCharArray(), code.getBytes(UTF_8), 1000, 256);
        SecretKey secretKey = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1").generateSecret(keySpec);
        return decrypt(encryptedText.getBytes(), secretKey);
    }

    public static byte[] encrypt(byte[] pText, SecretKey secret) throws Exception {

        Cipher cipher = Cipher.getInstance(ENCRYPT_ALGO);
        cipher.init(Cipher.ENCRYPT_MODE, secret);
        byte[] encryptedText = cipher.doFinal(pText);
        return encryptedText;

    }

    public static String decrypt(byte[] cText, SecretKey secret) throws Exception {

        Cipher cipher = Cipher.getInstance(ENCRYPT_ALGO);
        cipher.init(Cipher.DECRYPT_MODE, secret);
        byte[] plainText = cipher.doFinal(cText);
        return new String(plainText, UTF_8);

    }

}
我正在测试这个

    try {
        String code = DoCrypto.encrypt("Clear Text","1234");
        Log.d("crypt", code);
        Log.d("crypt", DoCrypto.decrypt(code, "1234"));
    } catch (Exception e) {
        e.printStackTrace();
    }
这段代码给出了上面提到的错误“javax.crypto.AEADBadTagException:数据太短”。所以,也许我在想我可能做得不对,因为我只使用了一个4-6位数的密钥

是否有更好的方法来保护数据

更新#1:


经过一些修补,我现在得到了错误“javax.crypto.AEADBadTagException:mac签入GCM失败”。目前正在谷歌上搜索以了解原因。

附近的连接为这两台设备提供了一个身份验证令牌()。只要在发送敏感数据之前确认,实际连接和数据传输将是安全的

大多数客户端只是在两个屏幕上显示令牌,并有一个确认步骤。有些人将其散列(成一个数字或一系列图像)以使其更易于阅读