在Android中加密解密字符串的简便方法

在Android中加密解密字符串的简便方法,第1张

概述我的问题是如何加密字符串:StringAndroidId;@OverridepublicvoidonCreate(BundlesavedInstanceState){super.onCreate(savedInstanceState);setContentView(R.layout.download_movie_activity);cancel=(Button)findViewById(R.id.img_cancle);linear=(Li

我的问题是如何加密字符串:

String AndroIDID;@OverrIDepublic voID onCreate(Bundle savedInstanceState) {  super.onCreate(savedInstanceState);  setContentVIEw(R.layout.download_movIE_activity);  cancel = (button)findVIEwByID(R.ID.img_cancle);  linear= (linearLayout)findVIEwByID(R.ID.progress);  linear.setVisibility(VIEw.GONE);  String encrypted = "MzIyNTE2" + "OTQNzM4NTQ=";  Log.e("Encrypt", encrypted);  WifiManager wifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE);  WifiInfo wInfo = wifiManager.getConnectionInfo();  AndroIDID = wInfo.getMacAddress();  AndroIDID=encrypted;

如何加密我存储MAC地址的AndroIDID.

解决方法:

您可以使用Cipher.

此类提供用于加密和解密的加密密码的功能.它构成了Java Cryptographic Extension(JCE)框架的核心.

加密和解密样本:

public static SecretKey generateKey()     throws NoSuchAlgorithmException, InvalIDKeySpecException {     return secret = new SecretKeySpec(password.getBytes(), "AES"); }public static byte[] encryptMsg(String message, SecretKey secret)    throws NoSuchAlgorithmException, NoSuchpaddingException, InvalIDKeyException, InvalIDParameterSpecException, IllegalBlockSizeException, BadpaddingException, UnsupportedEnCodingException {    /* Encrypt the message. */   Cipher cipher = null;    cipher = Cipher.getInstance("AES/ECB/PKCS5padding");   cipher.init(Cipher.ENCRYPT_MODE, secret);    byte[] cipherText = cipher.doFinal(message.getBytes("UTF-8"));    return cipherText; }public static String decryptMsg(byte[] cipherText, SecretKey secret)     throws NoSuchpaddingException, NoSuchAlgorithmException, InvalIDParameterSpecException, InvalIDAlgorithmParameterException, InvalIDKeyException, BadpaddingException, IllegalBlockSizeException, UnsupportedEnCodingException {    /* Decrypt the message, given derived encContentValues and initialization vector. */    Cipher cipher = null;    cipher = Cipher.getInstance("AES/ECB/PKCS5padding");    cipher.init(Cipher.DECRYPT_MODE, secret);     String decryptString = new String(cipher.doFinal(cipherText), "UTF-8");    return decryptString; }

要加密:

SecretKey secret = generateKey();EncUtil.encryptMsg(String toEncrypt, secret))

要解密:

EncUtil.decryptMsg(byte[] toDecrypt, secret))
总结

以上是内存溢出为你收集整理的在Android中加密/解密字符串的简便方法全部内容,希望文章能够帮你解决在Android中加密/解密字符串的简便方法所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1101545.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-28
下一篇 2022-05-28

发表评论

登录后才能评论

评论列表(0条)

保存