2014-09-16 3 views
2

내 응용 프로그램에서는 문자열을 인코딩 한 다음 (javax.crypto.Mac을 사용하여) MAC ID를 생성하고 있습니다.하지만 다시 디코딩하려했으나 그렇게 할 수 없었습니다. 제발 도와 주실 수 있습니까? 내가 잘못한 곳을 지적 해 주시겠습니까?인코딩 된 문자열을 java로 디코딩?

코드

String userid = "AmilaI"; 
String time = gmtFormat.format(now)+ "Z"; 

String algorithmKey = time + userid; 

SecretKeySpec sks = new SecretKeySpec(algorithmKey.getBytes("UTF-8"), "HmacSHA1"); 
Mac mac = Mac.getInstance("HmacSHA1"); 
mac.init(sks); 
byte[] hashBytes = mac.doFinal(route.getBytes("UTF-8")); 

String hmac = Base64.encodeBase64String(hashBytes); 
hmac = hmac.replaceAll("\r\n", ""); 
System.out.println("Encrypted "+ hmac); 

byte[] decoded = Base64.decodeBase64(hmac); 
System.out.println("Decrypted " + new String(decoded, "UTF-8") + "\n"); 

그래서 나는 해독하여 사용자 ID를 얻을 수있는 방법은 경로를 되돌릴 수 다시 alogorithmKey를 얻을?

답변

5

HmacSHA1은 해시이므로 한 방향으로 만 작동하므로 원래 값을 얻을 수 없습니다. 가역적 인 알고리즘을 사용해야합니다.

+0

감사하십시오! 알았어 : –

1

public class Crypto { 

     private static final String engine = "AES"; 
     private static final String crypto = "AES/CBC/PKCS5Padding"; 
     private static Context ctx; 

     public Crypto(Context cntx) { 
      ctx = cntx; 
     } 

     public byte[] cipher(byte[] data, int mode) 
       throws NoSuchAlgorithmException, NoSuchPaddingException, 
       InvalidKeyException, IllegalBlockSizeException, 
       BadPaddingException, InvalidAlgorithmParameterException { 
      KeyManager km = new KeyManager(ctx); 
      SecretKeySpec sks = new SecretKeySpec(km.getId(), engine); 
      IvParameterSpec iv = new IvParameterSpec(km.getIv()); 
      Cipher c = Cipher.getInstance(crypto); 
      c.init(mode, sks, iv); 
      return c.doFinal(data); 
     } 

     public byte[] encrypt(byte[] data) throws InvalidKeyException, 
       NoSuchAlgorithmException, NoSuchPaddingException, 
       IllegalBlockSizeException, BadPaddingException, 
       InvalidAlgorithmParameterException { 
      return cipher(data, Cipher.ENCRYPT_MODE); 
     } 

     public byte[] decrypt(byte[] data) throws InvalidKeyException, 
       NoSuchAlgorithmException, NoSuchPaddingException, 
       IllegalBlockSizeException, BadPaddingException, 
       InvalidAlgorithmParameterException { 
      return cipher(data, Cipher.DECRYPT_MODE); 
     } 

     public String armorEncrypt(byte[] data) throws InvalidKeyException, 
       NoSuchAlgorithmException, NoSuchPaddingException, 
       IllegalBlockSizeException, BadPaddingException, 
       InvalidAlgorithmParameterException { 
      return Base64.encodeToString(encrypt(data), Base64.DEFAULT); 
     } 

     public String armorDecrypt(String data) throws InvalidKeyException, 
       NoSuchAlgorithmException, NoSuchPaddingException, 
       IllegalBlockSizeException, BadPaddingException, 
       InvalidAlgorithmParameterException { 
      return new String(decrypt(Base64.decode(data, Base64.DEFAULT))); 
     } 
    } 
+0

당신의 지원에 감사드립니다 !! –

관련 문제