2014-07-26 3 views
0

안녕하세요 구축이이 방법 암호화가 잘 작동하지만 암호가 바이트를 원하기 때문에 암호 해독이 오류가 발생하고 내가 직접 문자열로 변환 할 수 없습니다이 메서드에서 Cipher를 사용하여 문자열을 해독하는 방법?

import javax.crypto.Cipher; 
import javax.crypto.spec.SecretKeySpec; 

public class Test { 

    private byte[] encrypted; 

    private String encryptedtext; 
    private String decrypted; 



    public String Encrypt (String pInput) { 


     try { 

     String Input = pInput; 
     String key = "Bar12345Bar12345Bar12345Bar12345"; 

     // Erstelle key and cipher 
     SecretKeySpec aesKey = new SecretKeySpec(key.getBytes(), "AES"); 
     Cipher cipher = Cipher.getInstance("AES"); 

     // Verschlüsselung 
     cipher.init(Cipher.ENCRYPT_MODE, aesKey); 
     byte[] encrypted = cipher.doFinal(Input.getBytes()); 
     encryptedtext = new String(encrypted); 
     System.err.println("encrypted:" + encryptedtext); 


     }catch(Exception e) { 
     e.printStackTrace(); 
     } 

     return encrypted; 
    } 



    public String Decrypt (String pInput) { 


     try { 

      String Input = pInput; 

      String key = "Bar12345Bar12345Bar12345Bar12345"; 

      // Erstelle key and cipher 
      SecretKeySpec aesKey = new SecretKeySpec(key.getBytes(), "AES"); 
      Cipher cipher = Cipher.getInstance("AES"); 

      // Entschlüsselung 
      cipher.init(Cipher.DECRYPT_MODE, aesKey); 
      decrypted = new String(cipher.doFinal(encryptedtext)); // HERE IS THE PROBLEM IT WANT BYTE BUT I WANT TO ENCRYPT FROM A STRING 
      System.err.println("decrypted: " + decrypted); 

     }catch(Exception e) { 
      e.printStackTrace(); 
     } 
     return pInput; 
     } 

} 
+2

암호화는 바이트를 사용합니다. 그렇다면 암호화를 위해 어떻게 해결 했습니까? – immibis

+0

네,하지만 바이트를 문자열로 변환하여 사용하고 인쇄 할 수는 있지만 문제는 암호화에서 바이트 세션을 원하는 암호 해독을 사용할 때입니다. 하지만이 mehtod 문자열을주고 싶습니다 decryptet 텍스트를 문자열에 저장하여 decrpyted 값을 표시합니다. 나는이 버전을 무의미하게 사용하고 싶다. 나는 암호를 해독해야한다. 그리고 그것은 작동 할 것이지만 나는 무언가를 암호화하지 않고 복호화를 사용하기 전에 – hanso

+0

암호문은 바이트를 암호화하고 바이트를 반환한다. 그러나 문자열을 암호화하고 있습니다. 그렇다면 문자열을 암호화하기 전에 어떻게 문자열을 바이트로 변환 했습니까? – immibis

답변

2

바이트 배열 문자열에서 암호화 할 않으며, 어느 쪽도 반대 방향.

import javax.crypto.Cipher; 
import javax.crypto.spec.SecretKeySpec; 
import javax.xml.bind.DatatypeConverter; 

public class stackoverflow_test { 
    private byte[] encrypted; 

    private String encryptedtext; 
    private String decrypted; 

    public String Encrypt(String pInput) { 

     try { 

      String Input = pInput; 
      String key = "Bar12345Bar12345Bar12345Bar12345"; 

      SecretKeySpec aesKey = new SecretKeySpec(key.getBytes(), "AES"); 
      Cipher cipher = Cipher.getInstance("AES"); 

      cipher.init(Cipher.ENCRYPT_MODE, aesKey); 
      byte[] encrypted = cipher.doFinal(Input.getBytes()); 
      //encryptedtext = new String(encrypted); 
      encryptedtext = DatatypeConverter.printBase64Binary(encrypted); 
      System.err.println("encrypted:" + encryptedtext); 

     } catch (Exception e) { 
      e.printStackTrace(); 
     } 

     return encryptedtext; 
    } 

    public String Decrypt(String pInput) { 

     try { 

      String Input = pInput; 

      String key = "Bar12345Bar12345Bar12345Bar12345"; 

      SecretKeySpec aesKey = new SecretKeySpec(key.getBytes(), "AES"); 
      Cipher cipher = Cipher.getInstance("AES"); 

      cipher.init(Cipher.DECRYPT_MODE, aesKey); 
      encrypted = DatatypeConverter.parseBase64Binary(encryptedtext); 
      decrypted = new String(cipher.doFinal(encrypted)); 
      System.err.println("decrypted: " + decrypted); 

     } catch (Exception e) { 
      e.printStackTrace(); 
     } 
     return pInput; 
    } 

    public static void main(String[] ag){ 
     stackoverflow_test test = new stackoverflow_test(); 
     String a = test.Encrypt("Byte cannot directly convert to string"); 
     String b = test.Decrypt(a); 
    } 
} 

결과

encrypted:UmH+3eUagjrRDblxSStArnaktoxTLX+7qvPdwiTO7VggYmYtuXu/Ygww8ZG5SrDz 
decrypted: Byte cannot directly convert to string 
+0

고맙습니다 :)하지만 안드로이드는 Base64에 대한 몇 가지 문제가 발생하기 때문에이 코드에서 Base64를 사용하지 마시기 바랍니다 :/ – hanso

+0

이것이 도움이 될 수 있습니다. 이것을 16 진수 문자열로 바꾸십시오. http://stackoverflow.com/questions/9655181/convert -from-byte-array-to-hex-string-in-java – tom87416

+0

그리고 암호를 해독하지 않고 암호 해독을 사용하면 ... – hanso