md5 MD5加密

应用

/**
 * 利用MD5进行加密
 *
 * @param str 待加密的字符串
 * @return 加密后的字符串
 * @throws NoSuchAlgorithmException     没有这种产生消息摘要的算法
 * @throws UnsupportedEncodingException
 */

public String EncoderByMd5(String str) throws NoSuchAlgorithmException, UnsupportedEncodingException {// 确定计算方法
    MessageDigest md5 = MessageDigest.getInstance("MD5");

    BASE64Encoder base64en = new BASE64Encoder();
    // 加密后的字符串
    String newstr = base64en.encode(md5.digest(str.getBytes("utf-8")));
    return newstr;
}

两个工具类搞定

import java.io.IOException;
import java.io.OutputStream;

public class BASE64Encoder extends CharacterEncoder {private static final char[] pem_array = new char[]{'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '+', '/'};

    public BASE64Encoder() {}protected int bytesPerAtom() {return 3;
    }protected int bytesPerLine() {return 57;
    }protected void encodeAtom(OutputStream outStream, byte[] data, int offset, int len) throws IOException {byte a;
        if (len == 1) {a = data[offset];
            byte b = 0;
            outStream.write(pem_array[a >>> 2 & 63]);
            outStream.write(pem_array[(a << 4 & 48) + (b >>> 4 & 15)]);
            outStream.write(61);
            outStream.write(61);
        } else {byte b;
            if (len == 2) {a = data[offset];
                b = data[offset + 1];
                byte c = 0;
                outStream.write(pem_array[a >>> 2 & 63]);
                outStream.write(pem_array[(a << 4 & 48) + (b >>> 4 & 15)]);
                outStream.write(pem_array[(b << 2 & 60) + (c >>> 6 & 3)]);
                outStream.write(61);
            } else {a = data[offset];
                b = data[offset + 1];
                byte c = data[offset + 2];
                outStream.write(pem_array[a >>> 2 & 63]);
                outStream.write(pem_array[(a << 4 & 48) + (b >>> 4 & 15)]);
                outStream.write(pem_array[(b << 2 & 60) + (c >>> 6 & 3)]);
                outStream.write(pem_array[c & 63]);
            }}}
}
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintStream;
import java.nio.ByteBuffer;

public abstract class CharacterEncoder {protected PrintStream pStream;

    public CharacterEncoder() {}protected abstract int bytesPerAtom();

    protected abstract int bytesPerLine();

    protected void encodeBufferPrefix(OutputStream aStream) throws IOException {this.pStream = new PrintStream(aStream);
    }protected void encodeBufferSuffix(OutputStream aStream) throws IOException {}protected void encodeLinePrefix(OutputStream aStream, int aLength) throws IOException {}protected void encodeLineSuffix(OutputStream aStream) throws IOException {this.pStream.println();
    }protected abstract void encodeAtom(OutputStream var1, byte[] var2, int var3, int var4) throws IOException;

    protected int readFully(InputStream in, byte[] buffer) throws IOException {for(int i = 0; i < buffer.length; ++i) {int q = in.read();
            if (q == -1) {return i;
            }buffer[i] = (byte)q;
        }return buffer.length;
    }public void encode(InputStream inStream, OutputStream outStream) throws IOException {byte[] tmpbuffer = new byte[this.bytesPerLine()];
        this.encodeBufferPrefix(outStream);

        while(true) {int numBytes = this.readFully(inStream, tmpbuffer);
            if (numBytes == 0) {break;
            }this.encodeLinePrefix(outStream, numBytes);

            for(int j = 0; j < numBytes; j += this.bytesPerAtom()) {if (j + this.bytesPerAtom() <= numBytes) {this.encodeAtom(outStream, tmpbuffer, j, this.bytesPerAtom());
                } else {this.encodeAtom(outStream, tmpbuffer, j, numBytes - j);
                }}if (numBytes < this.bytesPerLine()) {break;
            }this.encodeLineSuffix(outStream);
        }this.encodeBufferSuffix(outStream);
    }public void encode(byte[] aBuffer, OutputStream aStream) throws IOException {ByteArrayInputStream inStream = new ByteArrayInputStream(aBuffer);
        this.encode((InputStream)inStream, aStream);
    }public String encode(byte[] aBuffer) {ByteArrayOutputStream outStream = new ByteArrayOutputStream();
        ByteArrayInputStream inStream = new ByteArrayInputStream(aBuffer);
        String retVal = null;

        try {this.encode((InputStream)inStream, outStream);
            retVal = outStream.toString("8859_1");
            return retVal;
        } catch (Exception var6) {throw new Error("CharacterEncoder.encode internal error");
        }}private byte[] getBytes(ByteBuffer bb) {byte[] buf = null;
        if (bb.hasArray()) {byte[] tmp = bb.array();
            if (tmp.length == bb.capacity() && tmp.length == bb.remaining()) {buf = tmp;
                bb.position(bb.limit());
            }}if (buf == null) {buf = new byte[bb.remaining()];
            bb.get(buf);
        }return buf;
    }public void encode(ByteBuffer aBuffer, OutputStream aStream) throws IOException {byte[] buf = this.getBytes(aBuffer);
        this.encode(buf, aStream);
    }public String encode(ByteBuffer aBuffer) {byte[] buf = this.getBytes(aBuffer);
        return this.encode(buf);
    }public void encodeBuffer(InputStream inStream, OutputStream outStream) throws IOException {byte[] tmpbuffer = new byte[this.bytesPerLine()];
        this.encodeBufferPrefix(outStream);

        int numBytes;
        do {numBytes = this.readFully(inStream, tmpbuffer);
            if (numBytes == 0) {break;
            }this.encodeLinePrefix(outStream, numBytes);

            for(int j = 0; j < numBytes; j += this.bytesPerAtom()) {if (j + this.bytesPerAtom() <= numBytes) {this.encodeAtom(outStream, tmpbuffer, j, this.bytesPerAtom());
                } else {this.encodeAtom(outStream, tmpbuffer, j, numBytes - j);
                }}this.encodeLineSuffix(outStream);
        } while(numBytes >= this.bytesPerLine());

        this.encodeBufferSuffix(outStream);
    }public void encodeBuffer(byte[] aBuffer, OutputStream aStream) throws IOException {ByteArrayInputStream inStream = new ByteArrayInputStream(aBuffer);
        this.encodeBuffer((InputStream)inStream, aStream);
    }public String encodeBuffer(byte[] aBuffer) {ByteArrayOutputStream outStream = new ByteArrayOutputStream();
        ByteArrayInputStream inStream = new ByteArrayInputStream(aBuffer);

        try {this.encodeBuffer((InputStream)inStream, outStream);
        } catch (Exception var5) {throw new Error("CharacterEncoder.encodeBuffer internal error");
        }return outStream.toString();
    }public void encodeBuffer(ByteBuffer aBuffer, OutputStream aStream) throws IOException {byte[] buf = this.getBytes(aBuffer);
        this.encodeBuffer(buf, aStream);
    }public String encodeBuffer(ByteBuffer aBuffer) {byte[] buf = this.getBytes(aBuffer);
        return this.encodeBuffer(buf);
    }
}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/415610.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

[UVA 10891] Game of Sum

图片加载可能有点慢&#xff0c;请跳过题面先看题解&#xff0c;谢谢 很容易想到这样一个状态&#xff1a;\(dp[l][r]\) 表示&#xff0c;\(l\) 到 \(r\) 这一段区间&#xff0c;双方都使用最优策略时&#xff0c;先手能得到的最大分数 $ $ 那么这个只要怎么求呢&#xff0c;想…

hapi 插件注册 核心代码

准备给自己的hapi框架加上微信开发库这样的插件&#xff0c;需要弄懂hapi如何注册插件、如何给插件传递参数。 1、定义插件 const Pkg require(../package.json) async function register(server, pluginOptions) {console.log(这是一个插件);console.log(这是插件参数);cons…

工作120:富文本组件封装

<template lang"html"><div class"editor"><!--定义的为表头的属性--><div ref"toolbar" class"toolbar"></div><!--定义的为表格的属性--><div ref"editor" class"text"…

mysql 常用命令与备份恢复 整理

常用命令 编辑1:使用SHOW语句找出在服务器上当前存在什么数据库&#xff1a;mysql> SHOW DATABASES;2:2、创建一个数据库MYSQLDATAmysql> CREATE DATABASE MYSQLDATA;3:选择你所创建的数据库mysql> USE MYSQLDATA; (按回车键出现Database changed 时说明操作成功&…

/lib/libcrypto.so“ not found,is 32-bit instead of 64-bit

关于百度导航与百度云推送冲突 只需要加入红色部分 defaultConfig {applicationId "com.tianxin.mient.leapp"minSdkVersion 15targetSdkVersion 27versionCode 19versionName "19.0"// multiDexEnabled truejavaCompileOptions {annotationProces…

运用Zabbix实现内网服务器状态及局域网状况监控(2) —— 环境配置

一、基本要求 Zabbix支持如下操作系统&#xff1a; LinuxIBM AIXFreeBSDNetBSDOpenBSDHP-UXMac OS XSolarisWindows: 2000, Server 2003, XP, Vista, Server 2008, 7, 8, Server 2012 (只能跑 Zabbix agent) 软件需要&#xff1a; 数据库 MySQL&#xff1a; 5.0.3 或者以上&am…

lodash 常用的方法总结(持续更新)

lodash的引入 var _ require(lodash);castArray _.castArray将一个值铸造为数组如果它不是数组类型。 _.castArray(1); // > [1]_.castArray({ a: 1 }); // > [{ a: 1 }]_.castArray(abc); // > [abc]_.castArray(null); // > [null]_.castArray(undefined); //…

WEB技术分类

HTMLXHTML ▪ HTML 5 ▪ CSS ▪ TCP/IPXMLXML ▪ XSL ▪ XSLT ▪ XSL-FO ▪ XPath ▪ XPointer ▪ XLink ▪ DTD ▪ XML Schema ▪ DOM ▪ XForms ▪ SOAP ▪WSDL ▪ RDF ▪ RSS ▪ WAP ▪ Web ServicesWeb脚本JavaScript ▪ HTML DOM ▪ DHTML ▪ VBScript ▪ AJAX ▪ jQuery …

Linux入门——适合初学者

Linux入门——适合初学者学习Linux也有一阵子了&#xff0c;这过程中磕磕撞撞的&#xff0c;遇到了问题&#xff0c;也解决了一些问题&#xff0c;学习的路子是曲折的&#xff0c;想总结点啥的&#xff0c;让刚刚学习Linux的不会望而生畏。 为啥我们要学习Linux 技术的价值不…

Tomcat 配置 login 和 gas

1.首先下载tomcat 2.配置环境变量 export PATH$PATH:/Users/wangchengcheng/Downloads/UtilitySoftWare/Work/ServerTools/apache-tomcat-7.0.82/bin 3.发布login gms 的部署包 4. 修改 server.xml /Users/wangchengcheng/Downloads/UtilitySoftWare/Work/ServerTools/apache-…

1 微信公众号开发 服务器配置 有什么用

启用并设置服务器配置后&#xff0c;用户发给公众号的消息以及开发者需要的事件推送&#xff0c;将被微信转发到该URL中。 换句话说&#xff0c;开发者需要监听这个URL&#xff0c;处理数据&#xff0c;并做出反应。

Android极光推送,Manifest merger failed with multiple errors, see logs

极光推送加载jar包时&#xff0c;报错 Manifest merger failed with multiple errors, see logs解决方法 加入红色部分defaultConfig {applicationId "com.nxin.client.liteapp"minSdkVersion 15targetSdkVersion 27versionCode 19versionName "19.0"// …

svn在linux下的使用(转)

svn在linux下的使用(转)ubuntu命令行模式操作svn 首先要安装SVN客户端到你的系统才能操作各种命令 apt-get install subversion 1、将文件checkout到本地目录 svn checkout path&#xff08;path是服务器上的目录&#xff09; 例如&#xff1a;svn checkout svn://192.168.1.1…

[Bzoj4540][Hnoi2016] 序列(莫队 + ST表 + 单调队列)

4540: [Hnoi2016]序列 Time Limit: 20 Sec Memory Limit: 512 MBSubmit: 1567 Solved: 718[Submit][Status][Discuss]Description 给定长度为n的序列&#xff1a;a1,a2,…,an&#xff0c;记为a[1:n]。类似地&#xff0c;a[l:r]&#xff08;1≤l≤r≤N&#xff09;是指序列&am…

2 微信公众号开发 服务器配置 Token验证

服务器配置的主要难点就是Token验证。 官方文档&#xff1a;https://mp.weixin.qq.com/wiki?tresource/res_main&idmp1445241432 接入指南&#xff1a;https://mp.weixin.qq.com/wiki?tresource/res_main&idmp1421135319 用户服务器端主要需要做的工作&#xff0c;…

Android 10分钟集成极光推送

1、首先申请key https://www.jiguang.cn/accounts/login/form 2、app build.gradle添加jar依赖 compile cn.jiguang.sdk:jpush:3.0.7 compile cn.jiguang.sdk:jcore:1.1.3 compile me.leolin:ShortcutBadger:1.1.16aar//消息桌面显示 注意最新依赖是&#xff1a; compile c…

Python requests介绍之接口介绍

Python requests介绍 引用官网介绍 Requests 唯一的一个非转基因的 Python HTTP 库&#xff0c;人类可以安全享用。 Requests 允许你发送纯天然&#xff0c;植物饲养的 HTTP/1.1 请求&#xff0c;无需手工劳动。你不需要手动为 URL 添加查询字串&#xff0c;也不需要对 POST 数…