【问题标题】:How to encode TO a 4 digits number or nummerical string in JAVA?如何在 JAVA 中编码为 4 位数字或数字字符串?
【发布时间】:2013-09-24 11:09:40
【问题描述】:

我对java比较陌生,最近几天我一直在努力完成一项任务,直到最后一个“任务”之前一切都很顺利。

更具体:

我想从 3 个字符串字段中创建一个加密/编码的 4 位数字。您可以将其想象为尝试生成一个 4 位数的 CVV 号码,就像信用卡中一样,借助(取决于)卡号,exp。日期和服务代码。为了实现这一点,我首先使用带有随机密钥的 DES 加密卡号,然后使用加密的卡号作为密钥加密(DES)到期日期,最后一步,我使用加密的 exp 日期加密(DES)服务代码作为一把钥匙。到目前为止,一切都很好,我可以在每一步中检索所需的信息。问题是作为我最终输出的加密服务代码的长度应为 4,并且仅包含数字。在网上研究了两天,尝试了几次之后,比如:

  • 散列:问题是没有从散列值解码回加密的服务代码
  • Base64 转换:无法达到长度和只有数字
  • 填充:会丢失重要信息
  • 进一步加密:找不到产生如此短(就长度而言)密钥的算法。

还有其他解决办法吗?

这是算法的最后两个步骤,只是为了让您了解它是如何运行的。

desCipher.init(Cipher.ENCRYPT_MODE, myDesKey_2);

        // Sensitive information - message to be encrypted
        byte[] date_of_exp = "032019".getBytes(); // Date of Expiration in form MMYYYY

        //System.out.println("Card Number : " + card_number); // Print original message

        // Encrypt the text
       byte[] date_of_expEncrypted = desCipher.doFinal(date_of_exp);

        System.out.println("");
        System.out.println("Date of Expiration Encrypted : " + date_of_expEncrypted); // Print the encrypted message
        System.out.println("");

        // Initialize the same cipher for decryption
        desCipher.init(Cipher.DECRYPT_MODE, myDesKey_2);

        String date_of_expEncrypted_;
        date_of_expEncrypted_ = DatatypeConverter.printBase64Binary(date_of_expEncrypted); 
        // SecretKey card_numberEncrypted_key;
        // card_numberEncrypted_key = stringToSecretKey (card_numberEncrypted_, "DES");
        SecretKey date_of_expEncrypted_key;
        date_of_expEncrypted_key = new SecretKeySpec(date_of_expEncrypted, 0, 8, "DES");
        System.out.println("");
        System.out.println("Date of expiration as secret key :" + date_of_expEncrypted_key);
        System.out.println("");

        // Decrypt the text
        byte[] date_of_expDecrypted = desCipher.doFinal(date_of_expEncrypted);

        System.out.println("Original Date of Expiration (decrypted) : " + new String(date_of_expDecrypted)); // Print the decrypted Text
        System.out.println("");
        System.out.println("");
        System.out.println("-----------------------------------------------------------------------------------");
        System.out.println("Further to Step 3"); // Print the decrypted Text
        System.out.println("-----------------------------------------------------------------------------------"); // Print the decrypted Text
        System.out.println("");
        System.out.println("");




    SecretKey myDesKey_3 = date_of_expEncrypted_key;

    //Cipher desCipher_2; // New Cipher for iteration 2

        // Create the cipher 
        //desCipher_2 = Cipher.getInstance("DES/ECB/PKCS5Padding");

        // Initialize the cipher for encryption
        desCipher.init(Cipher.ENCRYPT_MODE, myDesKey_3);

        // Sensitive information - message to be encrypted
        byte[] service_code = "318".getBytes(); 

       // Encrypt the text
       byte[] service_codeEncrypted = desCipher.doFinal(service_code);
        System.out.println("");
        System.out.println("Service Code Encrypted : " + service_codeEncrypted); // Print the encrypted message
        System.out.println("");
        // Initialize the same cipher for decryption
        desCipher.init(Cipher.DECRYPT_MODE, myDesKey_3);

        // Decrypt the text
        byte[] service_codeDecrypted = desCipher.doFinal(service_codeEncrypted);

        System.out.println("Service Code decrypted : " + new String(service_codeDecrypted)); // Print the decrypted Text
        System.out.println("");
        System.out.println("");
        System.out.println("-----------------------------------------------------------------------------------");
        System.out.println("Finish!!!"); // Print the decrypted Text
        System.out.println("-----------------------------------------------------------------------------------"); // Print the decrypted Text
        System.out.println("");
        System.out.println("");


        //Integer bigInt = new Integer("Bwwhw34".getBytes());
        // int service_codeEncrypted_hashed = service_codeEncrypted.hashCode();
        // System.out.println("hash code for Service Code Encrypted : " + service_codeEncrypted_hashed);
        // int service_codeEncrypted_hashed_2 = service_codeEncrypted_hashed.hashCode();

        // byte[] service_code__ = service_codeEncrypted.getBytes(); 
        //  System.out.println("hash code for Service Code Encrypted and baseD  : " + service_code__);



    }catch(NoSuchAlgorithmException e){
        e.printStackTrace();
    }catch(NoSuchPaddingException e){
        e.printStackTrace();
    }catch(InvalidKeyException e){
        e.printStackTrace();
    }catch(IllegalBlockSizeException e){
        e.printStackTrace();
    }catch(BadPaddingException e){
        e.printStackTrace();
    } 

}

输出“服务代码加密”将采用 [B@84abc9,这对我的目的无用。

提前谢谢,对不起我的英语不好!

【问题讨论】:

  • 你的任务根本不可能:有无限多的字符串,但只有 10000 个 4 位数字。
  • 感谢您的回复。我几乎可以肯定,只是想百分百确定。是时候寻找替代方法了。

标签: java encryption base64 encode des


【解决方案1】:

“我想从 3 个字符串字段中创建一个加密/编码的 4 位数字。”

你唯一的选择是哈希,否则你需要哈利波特来帮助你。或者您是否期望您可以将 4 个数字转换回任何 3 个可能的字符串?恐怕这种压缩不存在。

至于你的“[B@84abc9”。这是字节数组的默认 toString() 。在显示之前,您需要将其转换为十六进制或 Base64。

【讨论】:

  • 我已经从 3 个字符串字段中创建了字符串,目前,安全性不是一个选项,因为我在前面的步骤中被加密覆盖。实际的问题是如何将字符串编码为 4 位数字以及之后如何恢复它。如果有一个“dehash”函数,哈希可能是理想的......
  • @kiko77 你在要求不可能的事情。您无法从 4 位数字中恢复原始字符串。因此,唯一的方法是允许超过 4 个数字,或者使用哈希并将其减少到 4 个字符(或数字)。
  • 我认为这几乎是不可能的,但只是想确定一下。哈希只有一种方式(据我所知),因此并没有真正帮助我。
  • @kiko77 是的,这就是为什么可以使用散列将大量数据转换为小块的原因。因为它会丢失信息。同样不适用于压缩(或加密,如果你还在讨论的话)。
  • 几乎很明显这是不可能的。感谢你的回复。我希望我早点问过,这样我就可以节省很多尝试和搜索的工作时间,并专注于一种可能的新方法!
猜你喜欢
  • 2010-12-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-02-26
  • 2020-02-20
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多