Android中常用的加密方式[通俗易懂]

Android中常用的加密方式[通俗易懂]Android中常用的加密方式HmacSHA1publicstaticStringgetSignUtil(Stringkey,Stringbase){Log.i(TAG,”getSignUtil:GETSIGN”);Stringtype=”HmacSHA1″;SecretKeySpecsecret=newSecretKeySpec(key.getBytes(),type);Macmac=null;try{

大家好,又见面了,我是你们的朋友全栈君。

Android中常用的加密方式

HmacSHA1

public static String getSignUtil(String key ,String base) {
    Log.i(TAG, "getSignUtil: GET SIGN");
    String type = "HmacSHA1";
    SecretKeySpec secret = new SecretKeySpec(key.getBytes(), type);
    Mac mac = null;
    try {
        mac = Mac.getInstance(type);
        mac.init(secret);
        byte[] digest = mac.doFinal(base.getBytes());
        Log.i(TAG, "authorization:  " + Base64.encodeToString(digest, Base64.DEFAULT));
        return Base64.encodeToString(digest, Base64.DEFAULT);
    } catch (Exception e) {
        Log.e(TAG, "getSignUtil: " + e.toString());
    }
    return type;
}

MD5 and RSA

public class DigestUtils {

public static String TAG = “DigestUtils”;
private static final char[] hexCode = “0123456789ABCDEF”.toCharArray();

public static String md5(String input) {
    byte[] bytes = new byte[0];
    try {
        bytes = MessageDigest.getInstance("MD5").digest(input.getBytes());
    } catch (NoSuchAlgorithmException e) {
        Log.e(TAG, "md5: " + e.toString());
    }
    return printHexBinary(bytes);
}

public static String printHexBinary(byte[] data) {
    StringBuilder r = new StringBuilder(data.length * 2);
    for (byte b : data) {
        r.append(hexCode[(b >> 4) & 0xF]);
        r.append(hexCode[(b & 0xF)]);
    }
    return r.toString();
}


/**
 * RSA私钥解密
 *
 * @param message    加密字符串
 * @param privateKey 私钥
 * @return铭文
 */

public static String decrypt(String message, String privateKey) {
    try {
        if (message.contains(" ")) {
            message = message.replaceAll(" " , "+");
        }
        //base64编码的私钥
        final byte[] decoded = Base64.decode(privateKey, Base64.DEFAULT);

        //final byte[] inputByte = Base64.decodeBase64(message.getBytes(StandardCharsets.UTF_8))

        //decodeBase64(privateKey);
        RSAPrivateKey priKey = (RSAPrivateKey) KeyFactory.getInstance("RSA").generatePrivate(new PKCS8EncodedKeySpec(decoded));
        Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding");
        cipher.init(Cipher.DECRYPT_MODE, priKey);
        //64位解码加密后的字符串
        final byte[] inputByte = Base64.decode(message.getBytes(StandardCharsets.UTF_8), Base64.DEFAULT);

        //  decodeBase64(message.getBytes(StandardCharsets.UTF_8));
        //密文
        final int len = inputByte.length;
        //偏移量
        int offset = 0;
        //段数
        int i = 0;
        final ByteArrayOutputStream bos = new ByteArrayOutputStream();
        while (len - offset > 0) {
            byte[] cache;
            if
            (len - offset > 128) {
                cache = cipher.doFinal(inputByte, offset, 128);
            } else {
                cache = cipher.doFinal(inputByte, offset, len - offset);
            }
            bos.write(cache);
            i++;
            offset = 128 * i;
        }
        bos.close();
        return new String(bos.toByteArray(), StandardCharsets.UTF_8);
    } catch (InvalidKeyException | InvalidKeySpecException | NoSuchAlgorithmException | NoSuchPaddingException
            | IllegalBlockSizeException | BadPaddingException | IOException e) {
        Log.e(TAG, String.format("decrypt: 数据解密异常 , 原始数据: %s,密钥: %s,e: %s " , message, privateKey, e));
    }
    return null;
}




/**
 * RSA私钥解密
 *
 * @param message    加密字符串
 * @param privateKey 私钥
 * @return 铭文
 */

public static String decrypt2(String message, String privateKey) {
    try {
        if (message.contains(" ")) {
            message = message.replaceAll(" ", "+");
        }
        //base64编码的私钥
        final byte[] decoded = Base64.decode(privateKey.getBytes(StandardCharsets.UTF_8),0);


      //  final byte[] decoded = Base64Utils.decode(privateKey);
        RSAPrivateKey priKey = (RSAPrivateKey) KeyFactory.getInstance("RSA").generatePrivate(new PKCS8EncodedKeySpec(decoded));
        Cipher cipher = Cipher.getInstance("RSA");
        cipher.init(Cipher.DECRYPT_MODE, priKey);
        //64位解码加密后的字符串
        final byte[] inputByte = Base64.decode(message.getBytes(StandardCharsets.UTF_8),0);
        //密文
        final int len = inputByte.length;
        //偏移量
        int offset = 0;
        //段数
        int i = 0;
        final ByteArrayOutputStream bos = new ByteArrayOutputStream();
        while (len - offset > 0) {
            byte[] cache;
            if (len - offset > 128) {
                cache = cipher.doFinal(inputByte, offset, 128);
            } else {
                cache = cipher.doFinal(inputByte, offset, len - offset);
            }
            bos.write(cache);
            i++;
            offset = 128 * i;
        }
        bos.close();

        return new String(bos.toByteArray(),StandardCharsets.UTF_8);
    } catch (InvalidKeyException | InvalidKeySpecException | NoSuchAlgorithmException
            | NoSuchPaddingException | IllegalBlockSizeException | BadPaddingException | IOException e) {
        Log.e(TAG, String.format("decrypt: 数据解密异常 , 原始数据: %s,密钥: %s,e: %s " , message, privateKey, e));
    }
    return null;
}

}

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/145921.html原文链接:https://javaforall.net

(0)
上一篇 2022年5月13日 下午5:20
下一篇 2022年5月13日 下午5:40


相关推荐

  • phpstrom2021.5 激活码[在线序列号]

    phpstrom2021.5 激活码[在线序列号],https://javaforall.net/100143.html。详细ieda激活码不妨到全栈程序员必看教程网一起来了解一下吧!

    2022年3月20日
    54
  • 开源Manus替代:智谱AutoGLM沉思来了

    开源Manus替代:智谱AutoGLM沉思来了

    2026年3月12日
    1
  • STM32H743 TFTP Boot 简录

    STM32H743 TFTP Boot 简录文章目录 stm32h743 存储简况区域划分分散加载问题引出默认的分散加载文件修改分散加载文件 CubeMX 部分配置 boot 的 jump to appapp 中断向量表偏移 app 分散加载文件修改最简升级 py 脚本 MCU 处理参考 stm32h743 存储简况如下 2MBFlash 分 2 个 bank 存储区 可在两个 banks 并行执行读 编程 擦除操作 1Flash Word 8Words 32Bytes 256bits 其实 1Flash Word 还有额外的 10bitsECC

    2026年3月17日
    2
  • python的缩进规则具体是什么_python语言中的缩进

    python的缩进规则具体是什么_python语言中的缩进python的缩进规则有哪些发布时间:2020-09-2315:18:50来源:亿速云阅读:70作者:Leah本篇文章给大家分享的是有关python的缩进规则有哪些,小编觉得挺实用的,因此分享给大家学习,希望大家阅读完这篇文章后可以有所收获,话不多说,跟着小编一起来看看吧。一般的语言都是通过{}或end来作为代码块的标记,而Python则是通过缩进来识别代码块的。对于Python的这种“缩进”风…

    2022年10月13日
    6
  • 基于近邻的协同过滤算法「建议收藏」

    基于近邻的协同过滤算法「建议收藏」这节课我们来学习K近邻在推荐系统中的应用,你将完成本课程的第一个实战项目:基于KNN的电影推荐系统!为了使你能够顺利地完成实战内容,我们先了解一下推荐系统中的基础知识。基于近邻用户的协同过滤假定有一个场景:某个周日的下午,你感觉很无聊,然后从电脑上打开了一个视频网站,想看下最近有什么好看的电影。然而你发现网站上的热门电影基本都看过,其他的电影又太多,不知道该看什么。想使用搜索框去查一下,但是又不知道该搜什么关键词,这个时候你的内心很焦灼,总不能挨个去尝试吧,那时间成本也太大了…仔细想想还是有办法的,那

    2022年6月30日
    29
  • idea2021激活码永久【注册码】

    idea2021激活码永久【注册码】,https://javaforall.net/100143.html。详细ieda激活码不妨到全栈程序员必看教程网一起来了解一下吧!

    2022年3月19日
    52

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号