如何在Android应用程序中保护秘密字符串?

在我的Android应用程序中,我使用Microsoft转换器,它需要两个字符串,clientId和clientSecret。 目前,我硬编码了这两个字符串。 由于我发现classes.dex可以转换为jar,然后.class文件也可以转换为.java文件,我认为硬编码那些合理的字符串并不是一件好事。

所以我的问题很简单:如何隐藏恶意人员的字符串?

谢谢

预加密String并将其存储在资源文件中。 用钥匙解密它。 它只是通过默默无闻的安全,但至少“秘密”不会是纯文本。

public class KeyHelper { /** * Encrypt a string * * @param s * The string to encrypt * @param key * The key to seed the encryption * @return The encrypted string */ public static String encode(String s, String key) { return base64Encode(xorWithKey(s.getBytes(), key.getBytes())); } /** * Decrypt a string * * @param s * The string to decrypt * @param key * The key used to encrypt the string * @return The unencrypted string */ public static String decode(String s, String key) { return new String(xorWithKey(base64Decode(s), key.getBytes())); } private static byte[] xorWithKey(byte[] a, byte[] key) { byte[] out = new byte[a.length]; for (int i = 0; i < a.length; i++) { out[i] = (byte) (a[i] ^ key[i % key.length]); } return out; } private static byte[] base64Decode(String s) { try { return Base64.decode(s); } catch (IOException e) { throw new RuntimeException(e); } } private static String base64Encode(byte[] bytes) { return Base64.encodeBytes(bytes).replaceAll("\\s", ""); } } 

另请注意,此示例要求您在项目中包含Base64类:)