Rsa Algorithm For Key Generation And Cipher Verification In Java

Posted on  by 

Nov 18, 2019  InkDeck (a play on the real title, EncDec (Encrypt/Decrypt)) is an application that uses RSA encryption for secure communications. The application can encrypt messages using RSA as well as decrypt them. The user is able to select public/private keys. Also includes KeyMaker, to generate a private and public key. I know how to generate rsa keys and encrypt an decrypt. My question what the difference is between a Java generated rsa key and a rsa key generated with openssh. Is there a way to convert this key in java? – Ralph Oct 23 '13 at 6:33. Java program to encrypt and decrypt a given message using RSA algorithm. Open Command Prompt and compile & Run. RSA algorithm is used to changing message that no one can understand the communication between sender and receiver. Sender and Receiver have public and private key and they can only understand message.

Example of RSA generation, sign, verify, encryption, decryption and keystores in Java
RsaExample.java
Rsa Algorithm For Key Generation And Cipher Verification In Java
importjavax.crypto.Cipher;
importjava.io.InputStream;
importjava.security.*;
importjava.util.Base64;
import staticjava.nio.charset.StandardCharsets.UTF_8;
publicclassRsaExample {
publicstaticKeyPairgenerateKeyPair() throwsException {
KeyPairGenerator generator =KeyPairGenerator.getInstance('RSA');
generator.initialize(2048, newSecureRandom());
KeyPair pair = generator.generateKeyPair();
return pair;
}
publicstaticKeyPairgetKeyPairFromKeyStore() throwsException {
//Generated with:
// keytool -genkeypair -alias mykey -storepass s3cr3t -keypass s3cr3t -keyalg RSA -keystore keystore.jks
InputStream ins =RsaExample.class.getResourceAsStream('/keystore.jks');
KeyStore keyStore =KeyStore.getInstance('JCEKS');
keyStore.load(ins, 's3cr3t'.toCharArray()); //Keystore password
KeyStore.PasswordProtection keyPassword =//Key password
newKeyStore.PasswordProtection('s3cr3t'.toCharArray());
KeyStore.PrivateKeyEntry privateKeyEntry = (KeyStore.PrivateKeyEntry) keyStore.getEntry('mykey', keyPassword);
java.security.cert.Certificate cert = keyStore.getCertificate('mykey');
PublicKey publicKey = cert.getPublicKey();
PrivateKey privateKey = privateKeyEntry.getPrivateKey();
returnnewKeyPair(publicKey, privateKey);
}
publicstaticStringencrypt(StringplainText, PublicKeypublicKey) throwsException {
Cipher encryptCipher =Cipher.getInstance('RSA');
encryptCipher.init(Cipher.ENCRYPT_MODE, publicKey);
byte[] cipherText = encryptCipher.doFinal(plainText.getBytes(UTF_8));
returnBase64.getEncoder().encodeToString(cipherText);
}
publicstaticStringdecrypt(StringcipherText, PrivateKeyprivateKey) throwsException {
byte[] bytes =Base64.getDecoder().decode(cipherText);
Cipher decriptCipher =Cipher.getInstance('RSA');
decriptCipher.init(Cipher.DECRYPT_MODE, privateKey);
returnnewString(decriptCipher.doFinal(bytes), UTF_8);
}
publicstaticStringsign(StringplainText, PrivateKeyprivateKey) throwsException {
Signature privateSignature =Signature.getInstance('SHA256withRSA');
privateSignature.initSign(privateKey);
privateSignature.update(plainText.getBytes(UTF_8));
byte[] signature = privateSignature.sign();
returnBase64.getEncoder().encodeToString(signature);
}
publicstaticbooleanverify(StringplainText, Stringsignature, PublicKeypublicKey) throwsException {
Signature publicSignature =Signature.getInstance('SHA256withRSA');
publicSignature.initVerify(publicKey);
publicSignature.update(plainText.getBytes(UTF_8));
byte[] signatureBytes =Base64.getDecoder().decode(signature);
return publicSignature.verify(signatureBytes);
}
publicstaticvoidmain(String... argv) throwsException {
//First generate a public/private key pair
KeyPair pair = generateKeyPair();
//KeyPair pair = getKeyPairFromKeyStore();
//Our secret message
String message ='the answer to life the universe and everything';
//Encrypt the message
String cipherText = encrypt(message, pair.getPublic());
//Now decrypt it
String decipheredMessage = decrypt(cipherText, pair.getPrivate());
System.out.println(decipheredMessage);
//Let's sign our message
String signature = sign('foobar', pair.getPrivate());
//Let's check the signature
boolean isCorrect = verify('foobar', signature, pair.getPublic());
System.out.println('Signature correct: '+ isCorrect);
}
}

commented Oct 17, 2019

Rsa Algorithm For Key Generation And Cipher Verification In Java 10

It's good thank you so much , How can i create base64 like jwt (header,body,sign) ?

commented Nov 26, 2019

Thanks for the code. One issue - using openjdk version '11.0.5-ea' 2019-10-15 requires the KeyStore.getInstance('JCEKS') code to be KeyStore.getInstance('PKCS12').

Write A Program In Java To Implement Rsa Algorithm For Key Generation And Cipher Verification

commented Dec 29, 2019

Rsa Algorithm For Key Generation And Cipher Verification In Java Free

@stdunbar: It depends on your keyStore creation.

Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment

Coments are closed