我对java比较陌生,过去几天我一直在努力完成一项任务,直到最后一个“任务”之前一切都很顺利。
更详细:
我想从 3 个字符串字段中创建一个加密/编码的 4 位数字。您可以将其想象为尝试生成一个 4 位数的 CVV 号码,就像信用卡中一样,借助(取决于)卡号,exp。日期和服务代码。为了实现这一点,我首先使用带有随机密钥的 DES 加密卡号,然后使用加密的卡号作为密钥加密(DES)到期日期,最后一步,我使用加密的 exp 日期加密(DES)服务代码作为一把钥匙。到目前为止,一切都很好,我可以在每一步中检索所需的信息。问题是加密的服务代码,这将是我的最终输出,应该有长度 4 并且应该只包含数字。在网上研究了两天,尝试了几次之后,比如:
- 散列:问题是没有从散列值解码回加密的服务代码
- Base64 转换:无法达到长度,只有数字
- padding:会丢失重要信息
- 进一步加密:找不到产生如此短(就长度而言)密钥的算法。
还有其他解决方案吗?
这是算法的最后两个步骤,只是为了让您了解它是如何运行的。
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,这对我的目的无用。
提前谢谢,对不起我的英语不好!