Adblock breaks this site

Need help with encryption

Discussion in 'Programming General' started by Solarsonic888, Feb 21, 2011.

  1. Solarsonic888

    Solarsonic888 Active Member
    Banned

    Joined:
    Feb 4, 2011
    Posts:
    234
    Referrals:
    0
    Sythe Gold:
    0
    Need help with encryption

    I understand one way encryption, but let's have a situation.

    I have two programs. One will write to a .db file. The second one will read the .db file. (both are separate programs ran by different people).

    The writing will done using Strings indicated from a text field (unique values), meaning that the reading must be done completely off the data.db file, the reading program will have no source for the values except for the data.db file.

    How can I encrypt the values into the data.db using the writer, and then read their true value using the reader? (keeping in mind that it can't be easily decrypted by an outside source)

    Thanks a lot,
    ~Solarsonic888
     
  2. Nullware

    Nullware Guru

    Joined:
    Jan 30, 2007
    Posts:
    1,761
    Referrals:
    4
    Sythe Gold:
    0
    Need help with encryption

    I would think PGP encryption should be good enough to meet those needs. Have fun figuring out something like an "easy" library from Gnu though.
     
  3. blue_dolphin

    blue_dolphin Newcomer

    Joined:
    Feb 8, 2011
    Posts:
    5
    Referrals:
    0
    Sythe Gold:
    0
    Need help with encryption

    You can use public key encryption and just store the secret key internally in the reader.
     
  4. smooth_dudes

    smooth_dudes Newcomer

    Joined:
    Jul 7, 2011
    Posts:
    1
    Referrals:
    0
    Sythe Gold:
    0
    Need help with encryption

    You could also use symmetric encryption for encrypting the strings.

    Code:
    import java.security.NoSuchAlgorithmException;
    import java.security.NoSuchProviderException;
    import javax.crypto.BadPaddingException;
    import javax.crypto.Cipher;
    import javax.crypto.IllegalBlockSizeException;
    import javax.crypto.NoSuchPaddingException;
    import java.security.Key;
    import java.security.InvalidKeyException;
    import javax.crypto.spec.SecretKeySpec;
    
    public class Cryptograph {
    
        private Cipher cipher;
        private Key key;
        //create user key.
        private byte[] keyBytes = new byte[16];
        private byte[] jceKeyBytes = new byte[32];
    
        //default 256 bit key.
        /*
        private static byte[] defaultKeyBytes = new byte[] {
        (byte) 0x00a, (byte) 0x01b, (byte) 0x02c, (byte) 0x03dd, //32 bit
        (byte) 0x04e, (byte) 0x05f, (byte) 0x0a1, (byte) 0x0c2, //64 bit
        (byte) 0x0d3, (byte) 0x0e4, (byte) 0x0f5, (byte) 0x100, //96 bit
        (byte) 0x00e, (byte) 0x00d, (byte) 0x00e, (byte) 0x00f, //128 bit
        (byte) 0x00a, (byte) 0x00b, (byte) 0x00d, (byte) 0x0f6, //160 bit
        (byte) 0x0f7, (byte) 0x0f9, (byte) 0x000, (byte) 0x001, //192 bit
        (byte) 0x002, (byte) 0x003, (byte) 0x004, (byte) 0x005, //224 bit
        (byte) 0x006, (byte) 0x007, (byte) 0x008, (byte) 0x009  //256 bit
        };
         */
    
        public Cryptograph() {
    
    
        };
    
        private void generateKey(String input, boolean jcePolicy) {
    
            if (jcePolicy == true) {
    
                for (int i = 0; i < input.length(); i++) {
    
                    jceKeyBytes[i] = (byte) input.charAt(i);
                }
                key = new SecretKeySpec(jceKeyBytes, "AES");
            }
            else {
                
                for (int i = 0; i < input.length(); i++) {
    
                    keyBytes[i] = (byte) input.charAt(i);
                }
                key = new SecretKeySpec(keyBytes, "AES");
            }
        }
    
        public byte[] encrypt(String input, String inputKey, boolean jcePolicy) throws InvalidKeyException,
                BadPaddingException,
                IllegalBlockSizeException,
                NoSuchAlgorithmException,
                NoSuchPaddingException,
                NoSuchProviderException {
    
            generateKey(inputKey, jcePolicy);
    
            cipher = Cipher.getInstance("AES");
            cipher.init(Cipher.ENCRYPT_MODE, key);
    
            byte[] inputBytes = input.getBytes();
    
            return cipher.doFinal(inputBytes);
        }
    
        public String decrypt(byte[] input, String inputKey, boolean jcePolicy) throws InvalidKeyException,
                BadPaddingException,
                IllegalBlockSizeException,
                NoSuchAlgorithmException,
                NoSuchPaddingException {
    
            generateKey(inputKey, jcePolicy);
    
            cipher = Cipher.getInstance("AES");
            cipher.init(Cipher.DECRYPT_MODE, key);
    
            byte[] decryptedBytes = cipher.doFinal(input);
    
            return new String(decryptedBytes);
        }
    }
    Code:
    public class HexadecimalEncoder {
    
    
        public HexadecimalEncoder() {
    
        }
    
         //convert byte to hex-encoded string
        public String byteToHexString(byte bytes[]) {
    
            StringBuffer retString = new StringBuffer();
    
            for (int i = 0; i < bytes.length; ++i) {
    
                retString.append(Integer.toHexString(0x0100 + (bytes[i] & 0x00FF)).substring(1));
            }
            return retString.toString();
        }
    
        //convert hex-encoded string to byte
        public byte[] hexStringToByte(String s) {
    
            byte[] bts = new byte[s.length() / 2];
    
            for (int i = 0; i < bts.length; i++) {
    
                bts[i] = (byte) Integer.parseInt(s.substring(2 * i, 2 * i + 2), 16);
            }
            return bts;
        }
    
        //convert string to hex-encoded string
        public String stringToHex(String s) {
    
            char[] chars = s.toCharArray();
            StringBuffer output = new StringBuffer();
    
            for (int i = 0; i < chars.length; i++) {
    
                output.append(Integer.toHexString((int) chars[i]));
            }
            return output.toString();
        }
        //convert hex-encoded string to string
    
        public String hexToString(String s) {
    
            byte[] txtInByte = new byte[s.length() / 2];
            int j = 0;
            for (int i = 0; i < s.length(); i += 2) {
                txtInByte[j++] = Byte.parseByte(s.substring(i, i + 2), 16);
            }
            return new String(txtInByte);
        }
    
    }
     
< [HELP] Proxy with HttpWebRequests? | Java Question >


 
 
Adblock breaks this site