Proyectos de Subversion Android Microlearning

Rev

Autoría | Ultima modificación | Ver Log |

package com.cesams.twogetskills.library;
import java.util.*;

public abstract class RSA {
        private static long  mo(long g, long l) 
        {
                long result = g - (l * (long) Math.floor(g/l));
                return result; 
        }       
        
        private static long powmod(long base, long exp, long modulus) {
                 long accum = 1;
                 int i = 0;
                 long basepow2 = base;
                 while ((exp >> i)>0) 
                 {
                         if (((exp >> i) & 1) == 1) 
                         {
                                 accum = mo((accum * basepow2) , modulus);
                         }
                         basepow2 = mo((basepow2 * basepow2) , modulus);
                         i++;
                 }
                 return accum;
        }       
        
        public static String decode(String c, long d,  long n) 
        {
                 String[] decryptarray = c.split(" ");
                 
                 //Each number is then decrypted using the RSA formula: block ^D mod N
                 StringBuffer deencrypt = new StringBuffer();
                 for (int u=0; u < decryptarray.length; u++) 
                        {
                                if(decryptarray[u].equals("")) {
                                        continue;
                                }
                                String resultmod = String.valueOf(powmod(Long.valueOf(decryptarray[u]), d, n));
                                deencrypt.append(resultmod.substring(1, resultmod.length() - 1));
                 }
                 String s = deencrypt.toString();
                 
                 StringBuffer resultd = new StringBuffer();
                 for (int u=0;  u< s.length() ; u+=2) 
                 {
                         String num = s.substring(u, u + 2);
                         int i = Integer.valueOf(num) + 30; 
                     char ch  = (char) i;  
                                    
                         resultd.append(ch);  
                           
                 }
                 return resultd.toString();
        }       
        
  public static String encode (String m, long e, long n) {
        Vector<String> array = new Vector<String>();
        
         for (int i=0;  i<m.length(); i+=3) {
           String tmpasci="1";
           for (int h=0;  h<3; h++) {
                   String tmpstr = "";
                   if (i+ h < m.length()) {
                           int aux = m.charAt(i + h);
                           tmpstr =  String.valueOf(aux - 30);
                           if (tmpstr.length() < 2) {
                                   tmpstr = '0' + tmpstr;
                           }
                   } else {
                           break;
                   }
                   tmpasci = tmpasci + tmpstr;
          }
           tmpasci = tmpasci +  '1';
          array.add(tmpasci);
         }
         StringBuffer coded = new StringBuffer(); 
         for (int k=0; k<array.size(); k++) {
                 long resultmod = powmod(Long.valueOf(array.elementAt(k)), e, n);
                 coded.append( String.valueOf(resultmod));
                 coded.append(" ");
         }
         return coded.toString();
        }
}