Skip to content

Commit

Permalink
Add files via upload
Browse files Browse the repository at this point in the history
  • Loading branch information
roytuts authored May 21, 2019
1 parent 002d2bc commit 6df1605
Show file tree
Hide file tree
Showing 5 changed files with 102 additions and 0 deletions.
1 change: 1 addition & 0 deletions rsa-encryption-decryption/readme.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
You can read tutorial https://www.jeejava.com/encryption-and-decryption-using-rsa-in-java/
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.jeejava.rsa;

public final class RSAConstants {

private RSAConstants() {
}

public static final String ALGORITHM = "RSA";
public static final int ALGORITHM_BITS = 2048;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package com.jeejava.rsa;

import java.security.Key;

import javax.crypto.Cipher;

public class RSAEncryptDecrypt {

public static byte[] encrypt(String original, Key privateKey) {
if (original != null && privateKey != null) {
byte[] bs = original.getBytes();
byte[] encData = convert(bs, privateKey, Cipher.ENCRYPT_MODE);
return encData;
}
return null;
}

public static byte[] decrypt(byte[] encrypted, Key publicKey) {
if (encrypted != null && publicKey != null) {
byte[] decData = convert(encrypted, publicKey, Cipher.DECRYPT_MODE);
return decData;
}
return null;
}

private static byte[] convert(byte[] data, Key key, int mode) {
try {
Cipher cipher = Cipher.getInstance(RSAConstants.ALGORITHM);
cipher.init(mode, key);
byte[] newData = cipher.doFinal(data);
return newData;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.jeejava.rsa;

import java.security.KeyPair;
import java.security.KeyPairGenerator;

public class RSAKeyPair {

public static KeyPair keyPairRSA() {
KeyPairGenerator generator = null;
try {
generator = KeyPairGenerator.getInstance(RSAConstants.ALGORITHM);
} catch (Exception e) {
e.printStackTrace();
}
if (generator != null) {
generator.initialize(RSAConstants.ALGORITHM_BITS);
KeyPair keyPair = generator.genKeyPair();
return keyPair;
}
return null;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package com.jeejava.rsa;

import java.security.Key;
import java.security.KeyPair;
import java.util.Arrays;

public class RSATest {

public static void main(String[] args) {
String password = "password";
KeyPair keyPair = RSAKeyPair.keyPairRSA();
Key publicKey = keyPair.getPublic();
Key privateKey = keyPair.getPrivate();

System.out.println("Encrypt Start");
System.out.println("Original: " + password);
byte[] encrypted = RSAEncryptDecrypt.encrypt(password, privateKey);
System.out.println("Encrypted: " + new String(encrypted));
System.out.println("Encrypt End");

System.out.println();

System.out.println("Decrypt Start");
byte[] decrypted = RSAEncryptDecrypt.decrypt(encrypted, publicKey);
System.out.println("Decrypted: " + new String(decrypted));
System.out.println("Decrypted matches Original: " + Arrays.equals(decrypted, password.getBytes()));
System.out.println("Decrypt End");
}

}

0 comments on commit 6df1605

Please sign in to comment.