Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I encrypt/decrypt a string with another string as a password?

I'm making a simple program that takes text entered in a text box, and takes a password that's in another text box, then does some sort of simple encryption on it and saves it to a file. Afterwards, a user should be able to open up the file again and provide the password that was used to encrypt it and it should spit out the original text.

Right now I'm taking the string. Separate it into a char array, then doing the same for the password. After that, I take the password, convert all those chars to integers, find the average value for all of them, and use it as an offset to the chars int he original text. Kind of like:

textChars[1]= (char)((int)textChars[1]+offset);

Then I can do the reverse for the encrypted string:

encryptedChars[1]= (char)((int)encryptedChars[1]-offset);

The problem is that characters have different values on different platforms so sometimes the offset will turn the char into some crazy number (like a negative value) which will just turn the char into a question mark.

I looked at the crypto library in the standard Java API, but I feel confused as to how the key works if it's just randomly generated every time I start the program.

What I need is two functions that look like String encrypt(String text,String Password) which spits out the text encrypted with the password as a key to decrypting it, and String decrypt(String encryptedText, String Password) which would spit out the original text (or gibberish if the password is junk)

Any help is really appreciated, this is really just a personal project so I don't need any fancy encryption methods.

like image 886
RangerMauve Avatar asked Oct 14 '11 03:10

RangerMauve


People also ask

How do you encrypt a string with public and private keys?

Encode the string to byte string. Then encrypt the byte string with the public key. Then the encrypted string can be decrypted with the private key. The public key can only be used for encryption and the private can only be used for decryption.

How to encrypt a string in Python?

Install the python cryptography library with the following command. Then generate an encryption key, that can be used for encryption and decryption. Convert the string to byte string, so that it can be encrypted.

How do I decrypt a string from a my Documents folder?

Add a method that reads the encrypted string from the user's My Documents folder and decrypts the string with the wrapper's DecryptData method.

How to encrypt a string in Python with RSA?

Install the python rsa library with the following command. Generate public and private keys with rsa.newkeys () method. Encode the string to byte string. Then encrypt the byte string with the public key. Then the encrypted string can be decrypted with the private key.


1 Answers

You're trying to re-invent the wheel. Unless you're doing it for fun, I'd recommend using something like AES. If you just google "AES in java" you'll find a number of examples.

If you are doing it for fun and want something simple to implement, have a look at ROT13 as well.

Here's an example for AES in Java:

private static final String ALGORITHM = "AES";
private static final byte[] keyValue = 
    new byte[] { 'T', 'h', 'i', 's', 'I', 's', 'A', 'S', 'e', 'c', 'r', 'e', 't', 'K', 'e', 'y' };

 public String encrypt(String valueToEnc) throws Exception {
    Key key = generateKey();
    Cipher c = Cipher.getInstance(ALGORITHM);
    c.init(Cipher.ENCRYPT_MODE, key);
    byte[] encValue = c.doFinal(valueToEnc.getBytes());
    String encryptedValue = new BASE64Encoder().encode(encValue);
    return encryptedValue;
}

public String decrypt(String encryptedValue) throws Exception {
    Key key = generateKey();
    Cipher c = Cipher.getInstance(ALGORITHM);
    c.init(Cipher.DECRYPT_MODE, key);
    byte[] decordedValue = new BASE64Decoder().decodeBuffer(encryptedValue);
    byte[] decValue = c.doFinal(decordedValue);
    String decryptedValue = new String(decValue);
    return decryptedValue;
}

private static Key generateKey() throws Exception {
    Key key = new SecretKeySpec(keyValue, ALGORITHM);
    return key;
}

You may want to improve on this code.

like image 183
Akshay Avatar answered Sep 24 '22 18:09

Akshay