2013-09-24 6 views
0

나는 자바에 비교적 익숙하지 않다. 나는 지난 며칠 동안 일을 끝내기 위해 머리를 부러 뜨 렸으며,이 마지막 "퀘스트"까지는 모든 것이 잘되었다.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에있을 것은, 느릅 나무는 내 목적을 위해 쓸모가 없다.

고맙습니다. 사전에 고맙고 나쁜 영어로 유감스럽게 생각합니다!

+0

귀하의 작업은 근본적으로 불가능합니다 : 무한히 많은 문자열이 있지만 10000 개의 4 자리 숫자 만 있습니다. – Joni

+0

응답을 위해 Tahnk이 (가) 있습니다. 나는 그것에 대해 거의 확실했고, 단지 100 % 확신하고 싶었다. 대체 접근법을 찾을 시간. – kiko77

답변

1

"3 개의 문자열 필드 중에서 암호화 된/인코딩 된 4 자리 숫자를 만들고 싶습니다."

유일한 선택은 해시입니다. 그렇지 않으면 해리 포터가 도움이 될 것입니다. 아니면 4 자리를 3 개의 가능한 문자열로 다시 변환 할 수 있으리라고 기대 했습니까? 저 종류의 압축이 존재하지 않는 것이 두렵습니다.

"[B @ 84abc9"에 대해서. 이것이 바이트 배열의 기본 toString()입니다. 표시하기 전에이를 16 진수 또는 Base64로 바꾸고 싶을 것입니다.

+0

나는 이미 3 개의 문자열 필드에서 문자열을 만들었으며 지금은 이전 단계의 암호화에서 다루었 기 때문에 보안은 옵션이 아닙니다. 실제 질문은 문자열을 4 자리 숫자로 인코딩하는 방법과 이후에 그것을 복구하는 방법입니다. "해쉬 (dehash)"기능이 있다면 해시가 이상적이었을 것입니다 ... – kiko77

+0

@ kiko77 불가능한 것을 요구하고 있습니다.원래의 문자열을 4 자리에서 다시 얻을 수있는 방법은 없습니다. 따라서 유일한 방법은 4 자리 이상 허용하거나 해시를 사용하여 4 자 (또는 숫자)로 자르는 것입니다. – Kayaman

+0

나는 불가능하다고 생각했지만 확신하고 싶었다. 해시는 (내가 아는 한) 오직 한 가지 방식으로 작동하기 때문에 실제로 도움이되지 않습니다. – kiko77

관련 문제