傻瓜式 RSA 加解密演算法實作 |
|
jackkcg
站務副站長 發表:891 回覆:1050 積分:848 註冊:2002-03-23 發送簡訊給我 |
http://140.109.22.109/wangsd/CryptoSystem/RSA.htm 傻瓜式 RSA 加解密演算法實作
import java.math.*; /**
* A class to encrypt and decrypt plaintext and ciphertext, respectively.
*/ public class RSA
{
/**
* Encrypts the plaintext.
*
* @param message The plaintext message to be encrypted.
* @param publickey The public key.
* @param modulo The modulo value.
* @return The ciphertext as a
BigInteger array.
*/ public static BigInteger[] encrypt(String message, BigInteger publickey, BigInteger modulo)
{
byte[] temp;
byte[] digits = message.getBytes(); BigInteger[] bigdigits = new BigInteger[digits.length]; for (int i = 0; i < bigdigits.length; i ) {
temp = new byte[1];
temp[0] = digits[i];
bigdigits[i] = new BigInteger(temp);
} BigInteger[] encrypted = new BigInteger[bigdigits.length]; try
{
for (int j = 0; j < bigdigits.length; j )
encrypted[j] = bigdigits[j].modPow(publickey, modulo);
}
catch(Exception e)
{
return null;
} return encrypted;
} /**
* Decrypts the ciphertext.
*
* @param encrypted The ciphertext as a BigInteger array.
* @param privatekey The private key.
* @param modulo The modulo value.
* @return The decrypted plaintext.
*/ public static String decrypt(BigInteger[] encrypted, BigInteger privatekey, BigInteger modulo)
{
BigInteger[] decrypted = new BigInteger[encrypted.length]; try
{
for(int i = 0; i < decrypted.length; i )
decrypted[i] = new BigInteger(encrypted[i].toString()).modPow(privatekey, modulo);
}
catch(Exception e)
{
return null;
} for (int i = 0; i < decrypted.length; i )
{
System.out.println(decrypted[i].toString());
} char[] array = new char[decrypted.length]; for (int j = 0; j < array.length; j )
array[j] = (char)(decrypted[j].intValue()); return new String(array);
} } // class RSA
------
********************************************************** 哈哈&兵燹 最會的2大絕招 這個不會與那個也不會 哈哈哈 粉好 Delphi K.Top的K.Top分兩個字解釋Top代表尖端的意思,希望本討論區能提供Delphi的尖端新知 K.表Knowlege 知識,就是本站的標語:Open our mind |
本站聲明 |
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。 2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。 3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇! |