Mathematical foundation of information security-Modular repeated square calculation method (two methods to realize C+JAVA)

Preface

I am studying the course of Information Security Mathematical Foundation.
Program to realize modular repeat square calculation method.


First introduce the modular repeat square algorithm

1. Modular repeat square algorithm

The picture is from the video of the university MOOC teacher Chen Gongliang, which is slightly blurred. . .
Insert picture description here

Insert picture description here

Insert picture description here
Insert picture description here
Insert picture description here
Insert picture description hereBelow we use C language to implement this algorithm. We can think about it, how many small parts can this algorithm be divided into?
First of all, we need a function to convert decimal to binary, which is mainly used to convert exponents into binary form;
we also need to know how many times our loop calculation needs to be executed, so we need a function to determine the number of bits of the exponent; the
last one The function is our modular repeat square calculation function. Of course, we can also define a printing function.

Not much nonsense, code! ! !

Code!

#include<stdio.h>
#include<math.h>
int O_Binary(int n,int a[])//十进制转化为二进制函数 
{
    
     int i,j,m; 
 for(m=0;m<15;m++)
 {
    
     i=n%2;
  j=n/2;
  n=j;
  a[m]=i;
 }
 for(m=15;m>=0;m--)//将数组倒序输出 
 {
    
     printf("%d",a[m]);
  if(m%4==0)//每隔四个数字打印一个空格 
   printf(" ");
 }
 printf("\n");
 return 0;
}
int judge_O_bit(int n)//表达十进制所需的位数 
{
    
     int i=1;
 while(i)
 {
    
     if(n<pow(2,i))
  {
    
     
   //printf("%d ",i);
   return i;
  }
  i++;
 }
}
int print(int a[])
{
    
     int i;
 printf("将二进制存到数组中:\n\t");
 for(i=0;i<=15;i++)
 {
    
     printf("%d",a[i]);
  if((i+1)%4==0)//每隔四个数字打印一个空格 
   printf(" ");
 }
 return 0;
} 
int jisuan_Mod(int zhi,int di,int mo,int bit,int a[])//zhi 指数,di底数,mo模几,bit指数的二进制的位数  
{
    
     int x,y,j;          //a[]存储指数的二进制,a[0]= 1 。。。。 
 printf("开始输出结果!-->>\n");
 y=di;
 for(j=0;j<bit;j++)
 {
    
     if(j==0)//第一次时 
  {
    
     x=di%mo;
   y=(y*y)%mo;
   printf("%d. n%d = %d\n",j+1,j,a[j]);
   printf("\ta%d ≡%d, b%d ≡%d(mod %d)\n",j,x,j+1,y,mo);
   continue;
  }
  if(a[j]==0&&j!=bit-1&&j!=0)//二进制为0 且不是最后一次 
  {
    
     x=x;//x不变,依旧为上一个x
   y=(y*y)%mo;
   printf("%d. n%d = %d\n",j+1,j,a[j]);
   printf("\ta%d ≡%d, b%d ≡%d(mod %d)\n",j,x,j+1,y,mo);
   continue;
  }
  else//二进制为1
  {
    
     x=(x*y)%mo; 
   if(j!=bit-1)
   {
    
     y=(y*y)%mo;//最后一次时不再算y,直接输出x即可
    printf("%d. n%d = %d\n",j+1,j,a[j]);
    printf("\ta%d ≡%d, b%d ≡%d(mod %d)\n",j,x,j+1,y,mo);continue;
   }
   printf("%d. n%d = %d\n",j+1,j,a[j]);
   printf("\ta%d ≡%d(mod %d)\n",j,x,mo);
   continue;
  }
 }
 printf("最后,计算出 %d^%d ≡%d(mod %d)",di,zhi,x,mo);
 return x;
}
int main()
{
    
     int zhi,di,bit,i,yu,mo;
 int a[16]={
    
    0};
 printf("\t模重复平方计算\n");
 printf("请输入指数——十进制数字(0~32767):");
 scanf("%d",&zhi);
 printf("请输入底数:");
 scanf("%d",&di);
 printf("请输入模数:");
 scanf("%d",&mo);//13的二进制位 1101 
 printf("指数转化为二进制:\n\t");
 O_Binary(zhi,a); //数组a存放指数的二进制,a[0]= 1 
 bit=judge_O_bit(zhi);
 print(a);
 printf("\n\n\n"); 
 jisuan_Mod(zhi,di,mo,judge_O_bit(zhi),a); 
 return 0;
}

The first exercise, success. Insert picture description here
The second exercise is successful.Insert picture description here
Next, we will use JAVA to implement the second calculation method (this is what our teacher said, which is easier to understand)
First of all, the exponent is converted into binary form. It should be noted that, for the first initialization, multiply the base by 1. When the high bit of the binary is 1, the base is squared and then multiplied by the base. When the element is 0, just square it.
Let's take an example, as shown in the figure below.
Insert picture description here

On the JAVA code!

import java.util.Scanner;
/*2020/10/31 
 *模重复平方算法(罡罡同学)
 */
public class Demo3{
    
    
 public static void main(String[] args){
    
    
  Scanner pi = new Scanner(System.in);
  System.out.print("\t模重复平方法(罡罡同学)\n请输入指数:");
  int zhi=pi.nextInt();
  System.out.print("请输入底数:");
  int di=pi.nextInt();
  System.out.print("请输入模数:");
  int mo=pi.nextInt();
  String bin1 = Integer.toBinaryString(zhi);//4位
  System.out.println("指数转化为二进制: "+bin1);
  System.out.printf("最后结果为: %d^%d ≡"+count_mod(bin1,di,mo)+"(mod%d)",di,zhi,mo);
 }
 public static int count_mod(String s,int di,int mo)
 {
    
     int result=0;
  for(int i=0;i<s.length();i++)//s.length 可以替代参数zhi
  {
    
     if(i==0)
   {
    
    
    result=(di*1)%mo;continue;
   }
   if(s.charAt(i)=='1')
   {
    
    
    result=(result*result)*di%mo;
   }
   else
   {
    
    
    result=(result*result)%mo;
   }
  }
  return result;
 }
}

Screenshot of program execution result:
Insert picture description here
Personally think that the second method is simpler, and you can directly call many functions that have been defined by JAVA! ! !
Remember to like it! Thank you!

Guess you like

Origin blog.csdn.net/m0_46625346/article/details/109393558
Recommended