나는 자바에 비교적 익숙하지 않다. 나는 지난 며칠 동안 일을 끝내기 위해 머리를 부러 뜨 렸으며,이 마지막 "퀘스트"까지는 모든 것이 잘되었다.JAVA에서 4 자리 숫자 또는 nummerical 문자열로 인코딩하는 방법은 무엇입니까?
더 구체적으로는 :
나는 3 개 문자열 필드 중 암호화/인코딩 된 4 자리 번호를 만들려고합니다. 신용 카드에서와 같이 4 자리 CVV 번호를 생성하려고 시도하는 것으로 상상할 수 있습니다 (카드 번호에 따라 다릅니다). 날짜와 서비스 코드. 이것을 달성하기 위해, 나는 무작위 키로 DES를 사용하여 카드 번호를 암호화 한 다음, 암호화 된 카드 번호를 키로 사용하여 exp 날짜를 암호화 (DES)하고 마지막 단계로 암호화 된 exp 날짜를 사용하여 i 암호화 (DES) 서비스 코드를 암호화합니다 열쇠로. 지금까지, 아주 좋아, 나는 필요한 모든 정보를 모든 단계에서 검색 할 수있다. 문제는 최종 결과물 인 암호화 된 서비스 코드의 길이가 4이고 숫자 만 포함해야한다는 것입니다. 그물에 연구 이틀 후, 같은 여러 가지 시도를 시도한 후 :
- 이 해싱 : 문제가 다시 해시 값에서 암호화 서비스 코드에는 디코딩이 없었다이었다
- Base64로 변환 : 모를 수가
- 더 암호화 느슨한 것입니다 중요한 정보 : 알고리즘 페이지를 찾을 수 없습니다 t는 길이와 숫자 만
- 패딩을 달성 짧은 길이 (길이면에서) 키를 깎아 내린다.
다른 해결책이 있습니까?
여기 알고리즘의 마지막 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에있을 것은, 느릅 나무는 내 목적을 위해 쓸모가 없다.
고맙습니다. 사전에 고맙고 나쁜 영어로 유감스럽게 생각합니다!
귀하의 작업은 근본적으로 불가능합니다 : 무한히 많은 문자열이 있지만 10000 개의 4 자리 숫자 만 있습니다. – Joni
응답을 위해 Tahnk이 (가) 있습니다. 나는 그것에 대해 거의 확실했고, 단지 100 % 확신하고 싶었다. 대체 접근법을 찾을 시간. – kiko77