MessageDigest的更新方法是什么,BASE64Encoder是什么意思? [英] What does update method of MessageDigest do and what is BASE64Encoder meant for?

查看:194
本文介绍了MessageDigest的更新方法是什么,BASE64Encoder是什么意思?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下是一个将加密用户的代码String:

Following is a code that will encrypts the user String :

import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import sun.misc.BASE64Encoder;
import java.io.*;

class Encrypter {
public synchronized String encrypt(String plainText) throws Exception {
    MessageDigest md = null;
    try {
        md = MessageDigest.getInstance("SHA");
    }catch(Exception exc) {
        throw new Exception(exc.getMessage());
     }

     try {
        md.update(plainText.getBytes("UTF-8"));
     }catch(Exception exc) {
        throw new Exception(exc.getMessage());
      }

      byte raw[] = md.digest();
      String hash = (new BASE64Encoder()).encode(raw);
      return hash;
}
public static void main(String args[]) {
    try {
        Encrypter encrypter = new Encrypter();
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String userInput = br.readLine();
        String encryptedPassword = encrypter.encrypt(userInput);
        System.out.println(encryptedPassword);
    } catch(Exception exc) {
        System.out.println(exc);
      }
}
}

当我编译代码得到以下警告:

When i compile the code i get the these warnings :

Encrypter.java:4: warning: BASE64Encoder is internal proprietary API and may be removed in a future release
import sun.misc.BASE64Encoder;
           ^
Encrypter.java:23: warning: BASE64Encoder is internal proprietary API and may be removed in a future release
              String hash = (new BASE64Encoder()).encode(raw);
                                 ^
2 warnings

是否有其他方法加密字符串在java?

方法更新 MessageDigest 做? ie是什么语句 md.update(plainText.getBytes(UTF-8)); do?

What does the method update of class MessageDigest do ? i.e What does the statement md.update(plainText.getBytes("UTF-8")); do ?

什么是 BASE64Encoder 类?我找不到它的DOC

What is a BASE64Encoder class ? I couldn't find it's DOC

推荐答案


  1. 您没有执行任何加密。您正在计算输入的单向哈希或摘要。此散列可以稍后用于验证消息的完整性。请参阅哈希 SHA1 消息摘要

Base64 encoding 是用ASCII表示二进制数据的方法。这通常是可取的,因为并非所有数据存储和传输机制都支持原始二进制。例如,如果要通过http查询字符串参数传输计算的摘要,则需要将其编码为Base64。此外,将原始二进制文件保存或打印到控制台将产生可能在可打印范围之外的时髦字符流,并且还可能会从PC扬声器产生哔声!

Base64 encoding is a method of representing binary data in ASCII. This is often desirable because not all data storage and transmission mechanisms support raw binary. For example, if you want to transfer your computed digest via an http query string parameter, you'll want to encode it as Base64. Also, saving or printing raw binary to the console will produce a stream of funky characters which may be outside of the printable range, and may also produce beeps from your PC speaker!

您正在使用的 Base64Encoder 来自 sun.misc 包,不应该使用。这是内部Sun JVM代码,将来可能或可能不可用。这也解释了为什么你不能找到任何javadoc。

The Base64Encoder you're using comes from the sun.misc package and should NEVER be used. This is internal Sun JVM code which may or may not be available in the future. That also explains why you're weren't able to find any javadoc.

幸运的是,有几个免费开放的Base64编码器和解码器。 Apache Commons Codec 是一个广泛使用和稳定的库,其中包含几个编解码器,包括 Base64

Fortunately, several free and open Base64 encoders and decoders exist. Apache Commons Codec is a widely used and stable library which contains several codecs include Base64.

md.update(plainText.getBytes(UTF-8))将输入更新为消化。调用 digest 执行最终更新并计算输入的摘要。请参阅的javadoc md.digest md.update

md.update(plainText.getBytes("UTF-8")) updates the input to the digest. Calling digest performs a final update and computes the digest of the input. See javadoc of md.digest and md.update

这篇关于MessageDigest的更新方法是什么,BASE64Encoder是什么意思?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆