gpt4 book ai didi

java - 如何在JAVA中对4位数字或数字字符串进行编码?

转载 作者:行者123 更新时间:2023-12-01 14:04:55 25 4
gpt4 key购买 nike

我对java比较陌生,最近几天我为了完成一项任务而绞尽脑汁,一切都很顺利,直到最后一个“任务”。

更具体:

我想从 3 个字符串字段中创建一个加密/编码的 4 位数字。您可以将其想象为尝试在卡号、exp 的帮助下生成 4 位 CVV 号,就像信用卡中的那样。日期和服务代码。为了实现这一点,我首先使用 DES 和随 secret 钥对卡号进行加密,然后使用加密的卡号作为 key 对到期日期进行加密 (DES),最后一步,我使用加密的到期日期对服务代码进行加密 (DES)作为 key 。到目前为止,一切顺利,我可以在每一步中检索所需的信息。问题是加密的服务代码(将是我的最终输出)的长度应为 4 并且仅包含数字。经过两天在网上的研究,经过多次尝试,比如:

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

还有其他解决办法吗?

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

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,这对我的目的来说毫无用处。

提前致谢,并对我的英语不好表示歉意!

最佳答案

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

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

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

关于java - 如何在JAVA中对4位数字或数字字符串进行编码?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18979978/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com