Android - 字符串简单的加密和解密

提示:以下是本篇文章正文内容,下面案例可供参考

一、1.字符串的加密

 /**
     * 加密
     **/
    private String encryptPassword(String clearText) {
        try {
            DESKeySpec keySpec = new DESKeySpec(
                    MyConstant.PASSWORD_ENC_SECRET.getBytes("UTF-8"));
            SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
            SecretKey key = keyFactory.generateSecret(keySpec);

            Cipher cipher = Cipher.getInstance("DES");
            cipher.init(Cipher.ENCRYPT_MODE, key);
            String encrypedPwd = Base64.encodeToString(cipher.doFinal(clearText
                    .getBytes("UTF-8")), Base64.DEFAULT);
            return encrypedPwd;
        } catch (Exception e) {
        }
        return clearText;
    }

二、字符串的解密

 /**
     * 解密
     **/
    private String decryptPassword(String encryptedPwd) {
        try {
            DESKeySpec keySpec = new DESKeySpec(MyConstant.PASSWORD_ENC_SECRET.getBytes("UTF-8"));
            SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
            SecretKey key = keyFactory.generateSecret(keySpec);

            byte[] encryptedWithoutB64 = Base64.decode(encryptedPwd, Base64.DEFAULT);
            Cipher cipher = Cipher.getInstance("DES");
            cipher.init(Cipher.DECRYPT_MODE, key);
            byte[] plainTextPwdBytes = cipher.doFinal(encryptedWithoutB64);
            return new String(plainTextPwdBytes);
        } catch (Exception e) {
        }
        return encryptedPwd;
    }

三、备注:常量PASSWORD_ENC_SECRET可自行定义。

public class MyConstant {
    public static final String PASSWORD_ENC_SECRET = "mythmayor";
}
  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Android可以通过使用Java中的加解密算法来实现对字符串进行加解密。常见的加解密算法包括AES、DES、RSA等。 以下是一个使用AES加解密字符串的示例代码: ```java import javax.crypto.Cipher; import javax.crypto.spec.SecretKeySpec; import android.util.Base64; public class AESEncryption { private static final String ALGORITHM = "AES"; private static final String KEY = "your_key_here"; // 16位或32位的密钥 public static String encrypt(String value) throws Exception { SecretKeySpec key = generateKey(); Cipher cipher = Cipher.getInstance(ALGORITHM); cipher.init(Cipher.ENCRYPT_MODE, key); byte[] encryptedByteValue = cipher.doFinal(value.getBytes("utf-8")); String encryptedValue64 = Base64.encodeToString(encryptedByteValue, Base64.DEFAULT); return encryptedValue64; } public static String decrypt(String value) throws Exception { SecretKeySpec key = generateKey(); Cipher cipher = Cipher.getInstance(ALGORITHM); cipher.init(Cipher.DECRYPT_MODE, key); byte[] decryptedValue64 = Base64.decode(value, Base64.DEFAULT); byte[] decryptedByteValue = cipher.doFinal(decryptedValue64); String decryptedValue = new String(decryptedByteValue,"utf-8"); return decryptedValue; } private static SecretKeySpec generateKey() throws Exception { byte[] keyAsBytes; keyAsBytes = KEY.getBytes("utf-8"); SecretKeySpec keySpec = new SecretKeySpec(keyAsBytes, ALGORITHM); return keySpec; } } ``` 在使用时,可以使用`encrypt()`方法对需要加密字符串进行加密,使用`decrypt()`方法对加密后的字符串进行解密。注意,加解密时需要使用相同的密钥。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值