Java encryption and DSA Digital Signature

Source: Internet
Author: User
Java encryption and DSA Digital Signature

Bromon

1. One-way encryption (MD5 and SHA-1)

One-way encryption is usually used for message summarization. The specific algorithm can be obtained by reading the source code of Java. Security. After encapsulation, you can use a simple static method to implement it. Look:

Package org. bromon;
Public class md5encoder
{
Public static void main (string ARGs [])
{
String info = ARGs [0];
Try
{
// Select the MD5 Encryption Algorithm
Java. Security. messagedigest ALG =
Java. Security. messagedigest. getinstance ("MD5 ");

// Select the SHA-1 Encryption Algorithm
// Java. Security. messagedigest ALG =
Java. Security. messagedigest. getinstance ("SHA-1 ");

ALG. Update (info. getbytes ());
Byte [] digesta = alg. Digest ();
String result = "";
For (INT I = 0; I <digesta. length; I ++)
{
Int M = digesta [I];
If (M <0)
{
M + = 256; // modulo if it is a negative number
}

Result = Result + integer. tostring (M, 16). touppercase () + ""; // convert to uppercase
}
System. Out. println (result );
} Catch (exception E)
{
System. Out. println (E );
}
}
}

Compile: javac-D. md5encoder. Java
Run: Java org. bromon. md5encoder somedata

As a representative of single-key encryption, des still seems to be in the list of Pentagon export restrictions !?

Ii. asymmetric encryption DSA Digital Signature

The key pair is used. Based on the principle of asymmetric encryption, the Public Key is distributed and encrypted. The private key is kept confidential for decryption. The digital signature of DSA uses private key encryption and Public Key decryption to ensure non-repudiation and integrity. Take the digital signature of DSA as an example:

First, you need to generate a pair of keys:

Package org. bromon;
Import java. Io .*;
Import java. Security .*;

Public class dsageneratekeypair
{
Public static void main (string ARGs [])
{
Try
{
Java. Security. keypairgenerator keygen =
Java. Security. keypairgenerator. getinstance ("DSA ");

Securerandom sr = new securerandom ();
Sr. setseed ("123". getbytes (); // key Seed
Keygen. initialize (512, Sr );

Keypair keys = keygen. generatekeypair ();
Publickey pubkey = keys. getpublic ();
Privatekey prikey = keys. getprivate ();

// Serialize the generated key pair to a file
Objectoutputstream out = new objectoutputstream (New fileoutputstream ("prikey. dat "));
Out. writeobject (prikey );
Out. Close ();

Out = new objectoutputstream (New fileoutputstream ("pubkey. dat "));
Out. writeobject (pubkey );
Out. Close ();
} Catch (exception E)
{
System. Out. println (E );
}

}
}

After running, two. DAT files are generated in the current directory.

Then, you can use the private key to sign the data:

Package org. bromon;
Import java. Io .*;
Import java. Security .*;

Public class dsasigner
{
Public static void main (string ARGs [])
{
String S = "content to be encrypted ";
Try
{
// Import the Private Key
Objectinputstream in = new objectinputstream (New fileinputstream ("prikey. dat "));
Privatekey prikey = (privatekey) in. readobject ();
In. Close ();

// Sign the data
Signature = signature. getinstance ("DSA ");
Signature. initsign (prikey );
Signature. Update (S. getbytes ());
Byte [] signed = signature. Sign ();

// Write signed data to the file
Objectoutputstream out = new objectoutputstream (New fileoutputstream ("info. dat "));
Out. writeobject (s );
Out. writeobject (Signed );
Out. Close ();
} Catch (exception E)
{
System. Out. println (E );
}

}

}

After running the program, an encrypted file is generated in the current path and the file and public key are distributed to the receiver.

The following describes how to use the public key to verify whether the signature is normal:

Package org. bromon;
Import java. Security .*;
Import java. Io .*;

Public class dsachecker
{
Public static void main (string ARGs [])
{
Try
{
// Import the Public Key
Objectinputstream in = new objectinputstream (New fileinputstream ("pubkey. dat "));
Publickey pubkey = (publickey) in. readobject ();
In. Close ();

// Import the file to be read
In = new objectinputstream (New fileinputstream ("info. dat "));
String S = (string) in. readobject ();
Byte [] signed = (byte []) in. readobject ();
In. Close ();

// Verify the key pair
Signature signcheck = signature. getinstance ("DSA ");
Signcheck. initverify (pubkey );
Signcheck. Update (S. getbytes ());
If (signcheck. Verify (Signed ))
{
System. Out. println (s );
} Else {
System. Out. println ("No read permission ");
}
} Catch (exception E)
{
System. Out. println (E );
}

}

}

After running the command, if the key matches correctly, the encrypted content is displayed. If the public key format is corrupted, an exception is thrown.

Java has a very large encryption framework. There are many types of encryption and signature, as detailed in oreilly-Java Cryptography. However, some packages cannot be obtained in China.

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.