2015-01-05 122 views
1

AES加密,我發現在互聯網上這個Java AES加密。我可以在我的android應用程序中修改和使用這個java代碼嗎?其實我不知道它是否適合我的Android設備。如果我可以使用它,這是否意味着我可以修改這個java代碼?實施的Android

import java.io.UnsupportedEncodingException; 
import java.security.MessageDigest; 
import java.security.NoSuchAlgorithmException; 
import java.util.Arrays; 
import javax.crypto.Cipher; 
import javax.crypto.spec.SecretKeySpec; 
import org.apache.commons.codec.binary.Base64; 
/** 
Aes encryption 
*/ 
public class AES 
{ 

    private static SecretKeySpec secretKey ; 
    private static byte[] key ; 

    private static String decryptedString; 
    private static String encryptedString; 

    public static void setKey(String myKey){ 


     MessageDigest sha = null; 
     try { 
      key = myKey.getBytes("UTF-8"); 
      System.out.println(key.length); 
      sha = MessageDigest.getInstance("SHA-1"); 
      key = sha.digest(key); 
      key = Arrays.copyOf(key, 16); // use only first 128 bit 
      System.out.println(key.length); 
      System.out.println(new String(key,"UTF-8")); 
      secretKey = new SecretKeySpec(key, "AES"); 


     } catch (NoSuchAlgorithmException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } catch (UnsupportedEncodingException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 



    } 

    public static String getDecryptedString() { 
     return decryptedString; 
    } 
    public static void setDecryptedString(String decryptedString) { 
     AES.decryptedString = decryptedString; 
    } 
    public static String getEncryptedString() { 
     return encryptedString; 
    } 
    public static void setEncryptedString(String encryptedString) { 
     AES.encryptedString = encryptedString; 
    } 
    public static String encrypt(String strToEncrypt) 
    { 
     try 
     { 
      Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding"); 

      cipher.init(Cipher.ENCRYPT_MODE, secretKey); 


      setEncryptedString(Base64.encodeBase64String(cipher.doFinal(strToEncrypt.getBytes("UTF-8")))); 

     } 
     catch (Exception e) 
     { 

      System.out.println("Error while encrypting: "+e.toString()); 
     } 
     return null; 
    } 
    public static String decrypt(String strToDecrypt) 
    { 
     try 
     { 
      Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5PADDING"); 

      cipher.init(Cipher.DECRYPT_MODE, secretKey); 
      setDecryptedString(new String(cipher.doFinal(Base64.decodeBase64(strToDecrypt)))); 

     } 
     catch (Exception e) 
     { 

      System.out.println("Error while decrypting: "+e.toString()); 
     } 
     return null; 
    } 
    public static void main(String args[]) 
    { 
       final String strToEncrypt = "My text to encrypt"; 
       final String strPssword = "encryptor key"; 
       AES.setKey(strPssword); 

       AES.encrypt(strToEncrypt.trim()); 

       System.out.println("String to Encrypt: " + strToEncrypt); 
       System.out.println("Encrypted: " + AES.getEncryptedString()); 

       final String strToDecrypt = AES.getEncryptedString(); 
       AES.decrypt(strToDecrypt.trim()); 

       System.out.println("String To Decrypt : " + strToDecrypt); 
       System.out.println("Decrypted : " + AES.getDecryptedString()); 

    } 

} 

來源:http://aesencryption.net/

+0

能否請你描述你想要做什麼?您可以隨時隨地使用任何代碼。但它會做你想要的嗎?那麼你想做什麼? –

+0

我想使用AES加密將Android應用程序中的數據加密到MySQL數據庫中 – MSR

回答

3

,當我在一個Android應用程序中實現AES加密,我有實例的密碼時指定供應商BouncyCastle的。

Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding","BC"); 

您應該考慮使用cbc模式,因爲ecb模式並不安全。

Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding","BC"); 

你的代碼稍加修改之後,這應該工作:

import android.util.Base64; 
import java.io.UnsupportedEncodingException; 
import java.nio.charset.Charset; 
import java.security.MessageDigest; 
import java.security.NoSuchAlgorithmException; 
import java.security.SecureRandom; 
import java.util.Arrays; 
import java.util.Random; 

import javax.crypto.Cipher; 
import javax.crypto.spec.IvParameterSpec; 
import javax.crypto.spec.SecretKeySpec; 
/** 
Aes encryption 
*/ 
public class AES 
{ 

    private static SecretKeySpec secretKey ; 
    private static byte[] key ; 
    private static byte[] iv; 
    private static String decryptedString; 
    private static String encryptedString; 

    public static void setKey(String myKey){ 


     MessageDigest sha = null; 
     try { 
      key = myKey.getBytes("UTF-8"); 
      System.out.println(key.length); 
      sha = MessageDigest.getInstance("SHA-1"); 
      key = sha.digest(key); 
      key = Arrays.copyOf(key, 16); // use only first 128 bit 
      secretKey = new SecretKeySpec(key, "AES"); 
      iv = new byte[]{11,53,63,87,11,69,63,28,0,9,18,99,95,23,45,8}; 

     } catch (NoSuchAlgorithmException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } catch (UnsupportedEncodingException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 



    } 

    public static String getDecryptedString() { 
     return decryptedString; 
    } 
    public static void setDecryptedString(String decryptedString) { 
     AES.decryptedString = decryptedString; 
    } 
    public static String getEncryptedString() { 
     return encryptedString; 
    } 
    public static void setEncryptedString(String encryptedString) { 
     AES.encryptedString = encryptedString; 
    } 
    public static String encrypt(String strToEncrypt) 
    { 
     try 
     { 
      Cipher cipher = Cipher.getInstance("AES/CBC/PKCS7Padding","BC"); 
      cipher.init(Cipher.ENCRYPT_MODE, secretKey,new IvParameterSpec(iv)); 
      setEncryptedString(Base64.encodeToString(cipher.doFinal(strToEncrypt.getBytes("UTF-8")),Base64.DEFAULT)); 

     } 
     catch (Exception e) 
     { 

      System.out.println("Error while encrypting: "+e.toString()); 
     } 
     return null; 
    } 
    public static String decrypt(String strToDecrypt) 
    { 
     try 
     { 
      Cipher cipher = Cipher.getInstance("AES/CBC/PKCS7PADDING","BC"); 
      cipher.init(Cipher.DECRYPT_MODE, secretKey,new IvParameterSpec(iv)); 
      String decoded = new String(cipher.doFinal(Base64.decode(strToDecrypt,Base64.DEFAULT)), Charset.forName("UTF-8")); 
      setDecryptedString(decoded); 

     } 
     catch (Exception e) 
     { 

      System.out.println("Error while decrypting: "+e.toString()); 
     } 
     return null; 
    } 

} 
+0

我在更改CBC後得到錯誤 – MSR

+0

出現什麼樣的錯誤? – aschattney

+0

at com.android.org.bouncycastle.jcajce.provider.symmetric.util.BaseBlockCipher.engineInit(BaseBlockCipher.java:410) – MSR