本文中的示例说明了java实现的AES加密算法。 共享仅供参考。 具体情况如下。
import javax.crypto.Cipher;
import javax.crypto.spec.ivparameterspec;
import javax.crypto.spec.secret key spec;
import android.util.Base64;
//*
公共类AES crypt {
private final Cipher cipher;
私有文件安全密钥特殊密钥;
privatealgorithmparameterspecspec;
publicstaticfinalstringseed _ 16 _ character=' u1 mju1m0 fdo uz.qz ';
PublicAEScrypt(throwsexception )。
//hashpasswordwithsha-256 andcroptheoutputto 128-bitforkey
digest.update (seed _ 16 _ character.getbytes (' utf-8 ' ) );
byte[] keyBytes=new byte[32];
system.arraycopy(digest.digest ),0,keyBytes,0,keyBytes.length );
cipher=cipher.getinstance (AES/CBC/pkcs7padding );
key=newsecretkeyspec(keybytes,' AES ';
spec=getIV (;
}
publicalgorithmparameterspecgetiv (
byte [ ] iv={ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,
ivparameterspec=newivparameterspec (iv );
返回ivparameterspec;
}
公共字符串加密(string plaintext ) throws Exception {
cipher.init (cipher.encrypt _ mode,key,spec );
byte [ ] encrypted=cipher.do final (plaintext.getbytes (' utf-8 ' ) );
stringencryptedtext=new string (base64.encode ) encrypted,
Base64.DEFAULT ),' UTF-8 ';
return encryptedText;
}
公共字符串描述(stringcryptedtext ) throws Exception {
cipher.init (cipher.decrypt _ mode,key,spec );
byte [ ] bytes=base64.decode (crypted text,Base64.DEFAULT );
byte [ ] decrypted=cipher.do final (bytes );
stringdecryptedtext=new string (decrypted,' UTF-8 ' );
return decryptedText;
}
}
希望本文的描述对大家的java编程有帮助。