2014-11-29 3 views
0

문자열을 암호화하고 해독하려고하지만 이상한 결과가납니다. 왜 작동하지 않는지 모르겠습니다.Android : 내 암호화/복호화가 작동하지 않습니다. 이상한 결과가 발생합니다.

싱글 톤 패턴을 사용하여 내 암호화 클래스 :

import java.io.UnsupportedEncodingException; 
import java.security.InvalidAlgorithmParameterException; 
import java.security.InvalidKeyException; 
import java.security.NoSuchAlgorithmException; 
import java.security.spec.InvalidKeySpecException; 
import java.security.spec.InvalidParameterSpecException; 

import javax.crypto.BadPaddingException; 
import javax.crypto.Cipher; 
import javax.crypto.IllegalBlockSizeException; 
import javax.crypto.NoSuchPaddingException; 
import javax.crypto.SecretKey; 
import javax.crypto.SecretKeyFactory; 
import javax.crypto.spec.PBEKeySpec; 
import javax.crypto.spec.SecretKeySpec; 

import android.util.Base64; 

public class CryptExample { 

    private static final String SECRET_KEY_ALGORITHM = "PBKDF2WithHmacSHA1"; 
    private static final String CIPHER_TRANSFORMATION = "AES/CBC/PKCS5Padding"; 
    private static final String UTF_8_CHARSET = "UTF-8"; 
    private static final int BASE_64_ENCODING = Base64.DEFAULT; 

    private static final String STR_PASSWORD = "bM4uOGs600okBDsF"; 
    private static final String STR_SALT = "iMYGYBFpl2ghOy1k0wMb"; 
    private static final int ITERATION_COUNT = 4; 
    private static final int KEY_SIZE = 128; 

    private static CryptExample mCrypt; 

    private char[] mPasswordArray; 
    private byte[] mSalt; 
    private SecretKeySpec mSecretKeySpec; 
    private Cipher mCipher; 

    public static CryptExample getInstance() { 
     if (mCrypt == null) { 
      mCrypt = new CryptExample(); 
     } 

     return mCrypt; 
    } 

    private CryptExample() { 

     try { 

      mPasswordArray = STR_PASSWORD.toCharArray(); 
      mSalt = STR_SALT.getBytes(UTF_8_CHARSET); 

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

     try { 
      init(); 
     } catch (NoSuchAlgorithmException e) { 
      e.printStackTrace(); 
     } catch (InvalidKeySpecException e) { 
      e.printStackTrace(); 
     } catch (NoSuchPaddingException e) { 
      e.printStackTrace(); 
     } catch (InvalidParameterSpecException e) { 
      e.printStackTrace(); 
     } 

    } 

    private void init() throws NoSuchAlgorithmException, InvalidKeySpecException, NoSuchPaddingException, InvalidParameterSpecException { 

     SecretKeyFactory factory = SecretKeyFactory.getInstance(SECRET_KEY_ALGORITHM); 
     PBEKeySpec spec = new PBEKeySpec(mPasswordArray, mSalt, ITERATION_COUNT, KEY_SIZE); 

     SecretKey secretKey = factory.generateSecret(spec); 
     mSecretKeySpec = new SecretKeySpec(secretKey.getEncoded(), "AES"); 

     mCipher = Cipher.getInstance(CIPHER_TRANSFORMATION); 

    } 

    public String encrypt(String szContent) throws NoSuchAlgorithmException, InvalidKeyException, InvalidParameterSpecException, IllegalBlockSizeException, BadPaddingException, UnsupportedEncodingException { 

     mCipher.init(Cipher.ENCRYPT_MODE, mSecretKeySpec); 

     byte[] encryptedContentBytes = mCipher.doFinal(szContent.getBytes(UTF_8_CHARSET)); 

     return Base64.encodeToString(encryptedContentBytes, BASE_64_ENCODING); 
    } 

    public String decrypt(String szEncryptedContent) throws InvalidKeyException, InvalidAlgorithmParameterException, IllegalBlockSizeException, BadPaddingException, UnsupportedEncodingException, InvalidParameterSpecException { 

     byte[] encryptedContentBytes = Base64.decode(szEncryptedContent, BASE_64_ENCODING); 

     mCipher.init(Cipher.DECRYPT_MODE, mSecretKeySpec); 

     byte[] decryptedContentBytes = mCipher.doFinal(encryptedContentBytes); 

     return new String(decryptedContentBytes, UTF_8_CHARSET); 
    } 

} 

Ecryption/ENT 버튼을 누르면 DVR의 시험 :

@Override 
protected void onCreate(Bundle savedInstance) { 
    super.onCreate(savedInstance); 
    // Other code 

    String text = "This is an awesome text that should be encrypted ! This is me, John and Elvira! Welcome home!"; 

    CryptExample crypto = CryptExample.getInstance(); 

    try { 
     String szEncryptedText = crypto.encrypt(text); 

     Log.D(TAG, "Text before: " + text); 
     Log.D(TAG, "Text encrypted: " + szEncryptedText); 

     String szDecryptedText = crypto.decrypt(szEncryptedText); 

     Log.D(TAG, "Text decrypted: " + szDecryptedText); 

    } catch (InvalidKeyException e) { 
     e.printStackTrace(); 
    } catch (NoSuchAlgorithmException e) { 
     e.printStackTrace(); 
    } catch (InvalidParameterSpecException e) { 
     e.printStackTrace(); 
    } catch (IllegalBlockSizeException e) { 
     e.printStackTrace(); 
    } catch (BadPaddingException e) { 
     e.printStackTrace(); 
    } catch (UnsupportedEncodingException e) { 
     e.printStackTrace(); 
    } catch (InvalidAlgorithmParameterException e) { 
     e.printStackTrace(); 
    } 
} 

그리고 출력 :

Text before: This is an awesome text that should be encrypted ! This is me, John and Elvira! Welcome home! 
Text encrypted: iYtkSbJXeZERPrdqLO40BHo8RMsmVKt7+6jNK9yTW8bPWBIB6IpnBcEk9eZp37p8fcHCOz7uJhcchrY0rgVATqwZHd8F1Xb8IWtdmZxpkB1jsANtrkA4zwJh6/IMLeDz 
Text decrypted: ;?)?{eM??%t???me text that should be encrypted ! This is me, John and Elvira! Welcome home! 

PS : 당신에 대해 어떻게 생각하십니까 내 암호화 수업? 충분히 좋은가요? 키 크기 또는 반복 횟수는 어떻게됩니까? 4 개의 반복으로 128 len을 사용해도 괜찮습니까? 그리고 암호가 소금과 결합 되었습니까?

+0

예외 처리를 위해 멀티 캐치 또는'GeneralSecurityException'을 사용할 수 있습니다. 단순히 'IllegalStateException'의 원인으로 예외를 제공하면됩니다. 해독 중에 처리해야하는 것들은 code/runtime의 실수 대신 잘못된 I/O를 나타내는'IllegalBlockSizeException'과'BadPaddingException'입니다. 나는 항상 e.printStackTrace()를 제어 흐름을 엉망으로 만들지 않기 위해 새로운 IllegalStateException ("Exception not yet yet", e);을 던져서 (TODO를 나타내는 주석과 함께) 바꾼다. –

답변

1

자바/안드로이드는 init 방법으로 자신을 지정하지 않으면 임의의 IV를 제공합니다.

당신은 당신은 암호문에 접두어 및 암호 해독 중에를 사용하여 IV를 통신 할 수 new SecureRandomIvParameterSpec를 사용하여 하나를 생성 할 수 있습니다.

관련 문제