Android加密/解密
创始人
2024-10-08 12:02:28
0

在Android中进行加密和解密操作通常使用Java的加密库和算法。下面是一个使用AES加密/解密的示例代码:

import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;
import android.util.Base64;

public class EncryptionUtils {

    private static final String ALGORITHM = "AES";
    private static final String KEY = "your_key_here";

    public static String encrypt(String value) throws Exception {
        SecretKeySpec secretKeySpec = new SecretKeySpec(KEY.getBytes(), ALGORITHM);
        Cipher cipher = Cipher.getInstance(ALGORITHM);
        cipher.init(Cipher.ENCRYPT_MODE, secretKeySpec);
        byte[] encryptedBytes = cipher.doFinal(value.getBytes());
        return Base64.encodeToString(encryptedBytes, Base64.DEFAULT);
    }

    public static String decrypt(String encryptedValue) throws Exception {
        SecretKeySpec secretKeySpec = new SecretKeySpec(KEY.getBytes(), ALGORITHM);
        Cipher cipher = Cipher.getInstance(ALGORITHM);
        cipher.init(Cipher.DECRYPT_MODE, secretKeySpec);
        byte[] decryptedBytes = cipher.doFinal(Base64.decode(encryptedValue, Base64.DEFAULT));
        return new String(decryptedBytes);
    }
}

在上面的示例中,encrypt()方法使用AES算法对传入的字符串进行加密,并返回Base64编码后的加密结果。decrypt()方法则对加密后的字符串进行解密。

你需要将KEY替换为你自己的密钥。请注意,密钥的长度必须符合AES算法的要求,通常为16字节(128位)、24字节(192位)或32字节(256位)。

使用示例:

try {
    String originalText = "Hello, World!";
    String encryptedText = EncryptionUtils.encrypt(originalText);
    String decryptedText = EncryptionUtils.decrypt(encryptedText);

    Log.d("EncryptionUtils", "Original Text: " + originalText);
    Log.d("EncryptionUtils", "Encrypted Text: " + encryptedText);
    Log.d("EncryptionUtils", "Decrypted Text: " + decryptedText);
} catch (Exception e) {
    e.printStackTrace();
}

上面的代码将输出原始文本、加密后的文本和解密后的文本。

相关内容

热门资讯

软件辅助挂!wepoker手机... 软件辅助挂!wepoker手机助手,途游四川小程序好运卡,资料教程(确实有挂)途游四川小程序好运卡是...
长期以来!hhpoker作弊码... 长期以来!hhpoker作弊码怎么用,西部透视辅助,手册教程(有挂实锤)1、西部透视辅助破解器简单,...
据目击者称!wepoker辅助... 据目击者称!wepoker辅助器怎么弄,贵阳辅助工具,技法教程(确实有挂)贵阳辅助工具辅助器是一种具...
黑科技辅助!wepoker作弊... 黑科技辅助!wepoker作弊辅助,雀神麻将小程序辅助软件,讲义教程(竟然有挂)雀神麻将小程序辅助软...
总结辅助挂!wepoker破解... 总结辅助挂!wepoker破解器激活码,微信小程序修改器,积累教程(有挂助手)1、起透看视 微信小程...
不少玩家反映!wepoker有... 不少玩家反映!wepoker有脚本吗,0759湛江吴川三脚鸡辅助,秘籍教程(有挂教学)1、玩家可以在...
目前来看!aapoker插件,... 目前来看!aapoker插件,五十k辅助,手段教程(有挂方法)1、打开软件启动之后找到中间准星的标志...
随着!sohoopoker辅助... 随着!sohoopoker辅助,德扑之星插件,办法教程(有挂存在)1、完成德扑之星插件辅助器v3.3...
教学辅助挂!拱趴大菠萝怎么开挂... 教学辅助挂!拱趴大菠萝怎么开挂,蜀渝牌乐汇辅助器,资料教程(有挂猫腻)蜀渝牌乐汇辅助器脚本下载中分为...
2026版辅助挂!约局吧德州真... 2026版辅助挂!约局吧德州真的存在透视吗,潮友会app下载安卓辅助软件,指南教程(确实有挂)1、玩...