2010-09-01 252 views
8

我一直在這裏閱讀一些帖子和網絡上的文章,但我無法爲我的應用程序描繪一個基於串行鍵的系統。串行鍵:怎麼辦?

http://www.brandonstaggs.com/2007/07/26/implementing-a-partial-serial-number-verification-system-in-delphi/

我看這一個,但我不能把代碼放到Java和我不是很熟悉的條款會。

你能給我什麼可能的見解嗎?理想情況下,我的應用程序將用於銷售,但我不認爲它會很受歡迎,如果我有讓用戶欣賞產品併購買它的用戶,我不介意太多,但我想避免它很容易破解。請儘量具體,我對Java有點新鮮。

在此先感謝。

回答

6

這並不難,如果你的需求有些靈活 - 也許下面的方案會適合你。

您可以生成K = [SN,H([X,SN,Y])],它是遞增序列號與散列的並置,其中散列是串聯的安全散列函數唯一常數X和Y之間的序列號是祕密的"salt" you use to prevent the use of rainbow tables

使用一個衆所周知的安全散列算法(例如SHA-1或SHA-2,MD5大概也就足夠了,因爲對MD5已知的弱點是衝突攻擊,並preimage attacks),你應該準備就緒,至少就序列關鍵部分而言(您可能希望防止兩個人使用相同的關鍵字)。

你可以做的其他事情有用的是使用K = [SN,T,H([X,SN,T,Y])] - 同時使用序列號和時間戳。這可以用來爲串行密鑰只允許一個窄的使用窗口:它在時間戳的N秒內有效,所以它會阻止在該窗口之外重用密鑰。然後將K編碼/解碼爲可用於容易地允許用戶輸入密鑰(例如base64)的表示。

最好有一個簡單而透明的整體算法 - 如果有人反向設計你的方案,混淆不會幫助你。

+0

謝謝賈森。我會開始研究它! – Qosmo 2010-09-01 14:31:42

4

確保應用程序的安全並不是一項簡單的任務。許多公司正在投入大量資金來尋找新的安全算法,這些算法都可以快速破解。

保護Java應用程序有點困難。嵌入在您的應用程序中的任何串行驗證算法都可以反編譯,因此串行密鑰生成器將非常易於構建。

一個好的起點是你給的文章。它會告訴您如何構建密鑰驗證系統,以及如何爲您的(合法)用戶生成密鑰。

實施這樣的算法後,我建議你稍微保護一下源代碼,這樣反編譯變得更加「棘手」了。使用代碼混淆技術來隱藏您的驗證算法實現。這也會讓試圖通過修改字節碼來破解應用程序的任務變得更加困難。

一個好的技術可能是將您的密鑰驗證算法導出到遠程服務器上。客戶端將密鑰發送給服務器,服務器使用「驗證代碼」回覆,告訴您的應用程序您的密鑰是有效的。但這並不妨礙用戶修改您的應用程序以刪除任何密鑰驗證程序。對於沒有24小時互聯網連接的合法用戶來說,這可能會非常煩人。我正在考慮Steam,它在互聯網上每次發佈時驗證關鍵效度,並且會讓很多用戶感到困擾。

要找到一個好的保護技術,環顧四周,並嘗試確定別人如何做,哪些技術正在工作,哪些不是。他們是很多例子(特別是電子遊戲產業)。但請記住,即使是最好的公司也無法正確保護他們的應用程序。沒有技術是牢不可破的。

+0

我希望我能夠既作爲一個答案的標記,但賈森的回答給了我一些東西開始工作,所以我還是選了他對我的問題的目的答案。謝謝! – Qosmo 2010-09-01 14:28:06

12

我對那篇文章很感興趣,所以我用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 
    } 
} 
+0

如果你展示如何實現它,我會很高興。 – 2017-02-28 08:38:58