package sketch.avengergear.com;

import android.content.Context;
import android.telephony.TelephonyManager;
import android.util.Log;
import android.widget.TextView;

import org.spongycastle.jce.provider.BouncyCastleProvider;

import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.security.Security;
import java.security.SecureRandom;
import java.security.NoSuchAlgorithmException;
import java.security.NoSuchProviderException;
import java.security.InvalidKeyException;
import javax.crypto.Cipher;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.BadPaddingException;
import java.util.Arrays;
import java.util.Map;
import java.util.UUID;

public class CipherHelper
{
    private static final String TAG = "SKETCH-CIPHERHELPER";

    static {
        Security.insertProviderAt(new org.spongycastle.jce.provider.BouncyCastleProvider(), 1);
    }

    Cipher rsa_encoder_chiper = null;
    Cipher rsa_decoder_chiper = null;

    public CipherHelper( PrivateKey private_key, PublicKey public_key )
    { 
        try { 
            rsa_encoder_chiper = Cipher.getInstance("RSA/None/PKCS1Padding", "SC");
            rsa_decoder_chiper = Cipher.getInstance("RSA/None/PKCS1Padding", "SC");

            rsa_encoder_chiper.init(Cipher.ENCRYPT_MODE, public_key );
            rsa_decoder_chiper.init(Cipher.DECRYPT_MODE, private_key );
        } catch (InvalidKeyException e) {
            Log.e(TAG, "InvalidKey exception", e);
        } catch (NoSuchPaddingException e) {
            Log.e(TAG, "No Padding exception", e);
        } catch (NoSuchProviderException e) {
            Log.e(TAG, "No Provider exception", e);
        } catch (NoSuchAlgorithmException e ){
            Log.e(TAG, "No Algorithum exception", e);
        } 
    }

    public byte[] encodeBytes(byte[] info)
    {
        byte[] answer = null;
        try {
            answer = rsa_encoder_chiper.doFinal(info);
        } catch (BadPaddingException e) {
            Log.e(TAG, "Bad Padding exception", e);
        } catch (IllegalBlockSizeException e) { 
            Log.e(TAG, "Illegal Block Size exception", e);
        }
        return answer;
    }

    public byte[] decodeBytes(byte[] info)
    {
        byte[] answer = null;
        try {
            answer = rsa_decoder_chiper.doFinal(info);
        } catch (BadPaddingException e) {
            Log.e(TAG, "Bad Padding exception", e);
        } catch (IllegalBlockSizeException e) { 
            Log.e(TAG, "Illegal Block Size exception", e);
        }
        return answer;
    }
    
}


@@ -1118,27 +1118,27 @@
 sa_encoder_c
-h
 ip
+h
 er = null;%0A 
@@ -1156,27 +1156,27 @@
 sa_decoder_c
-h
 ip
+h
 er = null;%0A%0A
@@ -1290,27 +1290,27 @@
 sa_encoder_c
-h
 ip
+h
 er = Cipher.
@@ -1378,19 +1378,19 @@
 ecoder_c
-h
 ip
+h
 er = Cip
@@ -1459,27 +1459,27 @@
 sa_encoder_c
-h
 ip
+h
 er.init(Ciph
@@ -1534,19 +1534,19 @@
 ecoder_c
-h
 ip
+h
 er.init(
@@ -1666,32 +1666,133 @@
 exception%22, e);%0A
+        %7D catch (NoSuchAlgorithmException e )%7B%0A            Log.e(TAG, %22No Algorithum exception%22, e);%0A
         %7D catch 
@@ -1983,109 +1983,985 @@
   %7D 
-catch (NoSuchAlgorithmException e )%7B%0A            Log.e(TAG, %22No Algorithum exception%22, e);%0A        %7D 
+%0A    %7D%0A%0A    public byte%5B%5D encodeBytesWithPublicKey(byte%5B%5D info, PublicKey public_key)%0A    %7B%0A        byte%5B%5D answer = null;%0A        try %7B%0A            Cipher temp_encoder_cipher = Cipher.getInstance(%22RSA/None/PKCS1Padding%22, %22SC%22);%0A            temp_encoder_cipher.init(Cipher.ENCRYPT_MODE, public_key );%0A            answer = rsa_encoder_cipher.doFinal(info);%0A        %7D catch (BadPaddingException e) %7B%0A            Log.e(TAG, %22Bad Padding exception%22, e);%0A        %7D catch (IllegalBlockSizeException e) %7B %0A            Log.e(TAG, %22Illegal Block Size exception%22, e);%0A        %7D catch (InvalidKeyException e) %7B%0A            Log.e(TAG, %22InvalidKey exception%22, e);%0A        %7D catch (NoSuchAlgorithmException e )%7B%0A            Log.e(TAG, %22No Algorithum exception%22, e);%0A        %7D catch (NoSuchPaddingException e) %7B%0A            Log.e(TAG, %22No Padding exception%22, e);%0A        %7D catch (NoSuchProviderException e) %7B%0A            Log.e(TAG, %22No Provider exception%22, e);%0A        %7D%0A        return answer;
 %0A   
@@ -3087,27 +3087,27 @@
 sa_encoder_c
-h
 ip
+h
 er.doFinal(i
@@ -3485,11 +3485,11 @@
 er_c
-h
 ip
+h
 er.d

Fix typo add Encoder with others public key