第一个是大三时老师让实现的rsa加解密算法,这个程序是从网上找的,呵呵具体的忘了地址了,稍微改了改就变成作业了,呵呵,有点不好意思,今天翻出来想删了又舍不得就发上来吧
还有一个是用C#实现的快速排序算法,跟现在的论文有关
package seadn.common.serurity;
import java.math.BigInteger;
import java.security.SecureRandom;
public class RSA {
private final static BigInteger one = BigInteger.ONE;
private final static SecureRandom random = new SecureRandom();
private final static int defaultBitLength = 128;
private BigInteger privateKey = null;// 私钥
private BigInteger publicKey = null;// 公钥
private BigInteger modulus = null;// n
private BigInteger p1 = null; // 原始质数p
private BigInteger p2 = null; // 原始质数q
private BigInteger modulusE; // 实际模数n
private BigInteger publicKeyE; // 实际公钥
private int N;// 位数
private BigInteger phi0;
public RSA() {
this(defaultBitLength);
}
/**
*
* Created constructor
*
* @param N
*/
public RSA(int N) {
this.N = N;
// 生成N位公钥和私钥
p1 = BigInteger.probablePrime(N / 2, random);
p2 = BigInteger.probablePrime(N / 2, random);
phi0 = (p1.subtract(one)).multiply(p2.subtract(one));
modulus = p1.multiply(p2);
setPrivateKey();
publicKey = privateKey.modInverse(phi0);
modulusE = modulus;
publicKeyE = publicKey;
}
/**
*
* @param publicKeyE0
* @param modulusE0
*/
public void setPublicKey(BigInteger publicKeyE0, BigInteger modulusE0) {
publicKeyE = publicKeyE0;
modulusE = modulusE0;
}
/**
*
* @param message
* @return
*/
public String encrypt(String message) {
BigInteger exp = new BigInteger(message.getBytes());
BigInteger enc = encrypt(exp);
return enc.toString();
}
/**
*
* @param message
* @return
* @created 2006-5-18 19:33:15
*/
public BigInteger encrypt(BigInteger message) {
BigInteger rep = null;
String str_message = new String(message.toByteArray());
if (message != null) {
if (str_message.length() <= (N / 8))
if (publicKeyE != null && modulusE != null
&& message.toByteArray().length < Integer.MAX_VALUE)
rep = message.modPow(publicKeyE, modulusE);
}
return rep;
}
/**
*
* @param message
* @param publicKeyP
* @param modulusP
* @return
* @created 2006-5-18 19:33:17
*/
public String encrypt(String message, BigInteger publicKeyP,
BigInteger modulusP) {
BigInteger exp = new BigInteger(message.getBytes());
BigInteger enc = encrypt(exp, publicKeyP, modulusP);
return new String(enc.toByteArray());
}
/**
*
* @param message
* @param publicKeyP
* @param modulusP
* @return
* @created 2006-5-18 19:33:19
*/
public BigInteger encrypt(BigInteger message, BigInteger publicKeyP,
BigInteger modulusP) {
BigInteger rep = null;
String str_message = new String(message.toByteArray());
if (str_message.length() <= (N / 8))
if (publicKeyP != null && modulusP != null
&& message.toByteArray().length < Integer.MAX_VALUE)
rep = message.modPow(publicKeyP, modulusP);
return rep;
}
/**
*
* @param message
* @return
* @created 2006-5-18 19:33:23
*/
public String decrypt(String message) {
BigInteger exp = new BigInteger(message);
BigInteger enc = decrypt(exp);
return new String(enc.toByteArray());
}
/**
*
* @param encrypted
* @return
* @created 2006-5-18 19:33:26
*/
public BigInteger decrypt(BigInteger encrypted) {
return encrypted.modPow(privateKey, modulus);
}
/**
*
*
* @created 2006-5-18 17:29:59
*/
private void setPrivateKey() {
/*
* 设置privateKey,满足: 1. privateKey和phi0的最大公约数为1(互质)。 2.
* privateKey小于modulus 3. privateKey不小于p1和p2的最大者
*/
do {
privateKey = BigInteger.probablePrime(N / 2, random);
} while (privateKey.gcd(phi0).intValue() != 1
|| privateKey.compareTo(modulus) != -1
|| privateKey.compareTo(p1.max(p2)) == -1);
}
/**
*
*/
public String toString() {
String s = "";
s += "public = " + publicKey + "\n";
s += "modulus = " + modulus + "\n";
return s;
}
/**
* The getter method of the field modulus.
*
* @return Returns the modulus
* @created 2006-5-18 17:56:45
*/
public BigInteger getModulus() {
return this.modulus;
}
/**
* The getter method of the field publicKey.
*
* @return Returns the publicKey
* @created 2006-5-18 17:56:45
*/
public BigInteger getPublicKey() {
return this.publicKey;
}
}
import java.math.BigInteger;
import java.security.SecureRandom;
public class RSA {
private final static BigInteger one = BigInteger.ONE;
private final static SecureRandom random = new SecureRandom();
private final static int defaultBitLength = 128;
private BigInteger privateKey = null;// 私钥
private BigInteger publicKey = null;// 公钥
private BigInteger modulus = null;// n
private BigInteger p1 = null; // 原始质数p
private BigInteger p2 = null; // 原始质数q
private BigInteger modulusE; // 实际模数n
private BigInteger publicKeyE; // 实际公钥
private int N;// 位数
private BigInteger phi0;
public RSA() {
this(defaultBitLength);
}
/**
*
* Created constructor
*
* @param N
*/
public RSA(int N) {
this.N = N;
// 生成N位公钥和私钥
p1 = BigInteger.probablePrime(N / 2, random);
p2 = BigInteger.probablePrime(N / 2, random);
phi0 = (p1.subtract(one)).multiply(p2.subtract(one));
modulus = p1.multiply(p2);
setPrivateKey();
publicKey = privateKey.modInverse(phi0);
modulusE = modulus;
publicKeyE = publicKey;
}
/**
*
* @param publicKeyE0
* @param modulusE0
*/
public void setPublicKey(BigInteger publicKeyE0, BigInteger modulusE0) {
publicKeyE = publicKeyE0;
modulusE = modulusE0;
}
/**
*
* @param message
* @return
*/
public String encrypt(String message) {
BigInteger exp = new BigInteger(message.getBytes());
BigInteger enc = encrypt(exp);
return enc.toString();
}
/**
*
* @param message
* @return
* @created 2006-5-18 19:33:15
*/
public BigInteger encrypt(BigInteger message) {
BigInteger rep = null;
String str_message = new String(message.toByteArray());
if (message != null) {
if (str_message.length() <= (N / 8))
if (publicKeyE != null && modulusE != null
&& message.toByteArray().length < Integer.MAX_VALUE)
rep = message.modPow(publicKeyE, modulusE);
}
return rep;
}
/**
*
* @param message
* @param publicKeyP
* @param modulusP
* @return
* @created 2006-5-18 19:33:17
*/
public String encrypt(String message, BigInteger publicKeyP,
BigInteger modulusP) {
BigInteger exp = new BigInteger(message.getBytes());
BigInteger enc = encrypt(exp, publicKeyP, modulusP);
return new String(enc.toByteArray());
}
/**
*
* @param message
* @param publicKeyP
* @param modulusP
* @return
* @created 2006-5-18 19:33:19
*/
public BigInteger encrypt(BigInteger message, BigInteger publicKeyP,
BigInteger modulusP) {
BigInteger rep = null;
String str_message = new String(message.toByteArray());
if (str_message.length() <= (N / 8))
if (publicKeyP != null && modulusP != null
&& message.toByteArray().length < Integer.MAX_VALUE)
rep = message.modPow(publicKeyP, modulusP);
return rep;
}
/**
*
* @param message
* @return
* @created 2006-5-18 19:33:23
*/
public String decrypt(String message) {
BigInteger exp = new BigInteger(message);
BigInteger enc = decrypt(exp);
return new String(enc.toByteArray());
}
/**
*
* @param encrypted
* @return
* @created 2006-5-18 19:33:26
*/
public BigInteger decrypt(BigInteger encrypted) {
return encrypted.modPow(privateKey, modulus);
}
/**
*
*
* @created 2006-5-18 17:29:59
*/
private void setPrivateKey() {
/*
* 设置privateKey,满足: 1. privateKey和phi0的最大公约数为1(互质)。 2.
* privateKey小于modulus 3. privateKey不小于p1和p2的最大者
*/
do {
privateKey = BigInteger.probablePrime(N / 2, random);
} while (privateKey.gcd(phi0).intValue() != 1
|| privateKey.compareTo(modulus) != -1
|| privateKey.compareTo(p1.max(p2)) == -1);
}
/**
*
*/
public String toString() {
String s = "";
s += "public = " + publicKey + "\n";
s += "modulus = " + modulus + "\n";
return s;
}
/**
* The getter method of the field modulus.
*
* @return Returns the modulus
* @created 2006-5-18 17:56:45
*/
public BigInteger getModulus() {
return this.modulus;
}
/**
* The getter method of the field publicKey.
*
* @return Returns the publicKey
* @created 2006-5-18 17:56:45
*/
public BigInteger getPublicKey() {
return this.publicKey;
}
}
还有一个是用C#实现的快速排序算法,跟现在的论文有关
/*----------------------------------------------------------------
// 文件名:QuickSorter
// 文件功能描述:实现对订单效益值的快速排序
// 创建标识:
//----------------------------------------------------------------*/
using System.IO;
using System.IO.Compression;
namespace QuickSorter
{
public class QuickSorter
{
/// <summary>
/// 数值互换位置
/// 作者:张云
/// 日期:2007-06-06
/// </summary>
/// <param name="l1">要交换的订单效益值低位数字</param>
/// <param name="r1">要交换的订单效益值高位数字</param>
/// <param name="l2">对应订单效益值低位数字的订单号</param>
/// <param name="r2">对应订单效益值高位数字的订单号</param>
/// <returns></returns>
private void Swap(ref float l1, ref float r1,ref string l2,ref string r2)
{
float s;
s = l1;
l1 = r1;
r1 = s;
string t;
t = l2;
l2 = r2;
r2 = t;
}
/// <summary>
/// 排序过程
/// 作者:张云
/// 日期:2007-06-06
/// </summary>
/// <param name="list">要排序的订单效益值数组</param>
/// <param name="low">list数组排序开始的位置</param>
/// <param name="high">list数组排序结束的位置</param>
/// <param name="list">要排序的订单效益值数组对应的订单号数组</param>
/// <returns></returns>
public void Sort(float[] list, int low, int high,string[] orderNum)
{
float pivot;
int l, r;
int mid;
string ordPivot;
// 如果list数组排序开始的位置不小于排序的结束位置
if (high <= low)
{
return;
}
else if (high == low + 1)
{
if (list[low] < list[high])
{
Swap(ref list[low], ref list[high], ref orderNum[low], ref orderNum[high]);
}
return;
}
// 取得排序区间的中间位置
mid = (low + high) >> 1;
pivot = list[mid];
ordPivot = orderNum[mid];
Swap(ref list[low], ref list[mid], ref orderNum[low], ref orderNum[mid]);
l = low + 1;
r = high;
// 对l到r的区间进行排序
do
{
while (l <= r && list[l] > pivot)
{
l++;
}
while (r >= l && list[r] <= pivot)
{
r--;
}
if (l < r)
{
Swap(ref list[l], ref list[r], ref orderNum[l], ref orderNum[r]);
}
} while (l < r);
list[low] = list[r];
orderNum[low] = orderNum[r];
list[r] = pivot;
orderNum[r] = ordPivot;
// 对low到r-1区间的数递归排序
if (low + 1 < r)
{
Sort(list, low, r - 1, orderNum);
}
// 对r+1到high区间的数递归排序
if (r + 1 < high)
{
Sort(list, r + 1, high, orderNum);
}
}
}
}
// 文件名:QuickSorter
// 文件功能描述:实现对订单效益值的快速排序
// 创建标识:
//----------------------------------------------------------------*/
using System.IO;
using System.IO.Compression;
namespace QuickSorter
{
public class QuickSorter
{
/// <summary>
/// 数值互换位置
/// 作者:张云
/// 日期:2007-06-06
/// </summary>
/// <param name="l1">要交换的订单效益值低位数字</param>
/// <param name="r1">要交换的订单效益值高位数字</param>
/// <param name="l2">对应订单效益值低位数字的订单号</param>
/// <param name="r2">对应订单效益值高位数字的订单号</param>
/// <returns></returns>
private void Swap(ref float l1, ref float r1,ref string l2,ref string r2)
{
float s;
s = l1;
l1 = r1;
r1 = s;
string t;
t = l2;
l2 = r2;
r2 = t;
}
/// <summary>
/// 排序过程
/// 作者:张云
/// 日期:2007-06-06
/// </summary>
/// <param name="list">要排序的订单效益值数组</param>
/// <param name="low">list数组排序开始的位置</param>
/// <param name="high">list数组排序结束的位置</param>
/// <param name="list">要排序的订单效益值数组对应的订单号数组</param>
/// <returns></returns>
public void Sort(float[] list, int low, int high,string[] orderNum)
{
float pivot;
int l, r;
int mid;
string ordPivot;
// 如果list数组排序开始的位置不小于排序的结束位置
if (high <= low)
{
return;
}
else if (high == low + 1)
{
if (list[low] < list[high])
{
Swap(ref list[low], ref list[high], ref orderNum[low], ref orderNum[high]);
}
return;
}
// 取得排序区间的中间位置
mid = (low + high) >> 1;
pivot = list[mid];
ordPivot = orderNum[mid];
Swap(ref list[low], ref list[mid], ref orderNum[low], ref orderNum[mid]);
l = low + 1;
r = high;
// 对l到r的区间进行排序
do
{
while (l <= r && list[l] > pivot)
{
l++;
}
while (r >= l && list[r] <= pivot)
{
r--;
}
if (l < r)
{
Swap(ref list[l], ref list[r], ref orderNum[l], ref orderNum[r]);
}
} while (l < r);
list[low] = list[r];
orderNum[low] = orderNum[r];
list[r] = pivot;
orderNum[r] = ordPivot;
// 对low到r-1区间的数递归排序
if (low + 1 < r)
{
Sort(list, low, r - 1, orderNum);
}
// 对r+1到high区间的数递归排序
if (r + 1 < high)
{
Sort(list, r + 1, high, orderNum);
}
}
}
}