Rsa加密的Java测试代码大全
/*******************************************************************************
* $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());
}
}
本文地址:http://www.45fan.com/bcdm/69605.html