Quick start for Java encryption and digital signature programming

Source: Internet
Author: User
Tags hash

This article mainly discusses cryptography and digital signature, and how it is used in Java. The partner who is interested in cryptography is recommended to see Bruce Schneier's book: Applied Crypotography. There has been a great improvement in security in the release of jdk1.5, as well as direct support for RSA algorithms, and now we're going to solve the problem from the example (this article is just a brief introduction):

First, the commonly used concepts in cryptography

1) Message Summary:

This is a technique that is used in conjunction with the message authentication code to ensure message integrity. The main use of one-way hash function algorithm, can be used to verify the integrity of the message, and through the hash password directly in the form of text preservation, etc., the current widely used algorithms are MD4, MD5, sha-1,jdk1.5 to provide support for the above, in the Java Message digest is very simple, Java.security.MessageDigest provides an easy way to operate:

/**
*MessageDigestExample.java
*Copyright 2005-2-16
*/
import java.security.MessageDigest;
/**
*单一的消息摘要算法,不使用密码.可以用来对明文消息(如:密码)隐藏保存
*/
public class MessageDigestExample{
 public static void main(String[] args) throws Exception{
  if(args.length!=1){
   System.err.println("Usage:java MessageDigestExample text");
   System.exit(1);
  }
  byte[] plainText=args[0].getBytes("UTF8");
  //使用getInstance("算法")来获得消息摘要,这里使用SHA-1的160位算法
  MessageDigest messageDigest=MessageDigest.getInstance("SHA-1");
  System.out.println("\n"+messageDigest.getProvider().getInfo());
  //开始使用算法
  messageDigest.update(plainText);
  System.out.println("\nDigest:");
  //输出算法运算结果
  System.out.println(new String(messageDigest.digest(),"UTF8"));
 }
}

The message authentication code can also be used to encrypt the implementation, JAVAX.CRYPTO.MAC provides a solution, interested people can refer to the relevant API documentation, this article simply describes what is the digest algorithm.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.