Created
September 14, 2014 00:45
-
-
Save porthunt/89e5809ff8593a39c3a8 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.security.*; | |
import javax.crypto.*; | |
class CreateSignature { | |
public static void main (String[] args) throws Exception { | |
// verify args. If args.length is not more (or less) than 1, tells how to use the program. | |
if (args.length !=1) { | |
System.err.println("How to use: java CreateSignature your text between \"\""); | |
System.exit(1); | |
} | |
byte[] plainText = args[0].getBytes("UTF8"); | |
/* Step 1: Create the digest of the plainText */ | |
StringBuffer digest = createDigest(plainText); | |
System.out.println( "\tDigest: "+digest.toString() ); | |
/* Step 1: finished. */ | |
/* Step 2: Create the key to encrypt/decrypt the signature. */ | |
KeyPair key = createKey(); | |
/* Step 2: finished. */ | |
/* Step 3: Encrypt the digest with a private key. */ | |
byte[] cipherText = encrypt(String.valueOf(digest).getBytes(), key.getPrivate()); | |
/* Step 3: finished. */ | |
/* Step 4: Decrypt the signature with the public key. */ | |
String decryptedDigest = decrypt(key.getPublic(), cipherText); | |
/* Step 4: finished. */ | |
/* Step 5: Compare the old digest with the decrypted digest. */ | |
System.out.println("\nStep 5: Compare the old digest with the decrypted digest: "); | |
System.out.println( "\n\tOld digest: "+digest.toString() ); | |
System.out.println( "\tDecrypted digest: "+decryptedDigest); | |
if(decryptedDigest.equals(digest.toString())) | |
System.out.println( "\nThe digests are equal.\n"); | |
else | |
System.out.println( "\nThe digests are not equal. File is not ok.\n"); | |
/* Step 5: finished */ | |
} | |
/*************************************************/ | |
/* Method name: createDigest */ | |
/* Parameters: plainText */ | |
/* Function: create the digest of the file. */ | |
/* Return: a StringBuffer with the digest. */ | |
/*************************************************/ | |
public static StringBuffer createDigest(byte[] plainText) throws Exception { | |
MessageDigest messageDigest = MessageDigest.getInstance("MD5"); //especifica-se o tipo de digest (MD5) | |
//System.out.println( "\n" + messageDigest.getProvider().getInfo() ); //exibi-se a lista de informações | |
messageDigest.update( plainText); | |
byte [] digest = messageDigest.digest(); | |
String plainTextStr = new String(plainText, "UTF-8"); | |
System.out.println("\nStep 1: Create the digest of the plain text '"+plainTextStr+"':"); | |
System.out.println( "\n\tDigest length: " + digest.length * 8 + "bits" ); // tamanho do digest | |
StringBuffer buf = new StringBuffer(); | |
for(int i = 0; i < digest.length; i++) { | |
String hex = Integer.toHexString(0x0100 + (digest[i] & 0x00FF)).substring(1); | |
buf.append((hex.length() < 2 ? "0" : "") + hex); | |
} | |
return buf; | |
} | |
/* End of createDigest */ | |
/*************************************************/ | |
/* Method name: createKey */ | |
/* Parameters: - */ | |
/* Function: create an assymmetric pair of keys. */ | |
/* Return: the pair of keys. */ | |
/*************************************************/ | |
public static KeyPair createKey() throws Exception { | |
System.out.println("\nStep 2: Create a key to cipher the digest:"); | |
System.out.println( "\n\tStart generating RSA key..." ); | |
KeyPairGenerator keyGen = KeyPairGenerator.getInstance("RSA"); | |
keyGen.initialize(1024); | |
KeyPair key = keyGen.generateKeyPair(); | |
System.out.println( "\tRSA key created!"); | |
return key; | |
} | |
/* End of createKey */ | |
/*********************************************************************/ | |
/* Method name: encrypt */ | |
/* Parameters: msg, key */ | |
/* Function: encrypt the msg parameter with the key passed. */ | |
/* Return: the text encrypted. */ | |
/*********************************************************************/ | |
public static byte[] encrypt(byte[] msg, Key key) throws Exception { | |
System.out.println("\nStep 3: Encrypt the digest with the key created:"); | |
System.out.println( "\n\tStart encryption..." ); | |
Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding"); | |
cipher.init(Cipher.ENCRYPT_MODE, key); | |
byte[] cipherText = cipher.doFinal(msg); | |
StringBuffer buf = new StringBuffer(); | |
for(int i = 0; i < cipherText.length; i++) { | |
String hex = Integer.toHexString(0x0100 + (cipherText[i] & 0x00FF)).substring(1); | |
buf.append((hex.length() < 2 ? "0" : "") + hex); | |
} | |
// print the signature in hexadecimal. | |
System.out.println("\n\tSignature:\n\t"+buf.toString()+"\n"); | |
return cipherText; | |
} | |
/* End of encrypt */ | |
/*********************************************************************/ | |
/* Method name: decrypt */ | |
/* Parameters: key, cipherText */ | |
/* Function: encrypt the msg parameter with the key passed. */ | |
/* Return: the text encrypted. */ | |
/*********************************************************************/ | |
public static String decrypt(Key key, byte[] cipherText) throws Exception { | |
Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding"); | |
System.out.println("\nStep 4: Decrypt the digest with the key:"); | |
System.out.println( "\n\tStart decryption..." ); | |
cipher.init(Cipher.DECRYPT_MODE, key); | |
byte[] newPlainText = cipher.doFinal(cipherText); | |
System.out.println( "\tFinish decryption. " ); | |
String digest = new String(newPlainText, "UTF8"); | |
return digest; | |
} | |
/* End of decrypt */ | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment