2012-02-29 11 views
10

मैं एक वीडियो फ़ाइल एन्क्रिप्ट करने के लिए इस विधि का उपयोग कर रहा हूँ:वीडियो फ़ाइलों का एन्क्रिप्शन?

public static void encryptToBinaryFile(String password, byte[] bytes, File file) throws EncrypterException { 
    try { 
     final byte[] rawKey = getRawKey(password.getBytes()); 
     final FileOutputStream ostream = new FileOutputStream(file, false); 

     ostream.write(encrypt(rawKey, bytes)); 
     ostream.flush(); 
     ostream.close(); 

    } catch (IOException e) { 
     throw new EncrypterException(e); 
    } 
} 

private static byte[] encrypt(byte[] raw, byte[] clear) throws EncrypterException { 
    try { 
     final SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES"); 
     final Cipher cipher = Cipher.getInstance("AES"); 
     cipher.init(Cipher.ENCRYPT_MODE, skeySpec); 

     return cipher.doFinal(clear); 

    } catch (Exception e) { 
     throw new EncrypterException(e); 
    } 
} 

लेकिन यह 301,023,321 तत्व के आवंटन को खारिज करते हुए कहा OutOfMemoryError एक त्रुटि देता है।

1. क्या विधि है कि मैं ऐसी बड़ी फ़ाइलों के लिए सही उपयोग कर रहा हूं?

2. अगर हाँ मुझे यह त्रुटि क्यों मिल रही है? समाधान क्या है?

3. अगर कोई इसके लिए कुछ अच्छी विधि का सुझाव नहीं देता है?

+0

डोफिनल के बजाय, मैं अपने शुरुआती बाइट [] (प्रत्येक बार कुछ Kb) के साथ अपडेट का उपयोग करता हूं और उन्हें फ़ाइल में लिखता हूं। – njzk2

+0

या शायद 2 बाइटबफर आइटम – njzk2

+0

के साथ doFinal विधि का उपयोग करें आप इसे कैसे डिक्रिप्ट करने जा रहे हैं? आप इसे वीडियो में एन्क्रिप्ट करके इसे जटिल बना रहे हैं ... –

उत्तर

21

CipherInputStream और CipherOutputStream आपको आसानी से ऐसा करने में मदद करेगा। मैंने एक वीडियो फ़ाइल एन्क्रिप्ट और डिक्रिप्ट करने के लिए एक नमूना कार्यक्रम लिखा था। अपनी पसंद के अनुसार यह संशोधित करें ...

FileInputStream fis = new FileInputStream(new File("D:/Shashank/inputVideo.avi")); 
     File outfile = new File("D:/Shashank/encVideo.avi"); 
     int read; 
     if(!outfile.exists()) 
      outfile.createNewFile(); 
     File decfile = new File("D:/Shashank/decVideo.avi"); 
     if(!decfile.exists()) 
      decfile.createNewFile(); 
     FileOutputStream fos = new FileOutputStream(outfile); 
     FileInputStream encfis = new FileInputStream(outfile); 
     FileOutputStream decfos = new FileOutputStream(decfile); 
     Cipher encipher = Cipher.getInstance("AES"); 
     Cipher decipher = Cipher.getInstance("AES"); 
     KeyGenerator kgen = KeyGenerator.getInstance("AES"); 
     //byte key[] = {0x00,0x32,0x22,0x11,0x00,0x00,0x00,0x00,0x00,0x23,0x00,0x00,0x00,0x00,0x00,0x00,0x00}; 
     SecretKey skey = kgen.generateKey(); 
     //Lgo 
     encipher.init(Cipher.ENCRYPT_MODE, skey); 
     CipherInputStream cis = new CipherInputStream(fis, encipher); 
     decipher.init(Cipher.DECRYPT_MODE, skey); 
     CipherOutputStream cos = new CipherOutputStream(decfos,decipher); 
     while((read = cis.read())!=-1) 
       { 
        fos.write((char)read); 
        fos.flush(); 
       } 
     fos.close(); 
     while((read=encfis.read())!=-1) 
     { 
      cos.write(read); 
      cos.flush(); 
     } 
    cos.close(); 

मैं generateKey() उपयोग कर एक नया कुंजी पैदा कर रहा हूँ। आप जावा SecureRandom का उपयोग कर Random IV generation साथ भी एक बाइट सरणी का उपयोग कर सकते हैं, अपनी खुद की कुंजी उत्पन्न करने के ....

+0

के साथ मदद कर सकते हैं अच्छा लग रहा है बस मुझे आज़माएं !! – Navdroid

+0

काम कर रहा है लेकिन बहुत समय ले रहा है! – Navdroid

+0

को गति देने के लिए कोई सुझाव यह बहुत अधिक मात्रा में नहीं लेना चाहिए .... मुझे लगता है कि आपकी फाइल बहुत बड़ी है .... यह कब तक है ?? –

7

Encryption Major Mistakes

Tonyz Java AES Encryptor

फास्ट एन्क्रिप्शन/डिक्रिप्शन

import java.io.File; 
import java.io.FileInputStream; 
import java.io.FileOutputStream; 
import java.io.IOException; 
import java.io.InputStream; 
import java.io.OutputStream; 
import java.security.InvalidAlgorithmParameterException; 
import java.security.InvalidKeyException; 
import java.security.NoSuchAlgorithmException; 
import java.security.SecureRandom; 
import java.security.spec.AlgorithmParameterSpec; 

import javax.crypto.Cipher; 
import javax.crypto.CipherOutputStream; 
import javax.crypto.KeyGenerator; 
import javax.crypto.NoSuchPaddingException; 
import javax.crypto.SecretKey; 
import javax.crypto.spec.IvParameterSpec; 
import javax.crypto.spec.SecretKeySpec; 

public class Encrypter { 
    private final static int IV_LENGTH = 16; // Default length with Default 128 
               // key AES encryption 
    private final static int DEFAULT_READ_WRITE_BLOCK_BUFFER_SIZE = 1024; 

    private final static String ALGO_RANDOM_NUM_GENERATOR = "SHA1PRNG"; 
    private final static String ALGO_SECRET_KEY_GENERATOR = "AES"; 
    private final static String ALGO_VIDEO_ENCRYPTOR = "AES/CBC/PKCS5Padding"; 

    @SuppressWarnings("resource") 
    public static void encrypt(SecretKey key, /*AlgorithmParameterSpec paramSpec,*/ InputStream in, OutputStream out) 
      throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, 
      InvalidAlgorithmParameterException, IOException { 
     try { 
      // byte[] iv = new byte[] { (byte) 0x8E, 0x12, 0x39, (byte) 0x9C, 
      // 0x07, 0x72, 0x6F, 0x5A, (byte) 0x8E, 0x12, 0x39, (byte) 0x9C, 
      // 0x07, 0x72, 0x6F, 0x5A }; 
      //generate new AlgorithmParameterSpec 
      // AlgorithmParameterSpec paramSpec = new IvParameterSpec(iv); 
      Cipher c = Cipher.getInstance(ALGO_VIDEO_ENCRYPTOR); 
      c.init(Cipher.ENCRYPT_MODE, key, paramSpec); 
      out = new CipherOutputStream(out, c); 
      int count = 0; 
      byte[] buffer = new byte[DEFAULT_READ_WRITE_BLOCK_BUFFER_SIZE]; 
      while ((count = in.read(buffer)) >= 0) { 
       out.write(buffer, 0, count); 
      } 
     } finally { 
      out.close(); 
     } 
    } 

    @SuppressWarnings("resource") 
    public static void decrypt(SecretKey key, /*AlgorithmParameterSpec paramSpec,*/ InputStream in, OutputStream out) 
      throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, 
      InvalidAlgorithmParameterException, IOException { 
     try { 
      // byte[] iv = new byte[] { (byte) 0x8E, 0x12, 0x39, (byte) 0x9C, 
      // 0x07, 0x72, 0x6F, 0x5A, (byte) 0x8E, 0x12, 0x39, (byte) 0x9C, 
      // 0x07, 0x72, 0x6F, 0x5A }; 
      // read from input stream AlgorithmParameterSpec 
      // AlgorithmParameterSpec paramSpec = new IvParameterSpec(iv); 
      Cipher c = Cipher.getInstance(ALGO_VIDEO_ENCRYPTOR); 
      c.init(Cipher.DECRYPT_MODE, key, paramSpec); 
      out = new CipherOutputStream(out, c); 
      int count = 0; 
      byte[] buffer = new byte[DEFAULT_READ_WRITE_BLOCK_BUFFER_SIZE]; 
      while ((count = in.read(buffer)) >= 0) { 
       out.write(buffer, 0, count); 
      } 
     } finally { 
      out.close(); 
     } 
    } 

    public static void main(String[] args) { 
     File inFile = new File("C:/enc_test/video.swf"); 
     File outFile = new File("C:/enc_test/enc_video.swf"); 
     File outFile_dec = new File("C:/enc_test/dec_video.swf"); 

     try { 
      SecretKey key = KeyGenerator.getInstance(ALGO_SECRET_KEY_GENERATOR).generateKey(); 

      byte[] keyData = key.getEncoded(); 
      SecretKey key2 = new SecretKeySpec(keyData, 0, keyData.length, ALGO_SECRET_KEY_GENERATOR); //if you want to store key bytes to db so its just how to //recreate back key from bytes array 

      byte[] iv = new byte[IV_LENGTH]; 
      SecureRandom.getInstance(ALGO_RANDOM_NUM_GENERATOR).nextBytes(iv); // If 
                       // storing 
                       // separately 
      AlgorithmParameterSpec paramSpec = new IvParameterSpec(iv); 

      Encrypter.encrypt(key, paramSpec, new FileInputStream(inFile), new FileOutputStream(outFile)); 
      Encrypter.decrypt(key2, paramSpec, new FileInputStream(outFile), new FileOutputStream(outFile_dec)); 
     } catch (Exception e) { 
      e.printStackTrace(); 
     } 

    } 

} 

धन्यवाद सुझाव के लिए Naveem M kumarhttp://libeasy.alwaysdata.net/

+2

बहुत बढ़िया काम। बहुत बहुत धन्यवाद। कुछ कारणों से 'एईएस/सीबीसी/पीकेसीएस 5 पैडिंग' खुराक मेरे लिए काम नहीं करती है, इसे 'एईएस' में बदलती है ... –

+0

कभी-कभी राय कोड दिखाने के लिए बेहतर होता है, क्योंकि अधिकांश पाठकों को यह सही नहीं लगेगा। चतुर्थ रहस्य नहीं है, इसलिए आप इसे सिफरटेक्स्ट के साथ भेज सकते हैं। आमतौर पर, यह केवल सिफरटेक्स्ट के लिए तैयार किया जाता है और डिक्रिप्शन से पहले काटा जाता है। –

+0

@ArtjomB। मैंने कक्षा चर में IV_Length घोषित किया है, IV_LENGTH = 16. मुझे उम्मीद है कि मैंने आपके प्रश्न को सही ढंग से समझ लिया है –