Import Java.security.keypair;import Java.security.privatekey;import Java.security.publickey;import Java.security.securerandom;public class Generatekeypair {private string prikey;private string pubkey;public void Run () { try {java.security.KeyPairGenerator keygen = java.security.KeyPairGenerator.getInstance ("RSA"); SecureRandom Secrand = new SecureRandom (); Secrand.setseed ("Java". GetBytes ()); Initialize the random generator keygen.initialize (1024x768, Secrand); KeyPair keys = Keygen.genkeypair (); PublicKey PubKey = Keys.getpublic (); Privatekey Prikey = keys.getprivate ();p Ubkey = Bytestohexstr (pubkey.getencoded ());p Rikey = Bytestohexstr ( Prikey.getencoded ()); System.out.println ("pubkey=" + PubKey); System.out.println ("prikey=" + Prikey); System.out.println ("Write Object Pubkeys OK"); SYSTEM.OUT.PRINTLN ("Generate key pair success");} catch (Java.lang.Exception e) {e.printstacktrace (); SYSTEM.OUT.PRINTLN ("Generate key pair failed");};} /** * Transform The specified byte into a Hex String form. */public static final String bytestohexstr (byte[] BCD) {StringBuffer s = new StringBuffer (Bcd.length * 2); for (int i = 0; i < bcd.length; i++) {S.append (bcdlookup[(Bcd[i] >>> 4) & 0x 0f]); S.append (Bcdlookup[bcd[i] & 0x0f]);} return s.tostring ();} /** * Transform The specified Hex String into a byte array. */public static final byte[] Hexstrtobytes (String s) {byte[] bytes;bytes = new Byte[s.length ()/2];for (int i = 0; i < Bytes.length; i++) {Bytes[i] = (byte) integer.parseint (s.substring (2 * I, 2 * i + 2), 16);} return bytes;} private static final char[] Bcdlookup = {' 0 ', ' 1 ', ' 2 ', ' 3 ', ' 4 ', ' 5 ', ' 6 ', ' 7 ', ' 8 ', ' 9 ', ' A ', ' B ', ' C ', ' d ', ' e ', ' f '}; /** * @param args */public static void main (string[] args) {//TODO auto-generated method Stubgeneratekeypair n = new Gene Ratekeypair (); N.run ();}}
Import Java.security.keyfactory;import Java.security.privatekey;import Java.security.spec.PKCS8EncodedKeySpec; public class SignatureData {public void run () {try {String prikeyvalue = "123";//This is the private key encoding of the Generatekeypair output Pkcs8encodedkey Spec priPKCS8 = new Pkcs8encodedkeyspec (hexstrtobytes (Prikeyvalue)); Keyfactory Keyf = keyfactory.getinstance ("RSA"); Privatekey Myprikey = keyf.generateprivate (priPKCS8); String MyInfo = string.valueof (Math.random ()); Information to be signed//to generate a digital signature with the private key java.security.Signature signet = Java.security.Signature.getInstance ("Md5withrsa"); Signet.initsign (Myprikey); Signet.update (Myinfo.getbytes ("iso-8859-1")); byte[] signed = Signet.sign (); Digital signature of the information System.out.println ("Signed (signature content) =" + Bytestohexstr (signed)); System.out.println ("info (original value) =" + MyInfo); SYSTEM.OUT.PRINTLN ("sign and generate file Success");} catch (Java.lang.Exception e) {e.printstacktrace (); System.out.println ("Failed to sign and generate file");};} /** * Transform The specified byte into a Hex String form. */public static final String bytestohexstr (byte[] BCD{StringBuffer s = new StringBuffer (Bcd.length * 2); for (int i = 0; i < bcd.length; i++) {S.append (bcdlookup[(bcd[i) & Gt;>> 4) & 0x0f]); S.append (Bcdlookup[bcd[i] & 0x0f]);} return s.tostring ();} /** * Transform The specified Hex String into a byte array. */public static final byte[] Hexstrtobytes (String s) {byte[] bytes;bytes = new Byte[s.length ()/2];for (int i = 0; i < Bytes.length; i++) {Bytes[i] = (byte) integer.parseint (s.substring (2 * I, 2 * i + 2), 16);} return bytes;} private static final char[] Bcdlookup = {' 0 ', ' 1 ', ' 2 ', ' 3 ', ' 4 ', ' 5 ', ' 6 ', ' 7 ', ' 8 ', ' 9 ', ' A ', ' B ', ' C ', ' d ', ' e ', ' f '}; /** * @param args */public static void main (string[] args) {//TODO auto-generated method Stubsignaturedata s = new Signat Uredata (); S.run ();}}
Import Java.security.keyfactory;import Java.security.publickey;import Java.security.spec.X509EncodedKeySpec; public class VerifySignature {public static void main (string[] args) {new VerifySignature (). RUN1 (); public void Run1 () {try {String pubkeyvalue = "30819f300d0609";//This is the of the Generatekeypair output X509encodedkeyspec Bobpubkeyspec = new X509encodedkeyspec (hexstrtobytes (Pubkeyvalue)); Keyfactory keyfactory = keyfactory.getinstance ("RSA"); PublicKey PubKey = Keyfactory.generatepublic (bobpubkeyspec);//string info = ""; byte[] signed = Hexstrtobytes ("32323"); /This is the digital signature of the SignatureData output java.security.Signature Signetcheck = java.security.Signature.getInstance ("Md5withrsa"); Signetcheck.initverify (PubKey);//signetcheck.update (Info.getbytes ()); if (Signetcheck.verify (signed)) {// System.out.println ("info=" + info); System.out.println ("signature OK");} ELSESYSTEM.OUT.PRINTLN ("Non-signed normal");} catch (Java.lang.Exception e) {e.printstacktrace ();}} /** * Transform The specified byte into a Hex String form. */public Static FInal String bytestohexstr (byte[] BCD) {StringBuffer s = new StringBuffer (Bcd.length * 2); for (int i = 0; i < bcd.length ; i++) {s.append (bcdlookup[(Bcd[i] >>> 4) & 0x0f]); S.append (Bcdlookup[bcd[i] & 0x0f]);} return s.tostring ();} /** * Transform The specified Hex String into a byte array. */public static final byte[] Hexstrtobytes (String s) {byte[] bytes;bytes = new Byte[s.length ()/2];for (int i = 0; i < Bytes.length; i++) {Bytes[i] = (byte) integer.parseint (s.substring (2 * I, 2 * i + 2), 16);} return bytes;} private static final char[] Bcdlookup = {' 0 ', ' 1 ', ' 2 ', ' 3 ', ' 4 ', ' 5 ', ' 6 ', ' 7 ', ' 8 ', ' 9 ', ' A ', ' B ', ' C ', ' d ', ' e ', ' f '}; }
JAVA RSA Digital Signature