AES在android上解密

时间:2011-09-30 23:01:35

标签: android aes compatibility encryption fileinputstream

我在android和PC上使用相同的解密代码和相同的文件,但它不适用于android。 当我使用正常工作的简单字符串解密函数时:

string decrypt example(在两个系统上工作):

    String out = encrypter.decrypt("B4987F41FAFC5CC1D725E98A43448276919F1F6AC602F78E5CC780BF480AE1A2");

PC代码:

        String key = "12345678";
        Crypto encrypter = new Crypto(key);

        OutputStream output = new OutputStream()
        {
            private StringBuilder string = new StringBuilder();
            @Override
            public void write(int b) throws IOException {
                this.string.append((char) b );
            }
            public String toString(){
                return this.string.toString();
            }
        };

        encrypter.decrypt(new FileInputStream("C:/temp/asset.xml.secure"), output);
        System.out.println(output.toString());

Android代码:

        String key = "12345678";
        Crypto encrypter = new Crypto(key); 
        OutputStream output = new OutputStream()
        {
            private StringBuilder string = new StringBuilder();
            @Override
            public void write(int b) throws IOException {
                this.string.append((char) b );
            }
            public String toString(){
                return this.string.toString();
            }
        };
        InputStream input = this.getResources().getAssets().open("asset.xml.secure");
        encrypter.decrypt(input , output);

加密课程:

        public class Crypto {
Cipher ecipher;
Cipher dcipher;

/**
* Input a string that will be md5 hashed to create the key.
* @return void, cipher initialized
*/

public Crypto(){
    try{
        KeyGenerator kgen = KeyGenerator.getInstance("AES");
        kgen.init(128);
        this.setupCrypto(kgen.generateKey());
    } catch (Exception e) {
        e.printStackTrace();
    }
}
public Crypto(String key){
    SecretKeySpec skey = new SecretKeySpec(getMD5(key), "AES");
    this.setupCrypto(skey);
}

private void setupCrypto(SecretKey key){
    // Create an 8-byte initialization vector
    byte[] iv = new byte[]
    {
        0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09,0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f
    };

    AlgorithmParameterSpec paramSpec = new IvParameterSpec(iv);
    try
    {
        ecipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
        dcipher = Cipher.getInstance("AES/CBC/PKCS5Padding");

        // CBC requires an initialization vector
        ecipher.init(Cipher.ENCRYPT_MODE, key, paramSpec);
        dcipher.init(Cipher.DECRYPT_MODE, key, paramSpec);
    }
    catch (Exception e)
    {
        e.printStackTrace();
    }
}

// Buffer used to transport the bytes from one stream to another
byte[] buf = new byte[1024];

public void encrypt(InputStream in, OutputStream out){
    try {
        // Bytes written to out will be encrypted
        out = new CipherOutputStream(out, ecipher);

        // Read in the cleartext bytes and write to out to encrypt
        int numRead = 0;
        while ((numRead = in.read(buf)) >= 0){
            out.write(buf, 0, numRead);
        }
        out.close();
    }
    catch (java.io.IOException e){
        e.printStackTrace();
    }
}

/**
* Input is a string to encrypt.
* @return a Hex string of the byte array
*/
public String encrypt(String plaintext){
    try{
        byte[] ciphertext = ecipher.doFinal(plaintext.getBytes("UTF-8"));
        return this.byteToHex(ciphertext);
    } catch (Exception e){
         e.printStackTrace();
        return null;
    }

}

public void decrypt(InputStream in, OutputStream out){
    try {
        // Bytes read from in will be decrypted
        in = new CipherInputStream(in, dcipher);

        // Read in the decrypted bytes and write the cleartext to out
        int numRead = 0;
        while ((numRead = in.read(buf)) >= 0) {
            out.write(buf, 0, numRead);
        }
        out.close();
    } catch (java.io.IOException e) {
         e.printStackTrace();
    }
}

/**
* Input encrypted String represented in HEX
* @return a string decrypted in plain text
*/
public String decrypt(String hexCipherText){
    try{
        String plaintext = new String(dcipher.doFinal(this.hexToByte(hexCipherText)), "UTF-8");
        return  plaintext;
    } catch (Exception e){
        e.printStackTrace();
        return null;
    }
}

public String decrypt(byte[] ciphertext){
    try{
        String plaintext = new String(dcipher.doFinal(ciphertext), "UTF-8");
        return  plaintext;
    } catch (Exception e){
        e.printStackTrace();
        return null;
    }
}

private static byte[] getMD5(String input){
    try{
        byte[] bytesOfMessage = input.getBytes("UTF-8");
        MessageDigest md = MessageDigest.getInstance("MD5");
        return md.digest(bytesOfMessage);
    }  catch (Exception e){
         return null;
    }
}

static final String HEXES = "0123456789ABCDEF";

public static String byteToHex( byte [] raw ) {
    if ( raw == null ) {
      return null;
    }
    final StringBuilder hex = new StringBuilder( 2 * raw.length );
    for ( final byte b : raw ) {
      hex.append(HEXES.charAt((b & 0xF0) >> 4))
         .append(HEXES.charAt((b & 0x0F)));
    }
    return hex.toString();
}

public static byte[] hexToByte( String hexString){
    int len = hexString.length();
    byte[] ba = new byte[len / 2];
    for (int i = 0; i < len; i += 2) {
        ba[i/2] = (byte) ((Character.digit(hexString.charAt(i), 16) << 4) + Character.digit(hexString.charAt(i+1), 16));
    }
    return ba;
}

}

0 个答案:

没有答案