Use Java to implement RSA Algorithms

Source: Internet
Author: User
Tags mul

Use Java to implement RSA Algorithms
 
 
 
 
China IT lab collects and organizes-12-14. Save this article and recommend it to friends QQ.

--------------------------------------------------------------------------------

Organize your photos. Download Google photos
1. The principles of the RSA algorithm are as follows:
Principle 1.1
Assume that we need to transmit information from machine A to machine B. First, machine B randomly determines a key, which is called the private key private_key. This key is always stored in machine B without being sent out; then, the private_key calculates another key, which is called the Public Key public_key. This public_key feature is almost impossible to generate its private_key through this key calculation. Next, pass the public_key to machine A through the network,
After machine A receives the public_key, it uses this key to encrypt the information and sends the encrypted information to machine B over the network. Finally, machine B uses the known private_key to unbind the encrypted information.
Step 1
The security of RSA Algorithms depends on the difficulty of big data factor decomposition. Both public and private keys are functions of two prime numbers.
1.2.1
First select two big prime numbers P, Q, calculate n = p * q; M = p-1) * (q-1 );
1.2.2
Then the encryption key public_key is randomly selected, which requires mutual quality with M, such as public_key = m-1;
1.2.3
Use the Euclidean algorithm to calculate and decrypt the private key private_key, so that private_key meets
Public_key * private_key 3 1 (mod m)
Public_key, n is the public key, and private_key is the private key
1.2.4
When encrypting text, use the formula secretword = text ^ public_key (mod N) to obtain the ciphertext secretword
1.2.5
Use the formula word = text ^ private_key (mod N) to obtain the original text word = text ..

2 Programs
This algorithm is implemented in Java programming language and the development environment is eclipse.
// Bjtu software 0404
Import java. Io .*;

Public class RSA
{
Private int p = 0;
Private int q = 0;
Private long n = 0;
Private long m = 0;

Private long public_key = 0; // Public Key
Private long private_key = 0; // key

Private long text = 0; // plaintext
Private long secretword = 0; // ciphertext
Private long word = 0; // decrypted plaintext

// Determine whether it is a prime number
Public Boolean primenumber (long T)
{
Long K = 0;
K = (long) math. SQRT (double) t );
Boolean flag = true;
Outer: For (INT I = 2; I <= K; I ++)
{
If (T % I) = 0)
{
Flag = false;
Break outer;
}
}
Return flag;
}
// Input PQ
Public void inputpq () throws exception
{
Do {
System. Out. println ("Please input prime number P :");
Bufferedreader stdin = new bufferedreader (New inputstreamreader (system. In ));
String BR = stdin. Readline ();
This. P = integer. parseint (BR );
}
While (! Primenumber (this. p ));
Do {
System. Out. println ("Enter the prime number Q :");
Bufferedreader stdin = new bufferedreader (New inputstreamreader (system. In ));
String BR = stdin. Readline ();
This. q = integer. parseint (BR );
}
While (! Primenumber (this. q ));
This. n = This. p * This. Q;
This. M = (p-1) * (q-1 );
System. Out. println ("the product of these two prime numbers is p * Q:" + this. N );
System. Out. println ("the number of integers less than N and with N is M = (p-1) (q-1):" + this. m );
}
// Calculate the maximum public approx.
Public long gcd (long a, long B)
{
Long GCD;
If (B = 0)
GCD =;
Else
GCD = gcd (B, A % B );
System. Out. println ("GCD:" + gcd );
Return GCD;

}
// Enter the public key
Public void getpublic_key () throws exception
{
Do {
System. Out. println ("Enter the value of a public key. The value must be smaller than m and have interconnectivity with M :");
Bufferedreader stdin = new bufferedreader (New inputstreamreader (system. In ));
String BR = stdin. Readline ();
This. public_key = long. parselong (BR );
} While (this. public_key> = This. m) | (this. gcd (this. M, this. public_key )! = 1 ));
System. Out. println ("Public Key:" + this. public_key );
}
// Calculate the key
Public void getprivate_key ()
{
Long value = 1;
Outer: For (long I = 1; I ++)
{
Value = I * This. m + 1;
System. Out. println ("value:" + value );
If (Value % This. public_key = 0) & (value/This. public_key <this. m ))
{
This. private_key = value/This. public_key;
Break outer;
}
}
System. Out. println ("the generated private key is:" + this. private_key );
}
// Input plaintext
Public void gettext () throws exception
{
System. Out. println ("Enter plaintext :");
Bufferedreader stdin = new bufferedreader (New inputstreamreader (system. In ));
String BR = stdin. Readline ();
This. Text = long. parselong (BR );
}
// Encryption, decryption, and computing
Public long colum (long y, long N, long key)
{
Long Mul;
If (Key = 1)
Mul = Y % N;
Else
Mul = y * This. colum (Y, N, key-1) % N;
Return Mul;
}

// Encrypted and decrypted
Public void pascolum () throws exception
{
This. gettext ();
System. Out. println ("input plaintext:" + this. Text );
// Encryption
This. secretword = This. colum (this. text, this. N, this. public_key );
System. Out. println ("the resulting ciphertext is:" + this. secretword );
// Decrypt
This. Word = This. colum (this. secretword, this. N, this. private_key );
System. Out. println ("the decrypted plaintext is:" + this. Word );

}
Public static void main (string [] ARGs) throws exception
{
Rsa t = New RSA ();
T. inputpq ();
T. getpublic_key ();
T. getprivate_key ();
T. pascolum ();
}

}
3. Test Introduction
2.1 input PQ, calculate m, n
 
3.2 enter the public key to generate the key
 
3.3 input plaintext, generate ciphertext, and decrypt
The time limit here. The plaintext is temporarily replaced by a number. If you are interested, you can change the program to a number.
 

Enter the prime number P:
23
Enter the prime number Q:
29
The product of these two prime numbers is p * Q: 667.
The number of integers less than N and N is M = (p-1) (q-1): 616
Enter the value of a public key. The value must be smaller than m and be mutually compatible with M:
611
GCD: 1
GCD: 1
GCD: 1
GCD: 1
Public Key: 611
The generated private key is 123.
Enter plaintext:
311
Input plaintext: 311
The ciphertext is 653.
The decrypted plaintext is 311.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.