Using hybrid encryption/decryption

Discussion in 'Java' started by Susanne Kaufmann, Apr 18, 2007.

  1. Hello, I generated a RSA-keypair. Each key has been base64-encoded, so
    I can save it easily as string into a database (and/or file).

    When I am now encrypting a file, I use AES, and after that I wrap the
    AES-key with the RSA publickey. The wrapped-Key gets base64-encoded,
    too.

    My problem is, that I cannot get key back (I think). The error message
    I get is "java.security.InvalidKeyException: unknown block type". I
    looked for it, and I found some information, that this is a hint to a
    wrong key.

    One thing I figured out is, that the size of my unwrapped key is too
    big (a bytes/bit msitake? ).

    Here is my code:

    Key-Generation extract:

    File publicKeyFile = new File(args[1]);
    File privateKeyFile = new File(args[2]);
    KeyPairGenerator pairgen = KeyPairGenerator.getInstance("RSA", "BC");
    SecureRandom random = new SecureRandom();
    pairgen.initialize(KEYSIZE, random);
    KeyPair keyPair = pairgen.generateKeyPair();
    Base64 b64 = new Base64();
    String keyPub = new
    String(b64.encode(keyPair.getPublic().getEncoded()),"ASCII");
    String keyPri = new
    String(b64.encode(keyPair.getPrivate().getEncoded()),"ASCII");


    File-encryption:

    public String encrypt(FileInputStream fis, FileOutputStream fos, int
    contactID)
    {
    Key publicKey =null;
    SecretKey innerKey = null;
    String keyForDB ="";
    try
    {
    KeyGenerator keygen = KeyGenerator.getInstance("AES");
    SecureRandom random = new SecureRandom();
    keygen.init(128, random);
    innerKey = keygen.generateKey();
    // get recipients publicKey from database
    String keyBlob = db.getPublicKey(contactID);
    byte[] publicBytes = b64.decode(keyBlob.getBytes());
    X509EncodedKeySpec keySpec = new X509EncodedKeySpec(publicBytes);
    try{
    KeyFactory keyFactory = KeyFactory.getInstance("RSA","BC");
    publicKey = (Key)keyFactory.generatePublic(keySpec);
    }
    catch(NoSuchAlgorithmException e){}
    catch(InvalidKeySpecException e){
    e.printStackTrace();
    }

    Cipher cipher = Cipher.getInstance("RSA/NONE/PKCS1PADDING","BC");
    cipher.init(Cipher.WRAP_MODE, publicKey);
    byte[] wrappedKey = cipher.wrap(innerKey);
    keyForDB = new String(b64.encode(wrappedKey),"ASCII");
    DataOutputStream out = new DataOutputStream(fos);

    InputStream in = fis;
    cipher = Cipher.getInstance("AES/ECB/PKCS7Padding","BC");
    cipher.init(Cipher.ENCRYPT_MODE, innerKey);
    crypt(in, out, cipher);
    in.close();
    out.close();
    }
    catch (IOException e)
    {
    e.printStackTrace();
    }
    catch (GeneralSecurityException e)
    {
    e.printStackTrace();
    }
    return keyForDB;
    }

    File Decryption:

    public void decrypt(File file, FileOutputStream fos, String
    wrappedKey)
    {
    try
    {
    byte[] wrappedKeyBytes = b64.decode(wrappedKey.getBytes());
    DataInputStream in = new DataInputStream(new
    FileInputStream(file));
    String keyString = readFileAsString("keys/privatekey");
    byte[] privateKeyBytes = b64.decode(new
    String(keyString).getBytes());
    PKCS8EncodedKeySpec encKeySpec = new
    PKCS8EncodedKeySpec(privateKeyBytes);
    KeyFactory keyFactory = KeyFactory.getInstance("RSA", "BC");
    Key privateKey = keyFactory.generatePrivate(encKeySpec);
    Cipher cipher = Cipher.getInstance("RSA/NONE/PKCS1PADDING",
    "BC");
    cipher.init(Cipher.UNWRAP_MODE, privateKey);
    SecretKey key = (SecretKey)cipher.unwrap(wrappedKeyBytes, "AES",
    Cipher.SECRET_KEY);
    OutputStream out = fos;
    cipher = Cipher.getInstance("AES/ECB/PKCS7Padding", "BC");
    cipher.init(Cipher.DECRYPT_MODE, key);
    crypt(in, out, cipher);
    in.close();
    out.close();
    }
    catch (IOException e)
    {
    e.printStackTrace();
    }
    catch (GeneralSecurityException e)
    {
    e.printStackTrace();
    }
    }


    Thank you for any help,

    Susanne
    Susanne Kaufmann, Apr 18, 2007
    #1
    1. Advertising

  2. I got the mistake: The unknown block type was "NONE". I inserted "ECB"
    and now it works,

    Susanne
    Susanne Kaufmann, Apr 19, 2007
    #2
    1. Advertising

  3. Susanne Kaufmann

    Guest

    Hai Susanne,
    If you don't mind, may I ask the full code for my college assignment?
    , Dec 16, 2013
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Srinivasa Reddy K Ganji

    database connection string encryption and decryption

    Srinivasa Reddy K Ganji, Jul 18, 2003, in forum: ASP .Net
    Replies:
    1
    Views:
    693
    Gary Varga
    Jul 18, 2003
  2. Replies:
    2
    Views:
    1,606
    Sushant Bhatia
    May 3, 2005
  3. Rogue Chameleon

    Encryption & Decryption

    Rogue Chameleon, Sep 23, 2004, in forum: Java
    Replies:
    6
    Views:
    574
  4. Replies:
    6
    Views:
    658
    DishanF
    Jan 12, 2005
  5. Henrik Bruhn

    encryption and decryption using SoapExtension

    Henrik Bruhn, Feb 18, 2004, in forum: ASP .Net Web Services
    Replies:
    0
    Views:
    142
    Henrik Bruhn
    Feb 18, 2004
Loading...

Share This Page