0

是否可以使用 Android 应用程序对用户凭据进行AES 512 位加密?有没有人试过这个?我对此进行了很多搜索,但没有得到帮助。

任何帮助表示赞赏。

谢谢

4

2 回答 2

3

不,因为没有 AES-512 位加密之类的东西。FIPS 197中只有 NIST 指定的 AES-128、AES-192 和 AES-256 位加密

于 2013-09-02T17:20:49.157 回答
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);
于 2013-09-02T12:55:54.210 回答