itsec stuff
This commit is contained in:
parent
a4931a747a
commit
602276f342
121
ss2011/it-sicherheit/Uebungen/RSA_Exercise_4373.java
Normal file
121
ss2011/it-sicherheit/Uebungen/RSA_Exercise_4373.java
Normal file
@ -0,0 +1,121 @@
|
||||
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);
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
4
ss2011/it-sicherheit/Uebungen/src.RSA_Exercise_4373_log
Normal file
4
ss2011/it-sicherheit/Uebungen/src.RSA_Exercise_4373_log
Normal file
@ -0,0 +1,4 @@
|
||||
Signature of first exercise is:d2d9785e7c16021399a69f92f1066ff6f805ade1df0df15b31a4532fad491330110d24bfe55e95a646d3b83fcce838d747057ceb71d798fdc7d967ae972983c0d759f1742a5147a0afa6e66d4eeaa11a325df695af0139c32cb20fb166a379560d4e8e8a71b3ab113462770ccc94af12dd421e7e1fbf8aaffb6ab27e836172d0
|
||||
The signature of first exercise is :true
|
||||
Ciphertext:2db35f58e239adef275ceeaeb17917b1a64e1ae4787a446cf3c681125656f454e86b2d1cc982a804b723b56cc6d6b507b9020099b447ec70b6cd424051e719869351283b61ac35b23dc4a0ee4900f6280ab0368fe263ce32dde1ca269d6079605f88f44d0b55631fc98333e044e209a7b3c8e6ddec6fd239f2d59ad9ead01b1e
|
||||
Decrypted Text:4373first exercise
|
||||
BIN
ss2011/it-sicherheit/Uebungen/src.RSA_Exercise_4373_priv
Normal file
BIN
ss2011/it-sicherheit/Uebungen/src.RSA_Exercise_4373_priv
Normal file
Binary file not shown.
BIN
ss2011/it-sicherheit/Uebungen/src.RSA_Exercise_4373_pub
Normal file
BIN
ss2011/it-sicherheit/Uebungen/src.RSA_Exercise_4373_pub
Normal file
Binary file not shown.
Loading…
x
Reference in New Issue
Block a user