Advertisement
Anton0093

Lab_6 (Sem_3)

Nov 3rd, 2021
997
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.83 KB | None | 0 0
  1. import java.io.UnsupportedEncodingException;
  2. import java.math.BigInteger;
  3.  
  4. public class BlindSignatureRSA {
  5.  
  6.     /*
  7.         A - ALICE
  8.         B - BOB
  9.         e - открытый ключ
  10.         d - закрытый ключ
  11.         R - случайное число
  12.         М - сообщение
  13.         B --> A     M' = M * R^e
  14.         A --> B     S' = ( M * R^e)^d = M^d * R^ed = M^d * R
  15.  
  16.         B:  S'/R = (M^d * R)/R = M^d
  17.  
  18.      */
  19.  
  20.     public static BigInteger blindHideMsg(BigInteger M, BigInteger R, BigInteger e, BigInteger n) {
  21.         // M' = M * R^e
  22.         BigInteger hideMsg = M.multiply(R.modPow(e, n)).mod(n);
  23.         return hideMsg;
  24.     }
  25.  
  26.     public static BigInteger blindSignature(BigInteger blindMsg, BigInteger d, BigInteger n) {
  27.         //  S' = ( M * R^e)^d = M^d * R^ed = M^d * R
  28.         BigInteger blindSig = blindMsg.modPow(d, n);
  29.         return blindSig;
  30.     }
  31.  
  32.     public static BigInteger blindRetrieveSig(BigInteger blindSig, BigInteger R, BigInteger n) {
  33.         // S'/R = (M^d * R)/R = M^d
  34.         BigInteger signature = blindSig.multiply(R.modInverse(n)).mod(n);
  35.         return signature;
  36.     }
  37.  
  38.     public static BigInteger stringToBigInteger(String str){
  39.         byte [] raw = new byte[0];
  40.         try {
  41.             raw = str.getBytes("UTF8");
  42.         } catch (UnsupportedEncodingException unsupportedEncodingException) {
  43.             unsupportedEncodingException.printStackTrace();
  44.         }
  45.         return new BigInteger(raw);
  46.     }
  47.  
  48.     public static void main(String[] args) {
  49.  
  50.         BigInteger e = new BigInteger("65537"); // открытый ключ
  51.         BigInteger d = new BigInteger("60530511629930072241945671200867889027539295362393257858025617298274206834041"); // закрытый
  52.         BigInteger n = new BigInteger("99418278299100976004220175767913358809660152882550697543248103911741071816073"); //mod n
  53.         BigInteger R = new BigInteger("10879736607308378083"); //случайное число
  54.  
  55.         String message = "i love FCS"; // сообщение
  56.         BigInteger M = stringToBigInteger(message); // форматируем нашу строку в BigInteger
  57.  
  58.         BigInteger blindMsg = blindHideMsg(M, R, e, n);  // получаем M'
  59.         BigInteger blindSig = blindSignature(blindMsg, d, n); // получаем S'
  60.         BigInteger sig = blindRetrieveSig(blindSig, R, n); // получаем S'/R
  61.         BigInteger realSig = M.modPow(d, n);  // реальная подпись (sig = realSig)
  62.  
  63.         System.out.println("Message                  = " + M);
  64.         System.out.println("Blind hide message       = " + blindMsg);
  65.         System.out.println("Blind signature          = " + blindSig);
  66.         System.out.println("Blind retrieve signature = " + sig);
  67.         System.out.println("Signature                = " + realSig);
  68.     }
  69.  
  70. }
  71.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement