This article mainly talks about encryption and digital signature in cryptography, and how to use it in java. For partners interested in cryptography, we recommend that you read the book BruceSchneier: AppliedCrypotography. The security of the jdk1.5 release has been greatly improved, and direct support for the RSA algorithm is also provided. now we start from an instance to solve the problem (this document describes the digital signature
This article mainly talks about encryption and digital signature in cryptography, and how to use it in java. For partners interested in cryptography, we recommend that you read Bruce Schneier's book: Applied Crypotography. Security has been greatly improved in the jdk1.5 release, and direct support for the RSA algorithm is also provided. now we start with an example to solve the problem (this article is only for a brief introduction):
I. concepts commonly used in cryptography
1) message summary:
This is a technology used in combination with message authentication codes to ensure message integrity. One-way hash function algorithms are mainly used to verify the integrity of messages and store messages in text format using hashed passwords. Currently, MD4, MD5, and SHA-1 are widely used algorithms, jdk1.5 provides support for the above. it is very easy to digest messages in java, java. security. messageDigest provides a simple operation method:
/**
* MessageDigestExample. java
* Copyright 2005-2-16
*/
Import java. security. MessageDigest;
/**
* A single message digest algorithm without a password. it can be used to hide and save plaintext messages (such as passwords ).
*/
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 ");
// Use getInstance ("algorithm") to obtain the message digest. here the SHA-1 160-bit algorithm is used.
MessageDigest messageDigest = MessageDigest. getInstance ("SHA-1 ");
System. out. println ("" + messageDigest. getProvider (). getInfo ());
// Start using the algorithm
MessageDigest. update (plainText );
System. out. println ("Digest :");
// Output the algorithm operation result
System. out. println (new String (messageDigest. digest (), "UTF8 "));
}
}
The message authentication code can also be used for encryption. javax. crypto. Mac provides a solution. if you are interested, refer to the relevant API documentation. This article only briefly introduces what is the digest algorithm.
2) private key encryption:
Message digests can only check the integrity of messages, but one-way encryption is not allowed for plaintext messages. to encrypt plaintext messages, you must use other algorithms to ensure confidentiality, we need to use the private key cryptography to exchange private messages.
This is best understood by using symmetric algorithms. For example, if A encrypts A file with A key, and B reads the file, it needs the same key as A. Both parties share A private key (in the web environment, private keys are easily listened upon transmission ):
If you use the private key for encryption, you first need a key available for javax. crypto. keyGenerator generates a key (java. security. key), and then pass it to an encryption tool (javax. crypto. cipher), the tool uses the corresponding algorithms for encryption, the main symmetric algorithms are: DES (actual keys only use 56 bits), AES (three key lengths are supported: 128, 192, 256 bits), usually the first 128 bits, and the other are DESede. the jdk1.5 type also provides support for symmetric algorithms. the following example uses the AES algorithm for encryption:
/**
* PrivateExmaple. java
* Copyright 2005-2-16
*/
Import javax. crypto. Cipher;
Import javax. crypto. KeyGenerator;
Import java. security. Key;
/**
* Private