简体   繁体   English

在Java中混淆​​连接字符串

[英]obfuscate connection string in java

I've made a little program with Java and the library ojdbc14. 我已经用Java和库ojdbc14编写了一个小程序。 I've obfuscated with proguard the jar file but if I unzip the program and I check some classes I see easily the connection string of my database connection. 我对projar文件进行了模糊处理,但是如果我解压缩该程序并检查某些类,则可以轻松地看到数据库连接的连接字符串。 Someone could help to obfuscate these connection string? 有人可以帮助混淆这些连接字符串吗? Thanks and sorry for my English! 谢谢,抱歉我的英语!

One easy solution would be to base64 encode the string literal and unencode it when you need to use it. 一种简单的解决方案是对字符串文字进行base64编码,并在需要使用时对其进行未编码。 That way, it wouldn't be visible in plain text. 这样,它就不会以纯文本显示。

The Apache Commons library has a utility you can use, and it's built in to Java 8 . Apache Commons库具有您可以使用的实用程序,它内置于Java 8中

Instead of 代替

String secret = "secret";

do

String alpha = "sce"; // odd letters
String beta = "fsu"; // even letters + 1
StringBuilder secret = new StringBuilder();
for (int i = 0; i < alpha.length() + beta.length(); ++i) {
    if (i % 2 == 1) {
        secret.append(alpha.charAt(i / 2);
    } else {
        secret.append((char)(beta.charAt(i / 2) - 1);
    }
}

You get the idea. 你明白了。

I suggest you encrypt your connection string and keep the secret key outside the JAR. 我建议您连接字符串进行加密 ,并将密钥保留在JAR之外。 Make your program code contain a byte[] initialized with the encrypted bytes. 使您的程序代码包含以加密的字节初始化的byte[]

If you don't want any secret key external to the JAR, then your only option is "security through obscurity". 如果您不希望JAR外部有任何秘密密钥,那么您唯一的选择是“通过隐秘性确保安全”。 You can, for example, devise some seemingly complex routine which will produce the encryption key at runtime. 例如,您可以设计一些看似复杂的例程,该例程将在运行时生成加密密钥。 For example, seed the random number generator with a fixed seed, then take a few numbers from it. 例如,用固定种子为随机数生成器播种,然后从中获取一些数字。

A poor man's encryption approach is a simple XOR. 穷人的加密方法是简单的XOR。 You can generate the fixed pseudorandom sequence and use that to XOR each byte of your connection string: 您可以生成固定的伪随机序列,并将其用于对连接字符串的每个字节进行XOR:

byte[] password = "password".getBytes(StandardCharsets.UTF_8);
Random rnd = new Random(204862727);
for (int i = 0; i < password.length; i++)
  password[i] ^= rnd.nextInt();
// at this point password contains bytes which you would hardcode into your JAR
System.out.println("Encrypted: "+new String(password, StandardCharsets.UTF_8));
rnd = new Random(204862727);
for (int i = 0; i < password.length; i++)
  password[i] ^= rnd.nextInt();
System.out.println("Decrypted: "+new String(password, StandardCharsets.UTF_8));

On my system, this prints 在我的系统上,此打印

Encrypted: �ع�gO�
Decrypted: password

Obfuscation has the sole purpose of not letting others easily steal your code. 混淆的唯一目的是不让他人轻易窃取您的代码。 After the binary has been obfuscated, another programmer will find it more difficult to modify and resell your code, or make your program behave differently than you intended (by the way, Proguard also removes dead code, and probably makes other optimizations I cannot recall just now). 混淆了二进制文件之后,另一个程序员会发现修改和转售您的代码或使程序的行为与预期的行为更加困难(顺便说一下,Proguard还会删除无效的代码,并且可能会进行其他优化,我无法回忆起)现在)。

There's no value in hiding constants in source code. 在源代码中隐藏常量没有任何价值。 You could find billions of fancy ways, but what's the purpose? 您可以找到数十亿种奇特的方式,但是目的是什么? Once the attacker has the code, it will not inspect thousands of .class files: instead, if all she wants is seeing the connection string, she will just attach a debugger. 一旦攻击者有了代码,它就不会检查成千上万个.class文件:相反,如果她只想看到连接字符串,她将只附加一个调试器。 set a breakpoint (the JDBC stack is not obfuscated) and wait for your program to reconstruct the connection string. 设置一个断点(不会混淆JDBC堆栈),然后等待您的程序重建连接字符串。

Obfuscation is not encryption. 混淆不是加密。 If you don't want to ship the connection string with your app (maybe because it contains a secret), then just require the secret to the user interactively at boot time, or read from some location which is marked "safe". 如果您不希望将连接字符串与应用程序一起发送(可能是因为它包含机密信息),则只需在启动时以交互方式向用户要求该机密信息,或者从标记为“安全”的某个位置读取该机密信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM