Rsa加密的Java測試代碼

來源:互聯網
上載者:User

/*******************************************************************************
 * $Header$
 * $Revision$
 * $Date$
 *
 *==============================================================================
 *
 * Copyright (c) 2001-2004 .
 * All rights reserved.
 *
 * Created on 2004-11-15
 *******************************************************************************/

import java.math.BigInteger;

/**
 * TODO此處填寫 class 資訊
 *
 * @author Yanfei 
 */
/*
 * 修改曆史
 * $Log$
 */
public class RsaAlgorithm {
  BigInteger modulus;
  BigInteger publicKey;
  BigInteger privateKey;

  public RsaAlgorithm() {
      // generation prime number by RSATool
      // firstPrime = 8531092198849658733790731600554882570012152789399913498991432728465552799732061900314704077553345030066920054082501510392030827356194179550613533833120011
      // secondPrime = 11924164233834228789553347947559934849185846410886263275018134490056496194726414252409563040573266992224048791555973043496961395481558244111428543221720059
      modulus = new BigInteger("101726144473065307136337870615470919343156117442809797759084430536209670790991552510812754477461878742757991641136682979130702060350988957821490188383976777417165191105548667246571012637458521780431579809518401391017216215682790968984090555280612303529196758248073861960100967926654874945078177551837993000649");
      publicKey = new BigInteger("777777771");
     
      // privateKey = (X(firstPrime-1)(secondPrime-1) + 1)/publicKey   x is an integer.
      privateKey = new BigInteger("90174759107251056922810500448184130736176114745350210515099709962555046427810408233459605518228418589756487520725572479732405052968217872244415294457554090608096384694464961513226211645941929392277330122178360116753803241577778113257146900485635077056441559542883250804009040964580876711072978171708583360391");
  }

  public String encrypt(String str) {
      BigInteger t = new BigInteger(str);
      t = t.modPow(publicKey, modulus);
      System.out.println("encrypt str is : " + t.toString());
      return t.toString();
  }

  public String decrypt(String str) {
      BigInteger C = new BigInteger(str);
      C = C.modPow(privateKey, modulus);
      System.out.println("decrypt str is : " + C.toString());
      return C.toString();
  }

  public static void main(String[] args) {
      RsaAlgorithm rsa = new RsaAlgorithm();
      String encryptStr = rsa.encrypt("1234343");
      rsa.decrypt(encryptStr.toString());
  }
}

 

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.