JAVA PROGRAMMING JAVA LANGUAGE ONLY I have a homework that
JAVA PROGRAMMING - JAVA LANGUAGE ONLY
I have a homework that involves implementing one such asymmetrical encryption algorithm known as RSA. The assignment needs to include the following:
Create a class with a main method for our RSA implementation. Our program will have three parts
1. Generating the public and private keys
2. Encrypting a number using the public key
3. Decrypting the number using the private key
4. Main method that asks the user for input ) and displays output
I was given wikipedia pages to read to understand RSA, but I don\'t know how to begin this program. Any help would be greatly appreciated.
Solution
public key encrypts plain text to cipher text.
1) Generating the public and private keys.
package com.app.test;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.security.GeneralSecurityException;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.PrivateKey;
import java.security.PublicKey;
public class RSAKeyPair {
private int keyLength;
private PrivateKey privateKey;
private PublicKey publicKey;
public RSAKeyPair(int keyLength) throws GeneralSecurityException {
this.keyLength = keyLength;
KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance(\"RSA\");
keyPairGenerator.initialize(this.keyLength);
KeyPair keyPair = keyPairGenerator.generateKeyPair();
privateKey = keyPair.getPrivate();
publicKey = keyPair.getPublic();
}
public PrivateKey getPrivateKey()
{
return privateKey;
}
public PublicKey getPublicKey() {
return publicKey;
}
public final void toFileSystem(String privateKeyPathName, String publicKeyPathName)
throws IOException {
FileOutputStream privateKeyOutputStream = null;
FileOutputStream publicKeyOutputStream = null;
try
{
File privateKeyFile = new File(privateKeyPathName);
File publicKeyFile = new File(publicKeyPathName);
privateKeyOutputStream = new FileOutputStream(privateKeyFile);
privateKeyOutputStream.write(privateKey.getEncoded());
publicKeyOutputStream = new FileOutputStream(publicKeyFile);
publicKeyOutputStream.write(publicKey.getEncoded());
}
catch(IOException ioException)
{
throw ioException;
}
finally {
try {
if (privateKeyOutputStream != null) {
privateKeyOutputStream.close();
}
if (publicKeyOutputStream != null) {
publicKeyOutputStream.close();
}
} catch(IOException ioException) {
throw ioException;
}
}
}
}
2) Here is the testing java class creating two keys..
package com.app.test;
import java.io.FileInputStream;
import java.security.KeyFactory;
import java.security.spec.PKCS8EncodedKeySpec;
import java.security.spec.X509EncodedKeySpec;
import org.apache.commons.io.IOUtils;
import org.junit.Assert;
import org.junit.Test;
public class RSAKeyPairTest {
private final String privateKeyPathName = \"C://temp//private.key\"; // This path was my machine actual path
private final String publicKeyPathName = \"C://temp//public.key\";
@Test
public void testToFileSystem() throws Exception {
try {
RSAKeyPair rsaKeyPair = new RSAKeyPair(2048); // The parameter pass the length of the key.
rsaKeyPair.toFileSystem(privateKeyPathName, publicKeyPathName);
KeyFactory rsaKeyFactory = KeyFactory.getInstance(\"RSA\");
Assert.assertNotNull(rsaKeyPair.getPrivateKey());
Assert.assertNotNull(rsaKeyPair.getPublicKey());
Assert.assertEquals(rsaKeyPair.getPrivateKey(), rsaKeyFactory.generatePrivate(new PKCS8EncodedKeySpec(IOUtils.toByteArray(new FileInputStream(privateKeyPathName)))));
Assert.assertEquals(rsaKeyPair.getPublicKey(), rsaKeyFactory.generatePublic(new X509EncodedKeySpec(IOUtils.toByteArray(new FileInputStream(publicKeyPathName)))));
}
catch(Exception exception)
{
Assert.fail(\"The testToFileSystem() test failed because: \" + exception.getMessage());
}
}
}
3) Encrypting the number using the public key.
Data encryption and decryption can take place now that a public and private key file that can be generated and saved to the file system. RSACipher class will be built.
package com.app.test;
import org.apache.commons.codec.binary.Base64;
import org.apache.commons.io.IOUtils;
import java.io.FileInputStream;
import java.io.IOException;
import java.security.GeneralSecurityException;
import java.security.KeyFactory;
import java.security.spec.PKCS8EncodedKeySpec;
import java.security.spec.X509EncodedKeySpec;
import javax.crypto.Cipher;
public class RSACipher {
public String encrypt(String rawText, String publicKeyPath, String transformation, String encoding)
throws IOException, GeneralSecurityException
{
X509EncodedKeySpec x509EncodedKeySpec = new X509EncodedKeySpec(IOUtils.toByteArray(new FileInputStream(publicKeyPath)));
Cipher cipher = Cipher.getInstance(transformation);
cipher.init(Cipher.ENCRYPT_MODE, KeyFactory.getInstance(\"RSA\").generatePublic(x509EncodedKeySpec));
return Base64.encodeBase64String(cipher.doFinal(rawText.getBytes(encoding)));
}
public String decrypt(String cipherText, String privateKeyPath, String transformation, String encoding)
throws IOException, GeneralSecurityException {
PKCS8EncodedKeySpec pkcs8EncodedKeySpec = new PKCS8EncodedKeySpec(IOUtils.toByteArray(new FileInputStream(privateKeyPath)));
Cipher cipher = Cipher.getInstance(transformation);
cipher.init(Cipher.DECRYPT_MODE, KeyFactory.getInstance(\"RSA\").generatePrivate(pkcs8EncodedKeySpec));
return new String(cipher.doFinal(Base64.decodeBase64(cipherText)), encoding);
}
}
4) Here is the testing Encrypt and Decrypting data class for the above class
package com.app.test;
import com.reindel.keys.RSAKeyPair;
import org.junit.Assert;
import org.junit.Test;
public class RSACipherTest {
private final String privateKeyPathName = \"C://temp//private.key\";
private final String publicKeyPathName = \"C://temp//public.key\";
private final String transformation = \"RSA/ECB/PKCS1Padding\";
private final String encoding = \"UTF-8\";
@Test
public void testEncryptDecryptWithKeyPairFiles()
throws Exception {
try {
RSAKeyPair rsaKeyPair = new RSAKeyPair(2048); // Passing the length of key as a parameter.
rsaKeyPair.toFileSystem(privateKeyPathName, publicKeyPathName);
RSACipher rsaCipher = new RSACipher();
String encrypted = rsaCipher.encrypt(\"John has a long mustache.\", publicKeyPathName, transformation, encoding);
String decrypted = rsaCipher.decrypt(encrypted, privateKeyPathName, transformation, encoding);
Assert.assertEquals(decrypted, \"John has a long mustache.\");
}
catch(Exception exception)
{
Assert.fail(\"The testEncryptDecryptWithKeyPairFiles() test failed because: \" + exception.getMessage());
}
}
}
| package com.app.test; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.security.GeneralSecurityException; import java.security.KeyPair; import java.security.KeyPairGenerator; import java.security.PrivateKey; import java.security.PublicKey; public class RSAKeyPair { private int keyLength; private PrivateKey privateKey; private PublicKey publicKey; public RSAKeyPair(int keyLength) throws GeneralSecurityException { this.keyLength = keyLength; KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance(\"RSA\"); keyPairGenerator.initialize(this.keyLength); KeyPair keyPair = keyPairGenerator.generateKeyPair(); privateKey = keyPair.getPrivate(); publicKey = keyPair.getPublic(); } public PrivateKey getPrivateKey() { return privateKey; } public PublicKey getPublicKey() { return publicKey; } public final void toFileSystem(String privateKeyPathName, String publicKeyPathName) throws IOException { FileOutputStream privateKeyOutputStream = null; FileOutputStream publicKeyOutputStream = null; try { File privateKeyFile = new File(privateKeyPathName); File publicKeyFile = new File(publicKeyPathName); privateKeyOutputStream = new FileOutputStream(privateKeyFile); privateKeyOutputStream.write(privateKey.getEncoded()); publicKeyOutputStream = new FileOutputStream(publicKeyFile); publicKeyOutputStream.write(publicKey.getEncoded()); } catch(IOException ioException) { throw ioException; } finally { try { if (privateKeyOutputStream != null) { privateKeyOutputStream.close(); } if (publicKeyOutputStream != null) { publicKeyOutputStream.close(); } } catch(IOException ioException) { throw ioException; } } } } 2) Here is the testing java class creating two keys..
|









