Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Encryption

  1. #1
    Junior Member
    Join Date
    Jul 2013
    Posts
    18
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Encryption

    I'm currently struggling on sealing objects for an inherited class and was wondering if anyone here could help.


  2. #2
    Junior Member
    Join Date
    Jul 2013
    Posts
    18
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Encryption

    How do I use this code with a fixed key instead of a generated key?

    public static void main(String[] args) throws Exception {
     
    	Security.addProvider(new FlexiCoreProvider());
     
    	Cipher cipher = Cipher.getInstance("AES128_CBC", "FlexiCore");
    	KeyGenerator keyGen = KeyGenerator.getInstance("AES", "FlexiCore");
    	SecretKey secKey = keyGen.generateKey();
            System.out.println(secKey);
     
    	// Encrypt
     
    	cipher.init(Cipher.ENCRYPT_MODE, secKey);
     
    	String cleartextFile = "cleartextFile.txt";
    	String ciphertextFile = "ciphertextFile.txt";
     
    	FileInputStream fis = new FileInputStream(cleartextFile);
    	FileOutputStream fos = new FileOutputStream(ciphertextFile);
    	CipherOutputStream cos = new CipherOutputStream(fos, cipher);
     
    	byte[] block = new byte[8];
    	int i;
    	while ((i = fis.read(block)) != -1) {
    	    cos.write(block, 0, i);
    	}
    	cos.close();
     
    	// Decrypt
     
    	String cleartextAgainFile = "cleartextAgainFile.txt";
     
    	cipher.init(Cipher.DECRYPT_MODE, secKey);
     
    	fis = new FileInputStream(ciphertextFile);
    	CipherInputStream cis = new CipherInputStream(fis, cipher);
    	fos = new FileOutputStream(cleartextAgainFile);
     
    	while ((i = cis.read(block)) != -1) {
    	    fos.write(block, 0, i);
    	}
    	fos.close();
        }

Similar Threads

  1. read & write object in object file
    By amirsarem in forum What's Wrong With My Code?
    Replies: 1
    Last Post: June 8th, 2013, 11:35 AM
  2. static variable accessible in object b, not in object a?
    By Pajaro in forum What's Wrong With My Code?
    Replies: 4
    Last Post: March 30th, 2013, 02:41 PM
  3. testing weather one object is greater than another object
    By u-will-neva-no in forum Object Oriented Programming
    Replies: 1
    Last Post: February 19th, 2012, 07:02 PM
  4. Reading from ResultSet to Object and from object Object Array
    By anmaston in forum What's Wrong With My Code?
    Replies: 4
    Last Post: April 7th, 2011, 06:11 AM