Asymmetric Encryption Using Standalone Java Application
In this blog i only describe about asymmetric encryption. But there are to type
1. Symmetric encryption
2. Asymmetric encryption
What is Symmetric encryption?
Symmetric encryption is a form of computerized cryptography using a singular encryption key to guise an electronic message. Its data conversion uses a mathematical algorithm along with a secret key, which results in the inability to make sense out of a message.

What is asymmetric encryption?
Asymmetric Encryption is a form of Encryption where keys come in pairs. What one key encrypts, only the other can decryption. Frequently (but not necessarily), the keys are interchangeable, in the sense that if key A encrypts a message, then B can decrypt it, and if key B encrypts a message, then key A can decrypt it.

In asymmetric encryption we use two keys
1. Private key
2.Public Key
In asymmetric encryption there to way to encrypt and decrypt . If plain text encrypt using private key then it should decrypt by the public key. If plain text encrypt using public key it should decrtpt by using private key.
In this Java application I'm doing CLI program.
1. This has only one java class it call assignment2. To achieve the gall we have to import some packagers. . That are like this.
import java.util.Scanner;
import java.security.PublicKey;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.PrivateKey;
import java.util.Base64;
import java.util.Map;
import java.util.HashMap;
import java.util.Map;
import javax.crypto.KeyGenerator;
import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;
import javax.crypto.SecretKey;
2. After Generate the ket pair Using RSA algorithm.
//using RSA algo sirst creat the public and private key.
Map<String, Object> keys = new HashMap<String, Object>();
keys.put("private", privateKey);
keys.put("public", publicKey);
return keys;
KeyGenerator gen = KeyGenerator.getInstance("AES");
gen.init(255);
SecretKey secKey = gen.generateKey();
String encodKey = Base64.getEncoder().encodeToString(secKey.getEncoded());
return encodKey;
//Then use the cteated AES key to encrypet the plain text
byte[] decodedKey = Base64.getDecoder().decode(aesKeyString);
SecretKey originalKey = new SecretKeySpec(decodedKey, 0, decodedKey.length, "AES");
Cipher aesCipher = Cipher.getInstance("AES");
aesCipher.init(Cipher.ENCRYPT_MODE, originalKey);
byte[] byteCipherText = aesCipher.doFinal(Entered.getBytes());
return Base64.getEncoder().encodeToString(byteCipherText);
3. After generate key that can use encryption and decryption
private static String decryptAESKey(String encryptedAESKey, PublicKey publicKey) throws Exception {
Cipher C = Cipher.getInstance("RSA");
C.init(Cipher.DECRYPT_MODE, publicKey);
return new String(C.doFinal(Base64.getDecoder().decode(encryptedAESKey)));
}
private static String encryptAESKey(String plainAESKey, PrivateKey privateKey) throws Exception {
Cipher C = Cipher.getInstance("RSA");
C.init(Cipher.ENCRYPT_MODE, privateKey);
return Base64.getEncoder().encodeToString(C.doFinal(plainAESKey.getBytes()));
}
After run the program it's like this.
Click here to Source code
Comments
Post a Comment