Android 中MD5算法生成字符串

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/Rodulf/article/details/50958129
public class MainActivity extends AppCompatActivity {

    private String mPrivateKeyString;
    private String mPublicKeyString;
    private PublicKey mPublic;
    private PrivateKey mPrivate;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
      
        //测试md5算法
        md5Test();
    }

    public void md5Test(){
        String source = "abcdef";
        String md5CreatedString = md5String(source);
        Log.d("kodulf","MD5 算法得到的String:"+md5CreatedString);
    }
    //md5算法生成String
    private String md5String(String url) {
        String ret = null;

        if (url != null) {

            try {
                // 创建MD5的消息摘要算法的类,进行调用
                MessageDigest digest = MessageDigest.getInstance("MD5");

                // 计算出一个唯一识别的信息;
                byte[] data = digest.digest(url.getBytes());

                StringBuilder sb = new StringBuilder();

                // 字节数组转换为十六进制字符串
                for (byte b : data) {
                    int ib = b & 0x0FF;
                    String s = Integer.toHexString(ib);

                    if(ib < 16){  // 15 -> 0F 0 -> 00
                        sb.append('0');
                    }
                    sb.append(s);
                }

                ret = sb.toString();
            } catch (NoSuchAlgorithmException e) {
                e.printStackTrace();
            }
        }

        return ret;
    }
}

展开阅读全文

没有更多推荐了,返回首页