Hi All,
I have 2 pairs of Keys. I am encrypting a String with 1st public Key and 2nd Private Key,then saving the encrypted string in a text file. Then I am decrypting the string by reading from the file, decrypting with 1st public key and 2nd private key. Some this encryption-decrypting works fine, but some times I get jun charecters instead of plain text after decryption. Following is the code.Is there any problem in encryption or generating key methods?Please help me,
public class EncryptionManager {
public PublicKey loadPublicKeyFromKeyFile(String path, String algorithm)
throws IOException, NoSuchAlgorithmException,
InvalidKeySpecException {
// Read Public Key.
File filePublicKey = new File(path);
FileInputStream fis = new FileInputStream(path);
byte[] encodedPublicKey = new byte[(int) filePublicKey.length()];
fis.read(encodedPublicKey);
fis.close();
// Generate KeyPair.
KeyFactory keyFactory = KeyFactory.getInstance(algorithm);
X509EncodedKeySpec publicKeySpec = new X509EncodedKeySpec(
encodedPublicKey);
PublicKey publicKey = keyFactory.generatePublic(publicKeySpec);
return publicKey;
}
public byte[] doEncryption(String plainText, Key pub) throws
NoSuchAlgorithmException, NoSuchPaddingException,
InvalidKeyException, IllegalBlockSizeException,
BadPaddingException {
Cipher rsaCipher = Cipher.getInstance("RSA/ECB/NoPadding");
rsaCipher.init(Cipher.ENCRYPT_MODE, pub);
byte[] ciphertext = rsaCipher.doFinal(plainText.getBytes());
//System.out.println(new sun.misc.BASE64Encoder().encode(ciphertext));
return ciphertext;
}
public String doDecryption(byte[] encryptedText,Key key) throws
NoSuchAlgorithmException, InvalidKeyException,
NoSuchPaddingException, IllegalBlockSizeException,
BadPaddingException{
// Do the decryption
String plainText = "";
Cipher rsaCipher = Cipher.getInstance("RSA/ECB/NoPadding");
rsaCipher.init(Cipher.DECRYPT_MODE, key);
byte[] decryptText = rsaCipher.doFinal(encryptedText);
plainText = new String(decryptText);
return plainText;
}
PrivateKey loadPrivateKeyFromFile(String path,String algorithm)
throws IOException, NoSuchAlgorithmException,
InvalidKeySpecException{
// Read Private Key.
File filePrivateKey = new File(path);
FileInputStream fis = new FileInputStream(path);
byte[] encodedPrivateKey = new byte[(int) filePrivateKey.length()];
fis.read(encodedPrivateKey);
fis.close();
KeyFactory keyFactory = KeyFactory.getInstance(algorithm);
PKCS8EncodedKeySpec privateKeySpec = new PKCS8EncodedKeySpec(
encodedPrivateKey);
PrivateKey privateKey = keyFactory.generatePrivate(privateKeySpec);
return privateKey;
}
public void SaveKeyPair(String path, KeyPair keyPair) throws IOException {
PrivateKey privateKey = keyPair.getPrivate();
PublicKey publicKey = keyPair.getPublic();
// Store Public Key.
X509EncodedKeySpec x509EncodedKeySpec = new X509EncodedKeySpec(publicKey.getEncoded());
FileOutputStream fos = new FileOutputStream(path + "/public.key");
fos.write(x509EncodedKeySpec.getEncoded());
fos.close();
// Store Private Key.
PKCS8EncodedKeySpec pkcs8EncodedKeySpec =
new PKCS8EncodedKeySpec(
privateKey.getEncoded());
fos = new FileOutputStream(path + "/private.key");
fos.write(pkcs8EncodedKeySpec.getEncoded());
fos.close();
System.out.println("Keys are saved in location "+path);
}
String getBASE64EncoderString(byte[] b) {
return new sun.misc.BASE64Encoder().encode(b);
}
byte[] getBASE64DecoderString(String b)
throws IOException {
return new sun.misc.BASE64Decoder().decodeBuffer(b);
}
String getKeys(int length) throws
NoSuchAlgorithmException, IOException {
EncryptionManager enpManager = new EncryptionManager();
KeyPair keyPair = enpManager.generateKeyPair(length);
String pubKeyStr = "";
PublicKey pubKey = keyPair.getPublic();
// Store Private Key.
String path = ResourceBundle.getBundle("Utilities")
.getString("KeyPath");
//enpManager.savePrivateKey(privKey,path);
enpManager.SaveKeyPair(path, keyPair);
pubKeyStr = getBASE64EncoderString(pubKey.getEncoded());
return pubKeyStr;
}
KeyPair generateKeyPair(int length) throws
NoSuchAlgorithmException {
KeyPairGenerator keyGen = KeyPairGenerator.getInstance("RSA");
keyGen.initialize(length);
KeyPair generatedKeyPair = keyGen.genKeyPair();
return generatedKeyPair;
}
PrivateKey loadPrivateKeyFromFile(String path,String algorithm)
throws IOException, NoSuchAlgorithmException,
InvalidKeySpecException{
// Read Private Key.
File filePrivateKey = new File(path);
FileInputStream fis = new FileInputStream(path);
byte[] encodedPrivateKey = new byte[(int) filePrivateKey.length()];
fis.read(encodedPrivateKey);
fis.close();
KeyFactory keyFactory = KeyFactory.getInstance(algorithm);
PKCS8EncodedKeySpec privateKeySpec = new PKCS8EncodedKeySpec(
encodedPrivateKey);
PrivateKey privateKey = keyFactory.generatePrivate(privateKeySpec);
return privateKey;
}
}