Simplest way to encrypt a text file in java

前端 未结 11 1500
萌比男神i
萌比男神i 2020-11-30 01:18

For my School project I had to show that I can utilize file handling within a program. For this I made a very simple login process that you can create an account on that wri

相关标签:
11条回答
  • 2020-11-30 02:01

    An easy and fun scrambling algorithm would be the Burrows-Wheeler transform. Not really a secure encryption, but seriously, it's a school work and this is awesome.

    0 讨论(0)
  • 2020-11-30 02:04

    A very basic method would be to xor the data with a key. This method is symmetrical, i.e you can use the same key to decode as encode.

    If we choose a 1 byte key it's nice and simple, enough to make it unreadable (but not at all secure!):

    private void encodeDecode(byte[] bytes, byte key) {
        for(int i=0; i<bytes.length; i++)
            bytes[i] = (byte) (bytes[i]^key);
    }
    
    0 讨论(0)
  • 2020-11-30 02:05

    use simple subtitute encryption algorythm, change every character into number or other character.

    1. get every character of your string.
    2. get the ascii value of the string.
    3. add the ascii value with specific integer (this will be your encryption key)
    4. display the result
    0 讨论(0)
  • 2020-11-30 02:05

    Bouncy Castle Crypto API is a lightweight cryptography API in Java.

        import org.bouncycastle.crypto.*;
        import org.bouncycastle.crypto.engines.*;
        import org.bouncycastle.crypto.modes.*;
        import org.bouncycastle.crypto.params.*;
    
        // A simple example that uses the Bouncy Castle
        // lightweight cryptography API to perform DES
        // encryption of arbitrary data.
    
    
         public class Encryptor {
    
                private BufferedBlockCipher cipher;
                private KeyParameter key;
    
    
                // Initialize the cryptographic engine.
                // The key array should be at least 8 bytes long.
    
    
                public Encryptor( byte[] key ){
                /*
                cipher = new PaddedBlockCipher(
                           new CBCBlockCipher(new DESEngine()));
                */
                cipher = new PaddedBlockCipher(
                            new CBCBlockCipher(new BlowfishEngine()));
                this.key = new KeyParameter( key );
                }        
    
                // Initialize the cryptographic engine.
                // The string should be at least 8 chars long.
    
                public Encryptor( String key ){
                this( key.getBytes());
                }
                // Private routine that does the gritty work.
    
                private byte[] callCipher( byte[] data )
                throws CryptoException {
                int    size = cipher.getOutputSize( data.length );
    
                byte[] result = new byte[ size ];
                int    olen = cipher.processBytes(data,0,data.length result, 0);
                       olen += cipher.doFinal( result, olen );
    
                if( olen < size ){
                    byte[] tmp = new byte[ olen ];
                    System.arraycopy(
                            result, 0, tmp, 0, olen );
                    result = tmp;
                }
    
                return result;
            }
            // Encrypt arbitrary byte array, returning the
            // encrypted data in a different byte array.
    
            public synchronized byte[] encrypt( byte[] data )
            throws CryptoException {
                if( data == null || data.length == 0 ){
                    return new byte[0];
                }
    
                cipher.init( true, key );
                return callCipher( data );
            }
           // Encrypts a string.
    
            public byte[] encryptString( String data )
            throws CryptoException {
                if( data == null || data.length() == 0 ){
                    return new byte[0];
                }
    
                return encrypt( data.getBytes() );
            }
            // Decrypts arbitrary data.
    
            public synchronized byte[] decrypt( byte[] data )
            throws CryptoException {
                if( data == null || data.length == 0 ){
                    return new byte[0];
                }
    
                cipher.init( false, key );
                return callCipher( data );
            }
            // Decrypts a string that was previously encoded
            // using encryptString.
    
            public String decryptString( byte[] data )
            throws CryptoException {
                if( data == null || data.length == 0 ){
                    return "";
                }
    
                return new String( decrypt( data ) );
            }
        }
    
    0 讨论(0)
  • 2020-11-30 02:09

    you can use these function to encrypt and decrypt simple text

    //Encrypt simple text
    public String EncryptSimpleText (String text2Encrypt) throws Exception {
        byte[] encryptArray = Base64.getEncoder().encode(text2Encrypt.getBytes());
        return new String(encryptArray,"UTF-8");
    }
    
    //Decrypt simple text
    public String Decrypt2SimpleText(String textEncrypted) throws Exception {
        byte[] dectryptArray = textEncrypted.getBytes();
        byte[] decarray = Base64.getDecoder().decode(dectryptArray);
        return new String(decarray,"UTF-8");
    }
    
    0 讨论(0)
提交回复
热议问题