使用Android的AES 512位加密

时间:2013-09-02 12:40:47

标签: android encryption aes

是否可以使用Android应用为用户凭据执行 AES 512位加密? 有人试过吗? 我搜索了很多关于这一点,但没有得到帮助。

感谢任何帮助。

由于

2 个答案:

答案 0 :(得分:3)

不,因为没有AES-512位加密这样的东西。 NIST中仅指定了AES-128,AES-192和AES-256位加密 FIPS 197

答案 1 :(得分:1)

请看这个

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

import android.util.Base64;
import android.util.Log;

public class AesFileIo {
//    private static final String AES_ALGORITHM = "AES/CTR/NoPadding";
    private static final String AES_ALGORITHM = "AES/CBC/PKCS5Padding";
    private SecretKeySpec secretKeySpec;
    private IvParameterSpec ivSpec;

    public AesFileIo(byte[] aesKey, byte[] iv) {
        ivSpec = new IvParameterSpec(iv);
        secretKeySpec = new SecretKeySpec(aesKey, "AES");
    }

    public String decrypt(String text) {

        StringBuilder stringBuilder = new StringBuilder(); 
        try {
            Cipher cipher = Cipher.getInstance(AES_ALGORITHM);
            cipher.init(Cipher.DECRYPT_MODE, secretKeySpec, ivSpec);
            byte[] decordedValue =  Base64.decode(text,Base64.DEFAULT);
            String decryptedValue = new String(cipher.doFinal(decordedValue),"UTF-8");
            Log.e("decrypted Value :",decryptedValue);
            return decryptedValue; 
        } catch (Exception e) {
            Log.e(this.getClass().toString(), e.getMessage(), e);
        }
        return stringBuilder.toString();
    }

    public String encrypt(String text) {
        String encryptedValue=null;
        try {
            Cipher cipher = Cipher.getInstance(AES_ALGORITHM); 
            cipher.init(Cipher.ENCRYPT_MODE, secretKeySpec, ivSpec);
            byte[] encValue = cipher.doFinal(text.getBytes());
            encryptedValue = Base64.encodeToString(encValue,Base64.DEFAULT);
        } catch (Exception e) {
            Log.e(this.getClass().toString(), e.getMessage(), e);
        }
        return encryptedValue;
    }
}

如何使用

public static byte[] iv = { '0', '0','0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0' };
    private static final byte[] keyValue = new byte[] { 'b', 'i', 'r', 'a', 'j', 'z', 'a', 'l', 'a', 'v', 'a', 'd', 'i', 'y', 'a', 'r' };
AesFileIo aes = new AesFileIo(keyValue, iv);
String encrypetedText = aes.encrypt(str);
String decryptedText = aes.decrypt(encrypetedText);

System.out.println("EncrypetedText : " + encrypetedText);
System.out.println("DecryptedText : " + decryptedText);