Java implementations of several cryptographic algorithms include MD5, RSA, SHA256

Source: Internet
Author: User
Tags decrypt md5 encryption

SHA Encryption:

Package com;

Import Java.security.MessageDigest;
Import java.security.NoSuchAlgorithmException;
/**
* Getsha (String str) method available externally
* @author Randyjia
*
*/
public class SHA {
public static string Encrypt (String strsrc, String encname) {
MessageDigest MD = NULL;
StringBuilder sb = new StringBuilder ();

byte[] bt = Strsrc.getbytes ();
try {
md = Messagedigest.getinstance (encname);
Byte[] result = Md.digest (BT);
for (byte B:result) {
Sb.append (String.Format ("%02x", b));
}
} catch (NoSuchAlgorithmException e) {
return null;
}
return sb.tostring ();
}


public static void Main (String args[]) {
String s = SHA. Encrypt ("12345", "SHA-256");
System.out.println (s);

}
}

RSA Encryption:

Package com;

Import java.io.*;
Import java.security.*;

Import javax.crypto.*;
Import Java.security.spec.X509EncodedKeySpec;
Import Java.security.spec.PKCS8EncodedKeySpec;
Import sun.misc.*;

public class Rsa_test {
Private Keypairgenerator KPG = null;
Private KeyPair KP = null;
Private PublicKey public_key = null;
Private Privatekey private_key = null;

public rsa_test (int in, String address) throws NoSuchAlgorithmException,
FileNotFoundException, IOException
{
KPG = Keypairgenerator.getinstance ("RSA");
Kpg.initialize (in);
KP = Kpg.genkeypair ();
Public_key = Kp.getpublic ();
Private_key = Kp.getprivate ();

Sun.misc.BASE64Encoder b64 = new Sun.misc.BASE64Encoder ();
String pkstr = B64.encode (public_key.getencoded ());
String prstr = B64.encode (private_key.getencoded ());
System.out.print ("Pkstr Length:" +pkstr.length () + "" +pkstr);

FileWriter fw=new FileWriter (address + "/private_key.dat");
Fw.write (PRSTR);
Fw.close ();

FileWriter fw2 = new FileWriter (address + "/public_key.dat");
Fw2.write (PKSTR);
Fw2.close ();
}



Private static string encrypt (string source) throws exception{

FileReader fr = new FileReader ("e://p Ublic_key.dat ");
BufferedReader br=new BufferedReader (FR);
String Getpbkey = "";
while (true) {
String aLine = Br.readline (),
if (aline==null) break,
Getpbkey + = ALine;
}
System.out.println ("mybuilderstr:length:" + getpbkey.length () + "" +getpbkey);
Base64decoder b64d = new Base64decoder ();
Byte [] keybyte = B64d.decodebuffer (Getpbkey);
X509encodedkeyspec X509ek = new X509encodedkeyspec (keybyte);
Keyfactory keyfactory = keyfactory.getinstance ("RSA");
PublicKey PublicKey = Keyfactory.generatepublic (X509ek);

Cipher Cipher = cipher.getinstance ("RSA");
Cipher.init (Cipher.encrypt_mode,publickey);
byte[] SBT = Source.getbytes ();
Byte [] epbyte = Cipher.dofinal (SBT);
Base64encoder encoder = new Base64encoder ();
String epstr = Encoder.encode (epbyte);
return epstr;

}


public static string decrypt (string cryptograph) throws exception{

FileReader fr = new FileR Eader ("E://private_key.dat");
BufferedReader br=new BufferedReader (FR);
String Getpvkey = "";
while (true) {
String aLine = Br.readline (),
if (aline==null) break,
Getpvkey + = ALine;
}
Base64decoder b64d = new Base64decoder ();
Byte [] keybyte = B64d.decodebuffer (Getpvkey);
Pkcs8encodedkeyspec S8ek = new Pkcs8encodedkeyspec (keybyte);
Keyfactory keyfactory = keyfactory.getinstance ("RSA");
Privatekey Privatekey = keyfactory.generateprivate (S8ek);



Cipher Cipher = cipher.getinstance ("RSA");
Cipher.init (Cipher.decrypt_mode, privatekey);
Base64decoder decoder = new Base64decoder ();
Byte[] B1 = Decoder.decodebuffer (cryptograph);
Byte[] B = cipher.dofinal (B1);
return new String (b);
}

/*
public static void Main (string[] args) {
try {
New Rsa_test (1024x768, "e:/");
System.out.println ("");
String geteptstr = Encrypt ("Wuchao");
System.out.println ("Geteptstr:" +geteptstr);
String drpstr = Decrypt (GETEPTSTR);
System.out.println ("Drpstr:" +drpstr);
}
catch (IOException ex) {
Ex.printstacktrace ();
}
catch (NoSuchAlgorithmException ex) {
Ex.printstacktrace ();
}catch (Exception ex) {
Ex.printstacktrace ();
}

}
*/

}

MD5 Encryption:

Package com;

Import Java.security.MessageDigest;

public class Md5 {

Public final static String MD5 (string s) {
Char hexdigits[]={' 0 ', ' 1 ', ' 2 ', ' 3 ', ' 4 ', ' 5 ', ' 6 ', ' 7 ', ' 8 ', ' 9 ', ' A ', ' B ', ' C ', ' D ', ' E ', ' F '};
try {
byte[] Btinput = S.getbytes ();
MessageDigest mdinst = messagedigest.getinstance ("MD5");
Mdinst.update (Btinput);
byte[] MD = Mdinst.digest ();
int j = Md.length;
Char str[] = new CHAR[J * 2];
int k = 0;
for (int i = 0; i < J; i++) {
byte byte0 = md[i];
str[k++] = hexdigits[byte0 >>> 4 & 0xf];
str[k++] = hexdigits[byte0 & 0xf];
}
return new String (str);
} catch (Exception e) {
E.printstacktrace ();
return null;
}
}
public static void Main (string[] args) {
TODO auto-generated Method Stub
System.out.println (MD5.MD5 ("20121221ASFASFASFA"));
System.out.println (MD5.MD5 ("????"));
}

}

Java implementations of several cryptographic algorithms include MD5, RSA, SHA256

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.