Encryption and DSA Digital Sign in Java

zhaozj2021-02-16  40

Encryption and DSA Digital Sign in Java

Bromon

First, one-way encryption (MD5 and SHA-1)

One-way encryption is usually used for message summary, and the specific algorithm can read the source code for java.security. *. You can use a simple static method after the encapsulation, Look:

package org.bromon; public class MD5Encoder {public static void main (String args []) {String info = args [0]; try {// the MD5 algorithm selection java.security.MessageDigest alg = java.security.MessageDigest.getInstance ("MD5");

// Select 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

Result = result integer.tostring (m, 16) .touppercase () ""; // Convert to uppercase characters} system.out.println (result);} catch (exception e) {system.out.println (e) }}}

Compile: javac -d. MD5Encoder.java running: java org.bromon.md5encoder Somedata

DES as a representative of the single-key encryption, it is currently in the list of export restrictions in the Pentagon! ?

Second, asymmetric encryption DSA digital signature

Implemented by using a key pair. According to the principle of asymmetric encryption, the public key is distributed and encrypted with it, and the private key is confidentially used to decrypt. The digital signature of the DSA is encrypted with private key, and the public key is decrypted to ensure undisporant and integrity. Sign in 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 ();

// the generated file key to the sequence of 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);}}} two after running. DAT file.

You can then use the private key to the data signature:

Package org.bromon; import java.io. *; import java.security. *;

public class DSASigner {public static void main (String args []) {String s = "required encrypted contents"; try {// import the private key ObjectInputStream in = new ObjectInputStream (new FileInputStream ( "prikey.dat")); PrivateKey PRIKEY = () In.readObject (); in.close (); // Signature Signature Signature = Signature.getInstance ("DSA"); Signature.initsign (Prikey); Signature.Update (S.GetBytes () ); Byte [] signed = signature.sign (); // Write the signed data to file ObjectOutputStream out = new ObjectOutputStream (New FileoutputStream ("info.dat"); out.writeObject (s); out.writeObject (s); out.writeObject (SIGNED); out.close ();} catch (exception e) {system.out.println (e);}}

}

After running, a encrypted file is generated after the current path, and this file is distributed to the recipient together with the public key.

Below is the use of public key verification signatures Normal:

Package org.bromon; import java.security. *; import java.io. *;

public class DSAChecker {public static void main (String args []) {try {// import the public ObjectInputStream in = new ObjectInputStream (new FileInputStream ( "pubkey.dat")); PublicKey pubkey = (PublicKey) in.readObject () In.close (); // Import file IN = New ObjectInputStream (New FileInputStream ("info.dat"); string s = () In.readObject (); byte [] sign = (Byte []) in.readObject (); in .close (); // verifying the key to 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 Reading Permissions");}} Catch (Exception E) {System.out.Println e);}}} After the key matches, encrypted content will be displayed. If the public key format is destroyed, an exception will be thrown.

Java has a very large encryption framework, a variety of encryption and signatures, and detailed in Oreilly - Java Cryptography. However, some packages cannot get in China.

转载请注明原文地址:https://www.9cbs.com/read-21729.html

New Post(0)