122 lines
3.2 KiB
Java
122 lines
3.2 KiB
Java
package src;
|
|
import java.security.InvalidKeyException;
|
|
import java.security.Key;
|
|
import java.security.KeyPair;
|
|
import java.security.KeyPairGenerator;
|
|
import java.security.NoSuchAlgorithmException;
|
|
import java.security.NoSuchProviderException;
|
|
import java.security.PrivateKey;
|
|
import java.security.PublicKey;
|
|
import java.security.Signature;
|
|
import java.security.SignatureException;
|
|
|
|
import javax.crypto.Cipher;
|
|
|
|
public class RSA_Exercise_4373 extends RSA_Basics implements RSA_ExerciseInterface{
|
|
|
|
private static String text="first exercise";
|
|
|
|
private Key privatekey;
|
|
private Key publickey;
|
|
|
|
|
|
public Key getPrivate() {
|
|
return privatekey;
|
|
}
|
|
|
|
public Key getPublic() {
|
|
return publickey;
|
|
}
|
|
|
|
public void generateRSAKeypair() {
|
|
// Get a KeyPair Generator
|
|
KeyPairGenerator gen;
|
|
try { gen = KeyPairGenerator.getInstance("RSA");}
|
|
catch (NoSuchAlgorithmException e) {
|
|
//This should not be the case ;-)
|
|
privatekey = null;
|
|
publickey = null;
|
|
return;}
|
|
|
|
//set to 1024bit
|
|
gen.initialize(1024);
|
|
|
|
//Generate the Pair
|
|
KeyPair keypair = gen.genKeyPair();
|
|
|
|
//Assign class vars
|
|
privatekey = keypair.getPrivate();
|
|
publickey = keypair.getPublic();
|
|
}
|
|
|
|
public String sign(String value, Key key) throws Exception
|
|
{
|
|
// Instance
|
|
Signature signature = Signature.getInstance("SHA1withRSA");
|
|
|
|
signature.initSign((PrivateKey) key);
|
|
signature.update(value.getBytes());
|
|
return asHex(signature.sign());
|
|
}
|
|
|
|
public boolean verify(String value,String sigtoverify, Key key) throws NoSuchAlgorithmException, NoSuchProviderException, InvalidKeyException, SignatureException
|
|
{
|
|
// Instance
|
|
Signature signature = Signature.getInstance("SHA1withRSA");
|
|
|
|
signature.initVerify((PublicKey) key);
|
|
signature.update(value.getBytes());
|
|
return signature.verify(asByteArray(sigtoverify));
|
|
}
|
|
|
|
public String encrypt(String value, Key key) throws Exception
|
|
{
|
|
// Instance
|
|
Cipher cip = Cipher.getInstance("RSA");
|
|
|
|
// Set mode + finalize
|
|
cip.init(Cipher.ENCRYPT_MODE, key);
|
|
return asHex(cip.doFinal(value.getBytes()));
|
|
}
|
|
|
|
public String decrypt(String value, Key key) throws Exception
|
|
{
|
|
// Instance
|
|
Cipher cip = Cipher.getInstance("RSA");
|
|
|
|
// Set mode + finalize
|
|
cip.init(Cipher.DECRYPT_MODE, key);
|
|
return new String(cip.doFinal(asByteArray(value)));
|
|
}
|
|
|
|
|
|
public static void main(String[] args) throws Exception
|
|
{
|
|
String output="";
|
|
|
|
//Generate Instance
|
|
RSA_Exercise_4373 rsa=new RSA_Exercise_4373();
|
|
|
|
//Generate new Keypair
|
|
rsa.generateRSAKeypair();
|
|
|
|
String studid="4373"; //Please change me to your own four last digits of the student id
|
|
|
|
//Signature Checks
|
|
output="Signature of "+text+" is:"+rsa.sign(studid+text,rsa.getPrivate())+"\n";
|
|
output+="The signature of "+text+" is :"+rsa.verify(studid+text, rsa.sign(studid+text,rsa.getPrivate()), rsa.getPublic())+"\n";
|
|
|
|
output+="Ciphertext:" +rsa.encrypt(studid+text,rsa.getPublic())+"\n";
|
|
output+="Decrypted Text:"+rsa.decrypt(rsa.encrypt(studid+text,rsa.getPublic()),rsa.getPrivate())+"\n";
|
|
|
|
System.out.println(output);
|
|
|
|
// Write current Public and Private key for solution hand-in
|
|
writeData(rsa.getClass().getName(), rsa.getPrivate(), rsa.getPublic(),output);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|