class MyInt
{
    int p; // the length of a number
    boolean b[]; // the array storing bits of a number, in the reversed order
    
    MyInt(int p0){
    p = p0;
    b = new boolean[2*p]; // for the purposes of multiplication, we reserve more space
    } void populate(int n) {
// makes the content of this a representation of an integer number n
for(int i = 0; i<this.b.length; i++) {
b[i] = (n%2==1);
n = n/2;
}
}
    
    MyInt add (MyInt n){ // add this number to a number n and return as a new object
    MyInt result= new MyInt(p);
    // copy this to result
for(int i = 0; i<this.b.length; i++) {
result.b[i] = this.b[i];
}
    for(int i=0;i<p; i++){ 
     // add the i-th bit of n to the i-th bit of result
     if(!n.b[i]) continue; // if the bit in n is 0, no action is needed
     int j = i;
     while(result.b[j]) { // carry over if necessary
     result.b[j] = false;
     j++;
     }
     result.b[j] = true;
    }
    return result;
}

int compare(MyInt n) {
// this method compares this with another MyInt n
// if this<n then returns -1
// if this=n then returns 0
// if this>n then returns 1
for(int i = this.b.length-1; i>=0; i--) {
if(this.b[i] && !n.b[i]) return 1;
if(!this.b[i] && n.b[i]) return -1;
}
return 0;
}

    MyInt subtract (MyInt n){ // subtracts from this number a number n and return as a new object
     // only subtracts if this >= n; otherwise, returns null
     if(this.compare(n) == -1) return null;
    MyInt result= new MyInt(p);
    // copy this to result
for(int i = 0; i<this.b.length; i++) {
result.b[i] = this.b[i];
}
    for(int i=0;i<p; i++){ 
     // subtract the i-th bit of n from the i-th bit of result
     if(!n.b[i]) continue; // if the bit in n is 0, no action is needed
     int j = i;
     while(!result.b[j]) { // carry over if necessary
     result.b[j] = true;
     j++;
     }
     result.b[j] = false;
    }
    return result;
}

void multiplyBy2() { // multiplies this number by 2 by shifting bits
for(int i = this.b.length-1; i>0; i--) {
b[i] = b[i-1];
}
b[0] = false;
}

    MyInt multiply (MyInt n){ // multiplies this number to a number n and return as a new object
    MyInt result= new MyInt(p);
    MyInt copyOfN = new MyInt(p);
    // copy n to copyOfN
for(int i = 0; i<this.b.length; i++) {
copyOfN.b[i] = n.b[i];
}
    for(int i=0;i<p; i++){ 
     // if the i-th bit of this is 1, add n multiplied by 2 raised to the power i
     if(this.b[i]) { 
result = result.add(copyOfN);
     }
     copyOfN.multiplyBy2();
    }
    return result;
}
    
           public String toString() { // convert the number to a string, in binary representation
String s = "";
for(int i = 0; i<this.b.length; i++) {
s = (this.b[i]?"1":0) + s;
}
return s;
}

} // end class MyInt
public class Coursework2 {
        
    public static void main(String[] args) {
     // example of addition 
     MyInt m = new MyInt(5);
     MyInt n = new MyInt(5);
m.populate(0);    
n.populate(1);
     for(int i = 0; i < 50; i++) {
     System.out.println(m.toString());
     m = m.add(n);
     }
     System.out.println();
     // example of subtraction
     for(int i = 0; i < 50; i++) {
     System.out.println(m.toString());
     m = m.subtract(n);
     }
     System.out.println();
     // example of comparing integers
     m = new MyInt(5);
     n = new MyInt(5);
m.populate(10);    
n.populate(9);
System.out.println(m.toString());
System.out.println(n.toString());
System.out.println(m.compare(n));
System.out.println(n.compare(m));
System.out.println(m.compare(m));
     System.out.println();
     // example of multiplying integers
     m = new MyInt(5);
     n = new MyInt(5);
m.populate(3);    
n.populate(11);
System.out.println(m.multiply(n));
    }
}求助写一段根据 以上代码的  大数取模运算 和 大指数运算  谢谢 各位高手了

解决方案 »

  1.   

    网站:http://www.bouncycastle.org/latest_releases.html
    使用Bouncy Castle这个来进行rsa加,解密。
      

  2.   

    只做过DES,没有碰过这东东~~~但是,见过一些书上有针对的算法来降低复杂度的
      

  3.   

    记得JAVA里面有BigInteger类的啊?我没有做过网页处理,不太清楚web代码是否能调用。