Evening guys,
I really should learn to go to bed and give up coding!
Folling a previous post:
I take plaintext, generate a random DES key and encrypt the ciphertext.
I then take a passphrase (a 16 character alphanumeric string) and using the PBEwithMD5andDES, create a key with which i encrypt my first random DES key to form an encrypted key (my FINAL KEY)
The ciphertext and the final key are encoded using the BASE64Encoder and saved as string to a database (havent included that bit in my test file).
The problem is now reversing the process to get my original plaintext.
I load the final key and the 16character passphrase - decode the final key, but when i try to decode i get a:
Exception in thread "main" javax.crypto.BadPaddingException: Given final block not properly padded
at com.sun.crypto.provider.DESCipher.engineDoFinal(DashoA6275)
at com.sun.crypto.provider.DESCipher.engineDoFinal(DashoA6275)
at com.sun.crypto.provider.PBEWithMD5AndDESCipher.engineDoFinal(DashoA6275)
at javax.crypto.Cipher.doFinal(DashoA6275)
at SecPrescrip.main(SecPrescrip.java:138)
error.
Can anyone spot my error? Will this be repeated when i decrypt at my final stage of the progression? Thanks so your help in advance
Cheers!
Relisys
============== CODE FOLLOWS ==================
import java.io.*;
import java.security.*;
import javax.crypto.*;
import com.sun.crypto.provider.SunJCE;
import sun.misc.*;
import java.security.spec.*;
import javax.crypto.spec.*;
public class SecPrescrip {
public static void main(String[] args) throws Exception {
// Create Key.
Key key;
KeyGenerator generator = KeyGenerator.getInstance("DES"
generator.init(new SecureRandom());
key = generator.generateKey();
// Get a cipher object
Cipher cipher = Cipher.getInstance("DES/ECB/PKCS5Padding"
// Encrypt the input string:
cipher.init(Cipher.ENCRYPT_MODE, key);
String input = "Medicare Secure Prescription: 30 Tamazopan 200mg tablets. Dosage: 1 to be taken every 4 hours";
System.out.println("Stage 1: ENCRYPT PRESCRIPTION WITH A RANDOM DES KEY"
System.out.println("==================================================="
System.out.println(" - Input Plain Text: "+input);
System.out.println(""
byte[] stringBytes = input.getBytes("UTF8"
byte[] raw = cipher.doFinal(stringBytes);
BASE64Encoder encoder = new BASE64Encoder();
String ciphertext1 = encoder.encode(raw);
System.out.println(" - Cipher Text: "+ciphertext1);
System.out.println(""
byte[] keybytes = key.getEncoded();
String randomkey = encoder.encode(keybytes);
System.out.println(" - Random Prescription Key: "+randomkey);
System.out.println(""
System.out.println("ENCRYPTION SUCESSFULL"
System.out.println(""
System.out.println(""
System.out.println("Stage 2: ENCRYPT RANDOM KEY WITH PATIENT MEDICARE KEY"
System.out.println("====================================================="
BASE64Decoder decoder = new BASE64Decoder();
String passphrase = "ABCD1234efghIJ56"; //Patient Medicare Key
System.out.println(" - Patient Medicare Key: "+passphrase);
System.out.println(""
System.out.println(" - Input Plain Text: "+randomkey);
String algorithm = "PBEWithMD5AndDES";
byte[] salt = new byte[8];
int iteration = 20;
KeySpec ks = new PBEKeySpec(passphrase.toCharArray());
SecretKeyFactory skf = SecretKeyFactory.getInstance(algorithm);
SecretKey key2 = skf.generateSecret(ks);
byte[] input2 = decoder.decodeBuffer(randomkey);
MessageDigest md = MessageDigest.getInstance("MD5"
md.update(passphrase.getBytes());
md.update(input2);
byte[] digest = md.digest();
System.arraycopy(digest, 0, salt, 0, 8);
AlgorithmParameterSpec aps = new PBEParameterSpec(salt, iteration);
cipher = Cipher.getInstance(algorithm);
cipher.init(Cipher.ENCRYPT_MODE, key2, aps);
byte[] outputFinalKey = cipher.doFinal(input2);
String ciphertext2 = encoder.encode(outputFinalKey);
System.out.println(" - Cipher Text (Final Prescription Key): "+ciphertext2);
System.out.println(""
System.out.println(""
System.out.println("ENCRYPTION SUCESSFULL"
System.out.println(""
System.out.println(""
System.out.println("Stage 3: DECRYPT PRESCRIPTION KEY USING PATIENT MEDICARE KEY"
System.out.println("============================================================"
//NOT CHANGED String passphrase = "ABCD1234efghIJ56";
System.out.println(" - Patient Medicare Key: "+passphrase);
System.out.println(""
System.out.println(" - Input Plain Text: "+ciphertext2);
algorithm = "PBEWithMD5AndDES";
salt = new byte[8];
iteration = 20;
ks = new PBEKeySpec(passphrase.toCharArray());
skf = SecretKeyFactory.getInstance(algorithm);
SecretKey key3 = skf.generateSecret(ks);
//Load in the input bytes as if they had been loaded from an sql database or the like
byte[] input3 = decoder.decodeBuffer(ciphertext2);
int input3length = input3.length;
byte[] input4 = new byte[input3length - 8];
/**
* This next part is where i'm having real problems!
**/
for (int i = 0; i < (input3.length-8); i++){
input4[ i] = input3[i+7];
}
//Load salt (should be first 8 bytes)
for (int i = 0; i < 8; i++){
salt[ i] = input3[ i];
}
aps = new PBEParameterSpec(salt, iteration);
cipher = Cipher.getInstance(algorithm);
cipher.init(Cipher.DECRYPT_MODE, key3, aps);
byte[] outputKey1 = cipher.doFinal(input3);
String plaintext1 = encoder.encode(outputKey1);
System.out.println(" - Plain Text (Random Generated Key): "+plaintext1);
System.out.println(""
System.out.println(""
System.out.println("ENCRYPTION SUCESSFULL"
System.out.println(""
System.out.println(""
System.out.println("Stage 4: DECRYPT PRESCRIPTION KEY USING PATIENT MEDICARE KEY"
System.out.println("============================================================"
// The decrypter string plaintext should be the same as the BASE64 Encoded representation of the random DES string
}
}
I really should learn to go to bed and give up coding!
Folling a previous post:
I take plaintext, generate a random DES key and encrypt the ciphertext.
I then take a passphrase (a 16 character alphanumeric string) and using the PBEwithMD5andDES, create a key with which i encrypt my first random DES key to form an encrypted key (my FINAL KEY)
The ciphertext and the final key are encoded using the BASE64Encoder and saved as string to a database (havent included that bit in my test file).
The problem is now reversing the process to get my original plaintext.
I load the final key and the 16character passphrase - decode the final key, but when i try to decode i get a:
Exception in thread "main" javax.crypto.BadPaddingException: Given final block not properly padded
at com.sun.crypto.provider.DESCipher.engineDoFinal(DashoA6275)
at com.sun.crypto.provider.DESCipher.engineDoFinal(DashoA6275)
at com.sun.crypto.provider.PBEWithMD5AndDESCipher.engineDoFinal(DashoA6275)
at javax.crypto.Cipher.doFinal(DashoA6275)
at SecPrescrip.main(SecPrescrip.java:138)
error.
Can anyone spot my error? Will this be repeated when i decrypt at my final stage of the progression? Thanks so your help in advance
Cheers!
Relisys
============== CODE FOLLOWS ==================
import java.io.*;
import java.security.*;
import javax.crypto.*;
import com.sun.crypto.provider.SunJCE;
import sun.misc.*;
import java.security.spec.*;
import javax.crypto.spec.*;
public class SecPrescrip {
public static void main(String[] args) throws Exception {
// Create Key.
Key key;
KeyGenerator generator = KeyGenerator.getInstance("DES"
generator.init(new SecureRandom());
key = generator.generateKey();
// Get a cipher object
Cipher cipher = Cipher.getInstance("DES/ECB/PKCS5Padding"
// Encrypt the input string:
cipher.init(Cipher.ENCRYPT_MODE, key);
String input = "Medicare Secure Prescription: 30 Tamazopan 200mg tablets. Dosage: 1 to be taken every 4 hours";
System.out.println("Stage 1: ENCRYPT PRESCRIPTION WITH A RANDOM DES KEY"
System.out.println("==================================================="
System.out.println(" - Input Plain Text: "+input);
System.out.println(""
byte[] stringBytes = input.getBytes("UTF8"
byte[] raw = cipher.doFinal(stringBytes);
BASE64Encoder encoder = new BASE64Encoder();
String ciphertext1 = encoder.encode(raw);
System.out.println(" - Cipher Text: "+ciphertext1);
System.out.println(""
byte[] keybytes = key.getEncoded();
String randomkey = encoder.encode(keybytes);
System.out.println(" - Random Prescription Key: "+randomkey);
System.out.println(""
System.out.println("ENCRYPTION SUCESSFULL"
System.out.println(""
System.out.println(""
System.out.println("Stage 2: ENCRYPT RANDOM KEY WITH PATIENT MEDICARE KEY"
System.out.println("====================================================="
BASE64Decoder decoder = new BASE64Decoder();
String passphrase = "ABCD1234efghIJ56"; //Patient Medicare Key
System.out.println(" - Patient Medicare Key: "+passphrase);
System.out.println(""
System.out.println(" - Input Plain Text: "+randomkey);
String algorithm = "PBEWithMD5AndDES";
byte[] salt = new byte[8];
int iteration = 20;
KeySpec ks = new PBEKeySpec(passphrase.toCharArray());
SecretKeyFactory skf = SecretKeyFactory.getInstance(algorithm);
SecretKey key2 = skf.generateSecret(ks);
byte[] input2 = decoder.decodeBuffer(randomkey);
MessageDigest md = MessageDigest.getInstance("MD5"
md.update(passphrase.getBytes());
md.update(input2);
byte[] digest = md.digest();
System.arraycopy(digest, 0, salt, 0, 8);
AlgorithmParameterSpec aps = new PBEParameterSpec(salt, iteration);
cipher = Cipher.getInstance(algorithm);
cipher.init(Cipher.ENCRYPT_MODE, key2, aps);
byte[] outputFinalKey = cipher.doFinal(input2);
String ciphertext2 = encoder.encode(outputFinalKey);
System.out.println(" - Cipher Text (Final Prescription Key): "+ciphertext2);
System.out.println(""
System.out.println(""
System.out.println("ENCRYPTION SUCESSFULL"
System.out.println(""
System.out.println(""
System.out.println("Stage 3: DECRYPT PRESCRIPTION KEY USING PATIENT MEDICARE KEY"
System.out.println("============================================================"
//NOT CHANGED String passphrase = "ABCD1234efghIJ56";
System.out.println(" - Patient Medicare Key: "+passphrase);
System.out.println(""
System.out.println(" - Input Plain Text: "+ciphertext2);
algorithm = "PBEWithMD5AndDES";
salt = new byte[8];
iteration = 20;
ks = new PBEKeySpec(passphrase.toCharArray());
skf = SecretKeyFactory.getInstance(algorithm);
SecretKey key3 = skf.generateSecret(ks);
//Load in the input bytes as if they had been loaded from an sql database or the like
byte[] input3 = decoder.decodeBuffer(ciphertext2);
int input3length = input3.length;
byte[] input4 = new byte[input3length - 8];
/**
* This next part is where i'm having real problems!
**/
for (int i = 0; i < (input3.length-8); i++){
input4[ i] = input3[i+7];
}
//Load salt (should be first 8 bytes)
for (int i = 0; i < 8; i++){
salt[ i] = input3[ i];
}
aps = new PBEParameterSpec(salt, iteration);
cipher = Cipher.getInstance(algorithm);
cipher.init(Cipher.DECRYPT_MODE, key3, aps);
byte[] outputKey1 = cipher.doFinal(input3);
String plaintext1 = encoder.encode(outputKey1);
System.out.println(" - Plain Text (Random Generated Key): "+plaintext1);
System.out.println(""
System.out.println(""
System.out.println("ENCRYPTION SUCESSFULL"
System.out.println(""
System.out.println(""
System.out.println("Stage 4: DECRYPT PRESCRIPTION KEY USING PATIENT MEDICARE KEY"
System.out.println("============================================================"
// The decrypter string plaintext should be the same as the BASE64 Encoded representation of the random DES string
}
}