Java 串行钥匙:怎么办?

Java 串行钥匙:怎么办?,java,Java,我在这里读了一些帖子和网络上的文章,但我无法为我的应用程序描绘一个基于串行键的系统 我读过这篇文章,但我不能将代码转换成Java,而且我也不太熟悉这些术语 你能给我什么样的见解?理想情况下,我的应用程序将出售,但我不期望它会很受欢迎,如果我有用户欣赏并购买该产品,我不介意它被破解,但我希望避免它被轻易破解。请尽可能具体,我对Java有些陌生 提前感谢。保护应用程序通常不是一项简单的任务。许多公司正在投入大量资金寻找新的安全算法,这些算法很快就会被破解 保护Java应用程序有点困难。嵌入在应用程

我在这里读了一些帖子和网络上的文章,但我无法为我的应用程序描绘一个基于串行键的系统

我读过这篇文章,但我不能将代码转换成Java,而且我也不太熟悉这些术语

你能给我什么样的见解?理想情况下,我的应用程序将出售,但我不期望它会很受欢迎,如果我有用户欣赏并购买该产品,我不介意它被破解,但我希望避免它被轻易破解。请尽可能具体,我对Java有些陌生


提前感谢。

保护应用程序通常不是一项简单的任务。许多公司正在投入大量资金寻找新的安全算法,这些算法很快就会被破解

保护Java应用程序有点困难。嵌入在应用程序中的任何串行验证算法都可以反编译,因此串行密钥生成器将非常容易构建

一个好的起点是你的文章。它告诉您如何构建密钥验证系统,以及如何为您的(合法)用户生成密钥

在实现了这样一个算法之后,我建议您稍微保护一下源代码,这样反编译就变得有点“棘手”。使用代码混淆技术隐藏验证算法实现。这也会使那些试图通过修改字节码来破解你的应用程序的人更加困难

一个好的技术可能是在远程服务器上导出密钥验证算法。客户端将密钥发送到服务器,服务器用“验证代码”进行回复,以告知应用程序您的密钥有效。但这并不阻止用户修改您的应用程序以删除任何密钥验证过程。对于没有24小时互联网连接的合法用户来说,这可能会非常烦人。我想到的是Steam,它在互联网上每次发布时都会验证密钥的有效性,这让很多用户感到恼火


要找到一个好的保护技术,环顾四周,试着确定其他人是怎么做的,哪些技术有效,哪些无效。它们是很多例子(尤其是视频游戏行业)。但请记住,即使是最好的公司也无法正确保护其应用程序。没有什么技术是牢不可破的。

如果您的需求有一定的灵活性,那么这并不难——也许下面的方案适合您

您可以只生成K=[SN,H([X,SN,Y]),这是递增序列号与散列的串联,其中散列是唯一常数X和Y之间序列号串联的安全散列函数,它们是机密的

使用众所周知的安全散列算法(例如,SHA-1或SHA-2;MD5可能也足够了,因为MD5的已知弱点是冲突攻击,而不是冲突攻击),并且至少在串行密钥部分(您可能希望防止两个人使用相同的密钥)之前,您应该全部设置好

另一个有用的方法是使用K=[SN,T,H([X,SN,T,Y])——同时使用序列号和时间戳。这可用于仅允许串行密钥的狭窄使用窗口:它在时间戳的N秒内有效,因此它将防止在该窗口外重用密钥

然后将K编码/解码为一种表示形式,该表示形式可用于方便用户输入密钥(例如base64)


最好有一个简单而透明的整体算法——如果有人对你的方案进行反向工程,模糊处理不会对你有帮助。

我对那篇文章很感兴趣,所以我用Java实现了代码。可能有用

import java.util.Locale;
import java.util.Set;
import java.util.TreeSet;

public class KeyValidator {
    private static final byte[][] params = new byte[][] { { 24, 4, 127 }, { 10, 0, 56 }, { 1, 2, 91 }, { 7, 1, 100 } };
    private static final Set<String> blacklist = new TreeSet<String>();

    static {
        blacklist.add("11111111");
    }

    private static byte PKV_GetKeyByte(final int seed, final byte a, final byte b, final byte c) {
        final int a1 = a % 25;
        final int b1 = b % 3;
        if (a1 % 2 == 0) {
            return (byte) (((seed >> a1) & 0x000000FF) ^ ((seed >> b1) | c));
        } else {
            return (byte) (((seed >> a1) & 0x000000FF) ^ ((seed >> b1) & c));
        }
    }

    private static String PKV_GetChecksum(final String s) {
        int left = 0x0056;
        int right = 0x00AF;
        for (byte b : s.getBytes()) {
            right += b;
            if (right > 0x00FF) {
                right -= 0x00FF;
            }
            left += right;
            if (left > 0x00FF) {
                left -= 0x00FF;
            }
        }
        int sum = (left << 8) + right;
        return intToHex(sum, 4);
    }

    public static String PKV_MakeKey(final int seed) {
        // Fill KeyBytes with values derived from Seed.
        // The parameters used here must be exactly the same
        // as the ones used in the PKV_CheckKey function.
        // A real key system should use more than four bytes.
        final byte[] keyBytes = new byte[4];
        keyBytes[0] = PKV_GetKeyByte(seed, params[0][0], params[0][1], params[0][2]);
        keyBytes[1] = PKV_GetKeyByte(seed, params[1][0], params[1][1], params[1][2]);
        keyBytes[2] = PKV_GetKeyByte(seed, params[2][0], params[2][1], params[2][2]);
        keyBytes[3] = PKV_GetKeyByte(seed, params[3][0], params[3][1], params[3][2]);

        // the key string begins with a hexadecimal string of the seed
        final StringBuilder result = new StringBuilder(intToHex(seed, 8));

        // then is followed by hexadecimal strings of each byte in the key
        for (byte b : keyBytes) {
            result.append(intToHex(b, 2));
        }

        // add checksum to key string
        result.append(PKV_GetChecksum(result.toString()));

        final String key = result.toString();
        return key.substring(0, 4) + "-" + key.substring(4, 8) + "-" + key.substring(8, 12) + "-" + key.substring(12, 16) + "-" + key.substring(16, 20);
    }

    private static boolean PKV_CheckKeyChecksum(final String key) {
        // remove cosmetic hyphens and normalise case
        final String comp = key.replaceAll("-", "").toLowerCase(Locale.UK);
        if (comp.length() != 20) {
            return false; // Our keys are always 20 characters long
        }

        // last four characters are the checksum
        final String checksum = comp.substring(16);
        return checksum.equals(PKV_GetChecksum(comp.substring(0, 16)));
    }

    public static Status PKV_CheckKey(final String key) {
        if (!PKV_CheckKeyChecksum(key)) {
            return Status.KEY_INVALID; // bad checksum or wrong number of
            // characters
        }

        // remove cosmetic hyphens and normalise case
        final String comp = key.replaceAll("-", "").toLowerCase(Locale.UK);

        // test against blacklist
        for (String bl : blacklist) {
            if (comp.startsWith(bl)) {
                return Status.KEY_BLACKLISTED;
            }
        }

        // At this point, the key is either valid or forged,
        // because a forged key can have a valid checksum.
        // We now test the "bytes" of the key to determine if it is
        // actually valid.

        // When building your release application, use conditional defines
        // or comment out most of the byte checks! This is the heart
        // of the partial key verification system. By not compiling in
        // each check, there is no way for someone to build a keygen that
        // will produce valid keys. If an invalid keygen is released, you
        // simply change which byte checks are compiled in, and any serial
        // number built with the fake keygen no longer works.

        // Note that the parameters used for PKV_GetKeyByte calls MUST
        // MATCH the values that PKV_MakeKey uses to make the key in the
        // first place!

        // extract the Seed from the supplied key string
        final int seed;
        try {
            seed = Integer.valueOf(comp.substring(0, 8), 16);
        } catch (NumberFormatException e) {
            return Status.KEY_PHONY;
        }

        // test key 0
        final String kb0 = comp.substring(8, 10);
        final byte b0 = PKV_GetKeyByte(seed, params[0][0], params[0][1], params[0][2]);
        if (!kb0.equals(intToHex(b0, 2))) {
            return Status.KEY_PHONY;
        }

        // test key1
        final String kb1 = comp.substring(10, 12);
        final byte b1 = PKV_GetKeyByte(seed, params[1][0], params[1][1], params[1][2]);
        if (!kb1.equals(intToHex(b1, 2))) {
            return Status.KEY_PHONY;
        }

        // test key2
        final String kb2 = comp.substring(12, 14);
        final byte b2 = PKV_GetKeyByte(seed, params[2][0], params[2][1], params[2][2]);
        if (!kb2.equals(intToHex(b2, 2))) {
            return Status.KEY_PHONY;
        }

        // test key3
        final String kb3 = comp.substring(14, 16);
        final byte b3 = PKV_GetKeyByte(seed, params[3][0], params[3][1], params[3][2]);
        if (!kb3.equals(intToHex(b3, 2))) {
            return Status.KEY_PHONY;
        }

        // If we get this far, then it means the key is either good, or was made
        // with a keygen derived from "this" release.
        return Status.KEY_GOOD;
    }

    protected static String intToHex(final Number n, final int chars) {
        return String.format("%0" + chars + "x", n);
    }

    public enum Status {
        KEY_GOOD, KEY_INVALID, KEY_BLACKLISTED, KEY_PHONY
    }
}
import java.util.Locale;
导入java.util.Set;
导入java.util.TreeSet;
公共类密钥验证器{
私有静态最终字节[][]params=新字节[][{{24,4,127},{10,0,56},{1,2,91},{7,1100};
私有静态最终集黑名单=新树集();
静止的{
黑名单。添加(“11111111”);
}
私有静态字节PKV_GetKeyByte(最终整数种子、最终字节a、最终字节b、最终字节c){
最终int a1=a%25;
最终整数b1=b%3;
如果(a1%2==0){
返回(字节)((种子>>a1)&0x000000FF)^((种子>>b1)| c));
}否则{
返回(字节)((种子>>a1)和0x000000FF)^((种子>>b1)和c));
}
}
私有静态字符串PKV_GetChecksum(最终字符串s){
int left=0x0056;
int right=0x00AF;
对于(字节b:s.getBytes()){
右+=b;
如果(右>0x00FF){
右-=0x00FF;
}
左+=右;
如果(左>0x00FF){
左-=0x00FF;
}
}

int sum=(左)我希望我能将这两个都标记为答案,但Jason的答案给了我一些开始工作的东西,因此我将标记他的答案以用于我的问题。谢谢!如果您演示如何实现它,我将非常高兴。