java - How to do Encryption and Decryption of a File? -


i use cipheroutputstream encryption , decryption file in java, input file > 117 byte cannot encryption. use rsa algorithm public key lenght 1024 byte.

cipher.init(cipher.encrypt_mode, seckey);  string cleartextfile = "cleartext.txt"; string ciphertextfile = "ciphertextsymm.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(); 

how encryption input file length > 117 byte?

you cannot encrypt file using rsa because rsa (well, more precise, implementation of rsa in java) not let encrypt more data length of key. 1024 bits key, can encrypt 1024 bits 128 bytes (actually bit less padding reasons).

in cases, it bad practice encrypt large piece of data using public-key algorithm (asymmetric cryptography) 2 main reasons.

  1. the no practical, appropriate , secure cryptographic mode/padding encrypt large amounts of data using rsa (ie not secure that).

  2. public-key algorithms require large key secure (1024 bits, 2048 bits) , therefore slower symmetric-key algorithms (which require 128 256 bits keys secure).

if want more details on why should not use solely rsa encrypt large amounts of data, see these 2 great stacktexchange posts :

if want encrypt large amount of data, standard way proceed generate session key (a cryptographically secure random number used once). encrypt session key public key. encrypt file (the large amount of data) symmetric algorithm (such aes) using unencrypted session key. store encrypted session key , encrypted data altogether in final file. that's way pgp (or gnupg) proceeds when sends encrypted mail. ssl/tls works in similar way.

lastly, using cryptography complicated (pretty can create security flaw : encryption modes, padding, etc...) advise careful , make sure code going reviewed knowledgeable in crypto matters.

here piece of code shows general process :

// 1. generate session key keygenerator keygen = keygenerator.getinstance("aes"); keygen.init(128) secretkey sessionkey = keygen.generatekey();  // 2. encrypt session key rsa public key cipher rsacipher = cipher.getinstance("rsa"); rsacipher.init(cipher.encrypt_mode, rsapublickey) byte[] encryptedsessionkey = rsacipher.dofinal(sessionkey.getencoded());  // 3. encrypt data using session key (unencrypted) cipher aescipher = cipher.getinstance("aes/cbc/pkcs5padding"); aescipher.init(cipher.encrypt_mode, sessionkey); <-- sessionkey unencrypted //                                                   session key. // ... use aescipher encrypt data  // 4. save encrypted data along encrypted  // session key (encryptedsessionkey). // please note because of encryption mode (cbc), // need save iv (initialization vector). // aescipher.aescipher.getparameters(). //     getparametersspec(ivparameters.class).getiv(); 

Comments

Popular posts from this blog

jquery - How can I dynamically add a browser tab? -

node.js - Getting the socket id,user id pair of a logged in user(s) -

keyboard - C++ GetAsyncKeyState alternative -