gpt4 book ai didi

android - 在 Android 中加密/解密字符串的简单方法

转载 作者:行者123 更新时间:2023-12-03 14:29:22 28 4
gpt4 key购买 nike

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

String AndroidId;

@Override
public 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 加密扩展 (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 中加密/解密字符串的简单方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40123319/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com