forked from roytuts/java
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
102 additions
and
0 deletions.
There are no files selected for viewing
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
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/ |
10 changes: 10 additions & 0 deletions
10
rsa-encryption-decryption/src/main/java/com/jeejava/rsa/RSAConstants.java
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
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; | ||
} |
38 changes: 38 additions & 0 deletions
38
rsa-encryption-decryption/src/main/java/com/jeejava/rsa/RSAEncryptDecrypt.java
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
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; | ||
} | ||
|
||
} |
23 changes: 23 additions & 0 deletions
23
rsa-encryption-decryption/src/main/java/com/jeejava/rsa/RSAKeyPair.java
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
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; | ||
} | ||
|
||
} |
30 changes: 30 additions & 0 deletions
30
rsa-encryption-decryption/src/main/java/com/jeejava/rsa/RSATest.java
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
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"); | ||
} | ||
|
||
} |