2012-11-25 4 views
6

में आरएसए एन्क्रिप्शन मैं एक प्रोग्राम लिख रहा हूं जो एंड्रॉइड में आरएसए को नियोजित करता है। मैं RSA कुंजियों हो रही है:एंड्रॉइड

String test ="test"; 
byte[] testbytes = test.getBytes(); 
Cipher cipher = Cipher.getInstance("RSA"); 
cipher.init(Cipher.ENCRYPT_MODE, publicKey); 
byte[] cipherData = cipher.doFinal(testbytes); 
String s = new String(cipherData); 
Log.d("testbytes after encryption",s); 

डिक्रिप्शन समारोह में मैं पाने के लिए वापस डेटा decrypting कर रहा हूँ: एक परीक्षण स्ट्रिंग एन्क्रिप्ट करने के लिए एन्क्रिप्शन समारोह का उपयोग करना

KeyPair kp = kpg.genKeyPair(); 
publicKey = kp.getPublic(); 
privateKey = kp.getPrivate(); 

मैं निम्नलिखित समस्या है मूल स्ट्रिंग

Cipher cipher2 = Cipher.getInstance("RSA"); 
cipher2.init(Cipher.DECRYPT_MODE, privateKey); 
byte[] plainData = cipher.doFinal(cipherData); 
String p = new String(plainData); 
Log.d("decrypted data is:",p); 

लॉग में मुद्रित 'पी' में डेटा मूल स्ट्रिंग "परीक्षण" से मेल नहीं खाता है। मैं इसमें कहां गलत हो रहा हूँ?

+0

खैर क्या लॉग में मुद्रित किया जाता है? यदि आपके पास मिस्चैचिंग कुंजियां या गड़बड़ी वाले सिफर थे तो आपको एक अपवाद मिलेगा, गलत जवाब नहीं। –

+0

यह भी ध्यान दें कि 'सिफरडाटा' एक यादृच्छिक-जैसी बाइनरी स्ट्रिंग होगी, इसलिए कच्चे बाइट्स ('स्ट्रिंग एस = न्यू स्ट्रिंग (सिफरडाटा) का उपयोग करके इसे स्ट्रिंग में परिवर्तित करना;') आपको अजीब परिणाम दे सकता है। –

उत्तर

5

यहाँ यह, लेकिन व्यवहार में कैसे करना है पर एक उदाहरण है,

आप वास्तव में एन्क्रिप्ट नहीं कर सकते हैं और सिर्फ आरएसए के साथ सारी फ़ाइलें डिक्रिप्ट। आरएसए एल्गोरिदम केवल एक ब्लॉक को एन्क्रिप्ट कर सकता है, और यह पूरी फ़ाइल करने के लिए धीमा है।
आप 3 डीईएस या एईएस का उपयोग कर फ़ाइल एन्क्रिप्ट कर सकते हैं, और उसके बाद इच्छित प्राप्तकर्ता के आरएसए सार्वजनिक कुंजी का उपयोग करके एईएस कुंजी एन्क्रिप्ट कर सकते हैं।

कुछ कोड:

public static void main(String[] args) throws Exception { 
    KeyPairGenerator kpg = KeyPairGenerator.getInstance("RSA"); 
    Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding"); 

    kpg.initialize(1024); 
    KeyPair keyPair = kpg.generateKeyPair(); 
    PrivateKey privKey = keyPair.getPrivate(); 
    PublicKey pubKey = keyPair.getPublic(); 

    // Encrypt 
    cipher.init(Cipher.ENCRYPT_MODE, pubKey); 

    String test = "My test string"; 
    String ciphertextFile = "ciphertextRSA.txt"; 
    InputStream fis = new ByteArrayInputStream(test.getBytes("UTF-8")); 

    FileOutputStream fos = new FileOutputStream(ciphertextFile); 
    CipherOutputStream cos = new CipherOutputStream(fos, cipher); 

    byte[] block = new byte[32]; 
    int i; 
    while ((i = fis.read(block)) != -1) { 
     cos.write(block, 0, i); 
    } 
    cos.close(); 

    // Decrypt 
    String cleartextAgainFile = "cleartextAgainRSA.txt"; 

    cipher.init(Cipher.DECRYPT_MODE, privKey); 

    fis = new FileInputStream(ciphertextFile); 
    CipherInputStream cis = new CipherInputStream(fis, cipher); 
    fos = new FileOutputStream(cleartextAgainFile); 

    while ((i = cis.read(block)) != -1) { 
     fos.write(block, 0, i); 
    } 
    fos.close(); 
} 
+0

आपने उल्लेख किया है 'आप 3DES या AES का उपयोग करके फ़ाइल को एन्क्रिप्ट कर सकते हैं, और उसके बाद इच्छित प्राप्तकर्ता की आरएसए सार्वजनिक कुंजी का उपयोग करके एईएस कुंजी एन्क्रिप्ट कर सकते हैं।' मुझे वीडियो फ़ाइलों को एन्क्रिप्ट और डिक्रिप्ट करने के लिए बिल्कुल वांछित की आवश्यकता है। क्या आप इसके साथ मेरी मदद कर सकते हैं ... कुछ नमूना उपयोगी होंगे – Vishnu

+0

@nish मुझे एक ही आवश्यकता है, क्या आप ऐसा करने में सक्षम हैं? – Siddharth