tag:blogger.com,1999:blog-85195970436777844172024-03-19T09:53:43.915-07:00Geek's TweaksYour Recipe to Geek!Anonymoushttp://www.blogger.com/profile/06031158750101269976noreply@blogger.comBlogger4125tag:blogger.com,1999:blog-8519597043677784417.post-11259301872743570412013-07-09T09:23:00.002-07:002015-08-18T08:43:36.737-07:00S Hill Block cipher in Java<div dir="ltr" style="text-align: left;" trbidi="on">
S Hill Cipher is one of the basic substitution Hill Cipher scheme. According to Wikipedia:<br />
<br />
<br />
<blockquote class="tr_bq">
<span style="background-color: white; font-family: sans-serif; font-size: 13px; line-height: 19.1875px;">In </span><span style="font-family: sans-serif; font-size: x-small;"><span style="background-color: white; line-height: 19.1875px;">classical cryptography</span></span><span style="background-color: white; font-family: sans-serif; font-size: 13px; line-height: 19.1875px;">, the </span><b style="background-color: white; font-family: sans-serif; font-size: 13px; line-height: 19.1875px;">Hill cipher</b><span style="background-color: white; font-family: sans-serif; font-size: 13px; line-height: 19.1875px;"> is a </span><span style="font-family: sans-serif; font-size: x-small;"><span style="background-color: white; line-height: 19.1875px;">polygraphic substitution cipher</span></span><span style="background-color: white; font-family: sans-serif; font-size: 13px; line-height: 19.1875px;"> based on </span><span style="font-family: sans-serif; font-size: x-small;"><span style="background-color: white; line-height: 19.1875px;">linear algebra</span></span><span style="background-color: white; font-family: sans-serif; font-size: 13px; line-height: 19.1875px;">. Invented by </span><span style="font-family: sans-serif; font-size: x-small;"><span style="background-color: white; line-height: 19.1875px;">Lester S. Hill</span></span><span style="background-color: white; font-family: sans-serif; font-size: 13px; line-height: 19.1875px;"> in 1929, it was the first polygraphic cipher in which it was practical (though barely) to operate on more than three symbols at once.</span></blockquote>
<h4>
<b>Basic Algorithm:</b></h4>
<br />
Basic algorithm of Hill Cipher works with some matrix operation in modulo arithmetic Plain text is first broken into chunks of length <b>n</b>. Each chunk is then modulo multiplicated with a <b>nxn</b> matrix acting as key. The resulting chunks of text are then concatenated to from Cipher text. For decryption the <b>n</b>x<b>n </b>key is taken modulo inverse to from decryption key. Cipher text is then modulo multiplicated in the same manner as the plain text was, to get the Plain text back.<br />
<br />
Here is a Java program written to perform Encryption and Decryption using Hill Cipher. This program uses modulo 95 to cover the whole printable ASCII range.<br />
The HillCipher class uses a HillMatrix class and a Converter class to perform arithmetic and conversion operations respectively.<br />
<br />
HillMatrix Class:<br />
<br />
<br />
<pre class="brush: java">package encryption.hillcipher;
import java.math.BigInteger;
public class HillMatrix {
private int row;
private int column;
private int matrix[][];
private final int RANGE = 95;
public HillMatrix(int row, int col){
this.row = row;
this.column = col;
matrix = new int[row][col];
for( int i=0;i<row;i++){
for(int j=0;j<col;j++){
matrix[i][j]=0;
}
}
}
public void set(int i, int j, int val){
matrix[i][j]=val;
}
public int get(int i, int j){
return matrix[i][j];
}
public int getrowindex(){
return row;
}
public int getcolumnindex(){
return column;
}
public HillMatrix cross(HillMatrix B){
if(this.column != B.getrowindex()){
return null;
}
HillMatrix C = new HillMatrix(this.row,B.getcolumnindex());
for(int i=0;i<this.row;i++){
for(int j=0;j<B.getcolumnindex();j++){
for(int k=0;k<this.column;k++){
C.set(i, j, C.get(i, j) + this.get(i, k) * B.get(k, j));
}
}
}
return C;
}
private int det(HillMatrix m){
int res=0;
if(m.getcolumnindex() == 2){
res = m.get(0, 0)*m.get(1, 1) - m.get(1, 0)*m.get(0, 1);
return (res);
}
for(int i=0;i<m.getcolumnindex();i++){
res = (int) (res + Math.pow(-1, i) * cofac(i,0,m) * m.get(0,i));
}
return (res);
}
private int cofac( int i, int j,HillMatrix m){
int res;
int index = m.getcolumnindex()-1;
int deci = 0;
int decj ;
HillMatrix submat = new HillMatrix(index,index);
for(int k=0;k<index+1;k++){
decj=0;
if( k == j) {
deci=1;
continue;
}
for(int l=0;l<index+1;l++){
if( l == i){
decj=1;
continue;
}
submat.set(k-deci, l-decj, m.get(k, l));
}
}
res = det(submat);
return res;
}
public int det(){
return det(this);
}
public int cofac(int i, int j){
return cofac(i,j,this);
}
public HillMatrix inv(){
HillMatrix in = new HillMatrix(row , column);
int invdet = moduloinv(det(this));
int temp;
for(int i=0; i<row ; i++){
for(int j=0;j<column;j++){
temp = cofac(i,j,this)* (int) Math.pow(-1,i+j);
in.set(i, j, modulus(temp*invdet));
}
}
return in;
}
public HillMatrix transpose(){
HillMatrix m = new HillMatrix(row,column);
for(int i=0;i<row;i++){
for(int j=0;j<column;j++){
m.set(i, j, get(j,i));
}
}
return m;
}
private int modulus(int in){
int res;
if(in < 0){
return RANGE - (Math.abs(in)%RANGE);
}
res = Math.abs(in) % RANGE;
return res;
}
private int moduloinv(int in){
BigInteger bi = new BigInteger(String.valueOf(in));
try{
bi = bi.modInverse(new BigInteger(String.valueOf(RANGE)));
}
catch(Exception e){
System.out.println("Key is not inversable..!");
System.exit(-1);
}
return bi.intValue();
}
public HillMatrix mod(){
HillMatrix m = new HillMatrix(row,column);
for(int i=0;i<row;i++){
for(int j=0;j<column;j++){
m.matrix[i][j] = matrix[i][j]%RANGE;
}
}
return m;
}
public boolean IsInvertable(){
int det = det(this);
if( det != 0 && moduloinv(det) != 0) {
return true;
}
return false;
}
}
</pre>
<br />
Converter Class:
<br />
<pre class="brush: java">package encryption.hillcipher;
public class Converter{
private static final int OFFSET = 32;
public static int ToInt(char ch){
return ((int)ch-OFFSET);
}
public static char ToChar(int in){
return (char)(in + OFFSET);
}
}</pre>
<br />
Now here is the HillCipher class:
<br />
<pre class="brush: java">
public class HillCipher {
HillMatrix key;
int keyLenght;
public HillCipher(String keyText){
int l = keyText.length();
l=(int)Math.sqrt(l);
keyLenght = l;
key = new HillMatrix(keyLenght,keyLenght);
l = 0;
for(int i=0;i<keyLenght;i++){
for(int j=0;j<keyLenght;j++){
key.set(i, j, Converter.ToInt(keyText.charAt(l++)));
}
}
}
public StringBuffer Encrypt(StringBuffer pText){
StringBuffer cText = new StringBuffer();
HillMatrix enc = new HillMatrix(keyLenght,1);
int n = keyLenght -(pText.length() % keyLenght);
for(int i=0;i<n;i++){
pText.append('x');
}
n = 0;
while(n <pText.length()){
for(int i=0;i<keyLenght;i++){
enc.set(i, 0, Converter.ToInt(pText.charAt(n++)));
}
enc = key.cross(enc);
enc = enc.mod();
for(int i=0;i<keyLenght;i++){
cText.append(Converter.ToChar(enc.get(i, 0)));
}
}
return cText;
}
public StringBuffer Decrypt(StringBuffer cText){
StringBuffer pText = new StringBuffer();
HillMatrix dec = new HillMatrix(keyLenght,1);
HillMatrix invKey = key.inv();
int n = cText.length() % keyLenght;
for(int i = 0 ; i< n ;i++){
cText.append('x');
}
n = 0;
while(n <cText.length()){
for(int i =0;i<keyLenght;i++){
dec.set(i, 0, Converter.ToInt(cText.charAt(n++)));
}
dec = invKey.cross(dec);
dec = dec.mod();
for(int i=0;i<keyLenght;i++){
pText.append(Converter.ToChar(dec.get(i, 0)));
}
}
return pText;
}
public static boolean IsDecryptable(String keyText){
int l = keyText.length();
l=(int)Math.sqrt(l);
HillMatrix newkey = new HillMatrix(l,l);
int n = 0;
for(int i=0;i<l;i++){
for(int j=0;j<l;j++){package encryption.hillcipher;
public class Converter{
private static final int OFFSET = 32;
public static int ToInt(char ch){
return ((int)ch-OFFSET);
}
public static char ToChar(int in){
return (char)(in + OFFSET);
}
}
newkey.set(i, j, Converter.ToInt(keyText.charAt(n++)));
}
}
return newkey.IsInvertable();
}
}
</pre>
</div>Anonymoushttp://www.blogger.com/profile/06031158750101269976noreply@blogger.com0tag:blogger.com,1999:blog-8519597043677784417.post-34928626140628209622013-05-09T12:42:00.002-07:002013-05-09T12:48:29.187-07:00Open Terminal any where<div dir="ltr" style="text-align: left;" trbidi="on">
If you are a Linux user than you (must have to) use your terminal at least once each hour. Some times opening the terminal and navigating to the working directory using <i>cd</i> a number of time while keeping a strict sight on the spellings and case of directory path some times become very teasing. And you might wish to have an option on your right click menu with "Open with terminal"...!<br />
So here is how you can do this (I have done this on Ubuntu and I don't know about other distributions. If you are looking for some thing similar on windows, then try <a href="http://geeks-tweaks.blogspot.com/2013/04/open-command-prompt-any-where.html" target="_blank">this</a>) :<br />
<br />
<br />
<ol style="text-align: left;">
<li>Install <a href="https://apps.ubuntu.com/cat/applications/precise/nautilus-actions/" rel="nofollow" target="_blank">Nautilus Action Configuration Tool</a>. Nautilus is the default file manager in Ubuntu and many other Linux distros. According to <a href="http://en.wikipedia.org/wiki/Nautilus_(file_manager)" rel="nofollow" target="_blank">Wikipedia</a>:
<blockquote class="tr_bq">
Nautilus is the official file manager for the GNOME desktop. The name is a play on words, evoking the shell of a nautilus to represent an operating system shell. Nautilus replaced Midnight Commander in GNOME 1.4 and was the default from version 2.0 onwards.</blockquote>
<br />
</li>
<li>After installation open it.<br /><div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhFJ925EuLqjVJw2JeMvyerp0ngmw4XzhFl6sh45M1uyeS8JQ8nDOjAZBlQ_HtkmDRxCGVs-OeSey12rwLWRIqhSlbkiehTc_TnDo44iWPsghE7Jlke5IeBqjg7sMlckemyewH5sRTbkao/s1600/Screenshot+from+2013-05-10+00:12:50.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="261" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhFJ925EuLqjVJw2JeMvyerp0ngmw4XzhFl6sh45M1uyeS8JQ8nDOjAZBlQ_HtkmDRxCGVs-OeSey12rwLWRIqhSlbkiehTc_TnDo44iWPsghE7Jlke5IeBqjg7sMlckemyewH5sRTbkao/s400/Screenshot+from+2013-05-10+00:12:50.png" width="400" /></a></div>
<div class="separator" style="clear: both; text-align: left;">
<br /></div>
</li>
<li>Go to File > New.</li>
<li>Un Check <i>Display item in selection context menu.</i></li>
<li>Check <i>Display item in location context menu.</i></li>
<li>Set the context label as you want. For example I set it to Open in terminal..</li>
<li>Set the tooltip text.<br /><div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEglRwO4eD71daE5cL8M3n2g9t6S478jOd6LimnqKC8N-wfU_XecvtA5EcuBB09bYuII38kn4L5SpN_1wGzhqXiMWB1YF7CU1dj1KAf888-MqYN5kPWHa1NeSWQ2pBYbTJNiLf7xc_weAzw/s1600/Screenshot+from+2013-05-10+00:30:51.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="235" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEglRwO4eD71daE5cL8M3n2g9t6S478jOd6LimnqKC8N-wfU_XecvtA5EcuBB09bYuII38kn4L5SpN_1wGzhqXiMWB1YF7CU1dj1KAf888-MqYN5kPWHa1NeSWQ2pBYbTJNiLf7xc_weAzw/s400/Screenshot+from+2013-05-10+00:30:51.png" width="400" /></a></div>
<div class="separator" style="clear: both; text-align: center;">
<br /></div>
</li>
<li>Now in command tab set <b>Path </b>to <i>xterm. </i>Ubuntu users can also use <i>gnome-terminal.</i></li>
<li>Set <b>Working Directory </b>to <i>%d/%b </i>or <i>%d </i>if you want to open terminal in base directory only.<br /><div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjn_uZsgtbb3Rwvvf46x4xGpJCE0Ok4Fra9xDNWX4FtMeb8TxfNMQj9LS4Rw4wigVLGKMsH0k-SMPPe81l36RsGxIZ06T1Aok_GS66xkK-S3DUbrcjulQPZZ7nPdDnVYF8rfuvJR-gkocU/s1600/Screenshot+from+2013-05-10+00:15:59.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="261" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjn_uZsgtbb3Rwvvf46x4xGpJCE0Ok4Fra9xDNWX4FtMeb8TxfNMQj9LS4Rw4wigVLGKMsH0k-SMPPe81l36RsGxIZ06T1Aok_GS66xkK-S3DUbrcjulQPZZ7nPdDnVYF8rfuvJR-gkocU/s400/Screenshot+from+2013-05-10+00:15:59.png" width="400" /></a></div>
</li>
<li>Save and exit.</li>
<li>Now go to your required directory and enjoy....<br /><div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiS8tV9h7u_wjqBkpaOIWWhS19IxB7jCnEJAMfa4EEoQqEGja7ZLbuYvNJwqHNYl_Xi-lLafTbiJ6K4b5xM1l5YiU3sMdI1iz9nXkrrxkukkAAGzmKHyKo6jQytcvIIUBa0RVyT4qFEbuM/s1600/Screenshot+from+2013-05-10+00:22:11.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="241" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiS8tV9h7u_wjqBkpaOIWWhS19IxB7jCnEJAMfa4EEoQqEGja7ZLbuYvNJwqHNYl_Xi-lLafTbiJ6K4b5xM1l5YiU3sMdI1iz9nXkrrxkukkAAGzmKHyKo6jQytcvIIUBa0RVyT4qFEbuM/s400/Screenshot+from+2013-05-10+00:22:11.png" width="400" /></a></div>
</li>
</ol>
</div>
Anonymoushttp://www.blogger.com/profile/06031158750101269976noreply@blogger.com0tag:blogger.com,1999:blog-8519597043677784417.post-8038673333520525962013-04-28T02:57:00.000-07:002013-04-28T03:11:44.958-07:00Open Command Prompt Any Where<div dir="ltr" style="text-align: left;" trbidi="on">
If you are a programmer or you like to work with command line or you use some utilities that only runs on command line then you might really hate opening the command line then separately moving to the current directory by typing cd [my directory] hundred times trying to keeping the spellings of your directory path exactly same....<br />
I my self really dont like it. So there is the mercy of Windows <a href="http://en.wikipedia.org/wiki/Windows_Registry" rel="nofollow" target="_blank">Registry</a>....<br />
According to Wikipedia<br />
<blockquote class="tr_bq">
<blockquote class="tr_bq">
<blockquote class="tr_bq">
<span style="background-color: #eeeeee; font-family: Arial, Helvetica, sans-serif;"><span style="line-height: 14.392361640930176px;">The </span><b style="line-height: 14.392361640930176px;">Windows Registry</b><span style="line-height: 14.392361640930176px;"> is a hierarchical </span>database<span style="line-height: 14.392361640930176px;"> that stores configuration settings and options on </span>Microsoft Windows<span style="line-height: 14.392361640930176px;"> </span>operating systems<span style="line-height: 14.392361640930176px;">. It contains settings for low-level operating system components and for applications running on the platform that have opted to use the registry.</span></span></blockquote>
</blockquote>
</blockquote>
<br />
Opting the other details I am going to show you a way with which you can open the command prompt directly to your current working directory.<br />
<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgRE8rPbOzYPq_xGXF23sa5Z6mIpwGDTJkTslXCNvW-KigbDhpSlLJPQgbbneMdZkysLdO6JMsVVTKCbd_ZwPFWFxwrwIU5VbrsOS86CUchatLcjD5pZcQ6EvZnAdHySETNX_WvrKkQp08/s1600/Screen1.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="250" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgRE8rPbOzYPq_xGXF23sa5Z6mIpwGDTJkTslXCNvW-KigbDhpSlLJPQgbbneMdZkysLdO6JMsVVTKCbd_ZwPFWFxwrwIU5VbrsOS86CUchatLcjD5pZcQ6EvZnAdHySETNX_WvrKkQp08/s400/Screen1.png" width="400" /></a></div>
<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgKbw8X6Y74IGfXma-olNcYs9bBLxVtJcukMknrW45PhxxDCKyftY8HMsFCZO03IGg5Pc8PMs64cersolmxqE5MSJ2xpVVcMbU8pnvflMn9gR7LYdkNGFhytKabv4pumesiJGqfbOxXdJU/s1600/Screen1.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="250" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgKbw8X6Y74IGfXma-olNcYs9bBLxVtJcukMknrW45PhxxDCKyftY8HMsFCZO03IGg5Pc8PMs64cersolmxqE5MSJ2xpVVcMbU8pnvflMn9gR7LYdkNGFhytKabv4pumesiJGqfbOxXdJU/s400/Screen1.png" width="400" /></a></div>
<br />
Here are a few easy steps to get this functionality.<br />
<br />
<br />
<ol style="text-align: left;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgj2K0wvrsM_aRDHhNEvb2ib-qw4nZUtFBxkWLPPXUKUB6GtShuQY39UjFAX4mv4a_8QGRpemV3o2h0OQvqQs3o5diVt7mCiYtKGXBZS1BQVJRyyjVQ7F3L5K6YGEKULMaSEnzbaLg8814/s1600/1.png" imageanchor="1" style="clear: right; float: right; margin-bottom: 1em; margin-left: 1em;"><br /></a>
<li>Open Run from start menu or press win+R.</li>
<li>Type regedit and press ok.</li>
<li><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEg_MOXEZ05hoQZXFq8xyW1dVGwec_7NJKPwi58ccz5niQjLD0WsrTa76OhhlY066_UlCrYrLPG3hfBvT3HK95M8RrxPpkOkTB0GpTbehJohDQ0dw-DkPLJIaohJ5fGGxz_gAyMoQlgmNwo/s1600/1.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="208" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEg_MOXEZ05hoQZXFq8xyW1dVGwec_7NJKPwi58ccz5niQjLD0WsrTa76OhhlY066_UlCrYrLPG3hfBvT3HK95M8RrxPpkOkTB0GpTbehJohDQ0dw-DkPLJIaohJ5fGGxz_gAyMoQlgmNwo/s400/1.png" width="400" /></a></li>
<li>In the Registry Editor press Cltr+F to open find dialog and uncheck all options except "key". Press Find.</li>
<li>The key tree view will high light some key like "CDO.DropDirectory". But this is not what we are looking for.</li>
<li>Just keep pressing F3 until the tree high lights the key with name "Direcotry".</li>
<li>Drop this key. You will find 3 further keys here. Drop the one with name Background.</li>
<li>Here you will find 2 keys again. Right click the "shell" key and create a new key.</li>
<li>Name this key "Open Command Prompt Here" or any other name that you want....!</li>
<li>Now right click this key and create another with name "command".</li>
<li>High light the command key and you will see that the table on the right column is showing (Default), REG_SZ, Not Set. Right click it and press <b>Modify...</b></li>
<li>In the key value input box right "cmd.exe".</li>
<li><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgj2K0wvrsM_aRDHhNEvb2ib-qw4nZUtFBxkWLPPXUKUB6GtShuQY39UjFAX4mv4a_8QGRpemV3o2h0OQvqQs3o5diVt7mCiYtKGXBZS1BQVJRyyjVQ7F3L5K6YGEKULMaSEnzbaLg8814/s1600/1.png" imageanchor="1" style="clear: right; margin-bottom: 1em; margin-left: 1em;"><img border="0" height="250" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgj2K0wvrsM_aRDHhNEvb2ib-qw4nZUtFBxkWLPPXUKUB6GtShuQY39UjFAX4mv4a_8QGRpemV3o2h0OQvqQs3o5diVt7mCiYtKGXBZS1BQVJRyyjVQ7F3L5K6YGEKULMaSEnzbaLg8814/s400/1.png" width="400" /></a></li>
</ol>
<div>
<br /></div>
<div>
<br /></div>
<div>
Now go to any of your directory right click and Enjoy!</div>
</div>
Anonymoushttp://www.blogger.com/profile/06031158750101269976noreply@blogger.com0tag:blogger.com,1999:blog-8519597043677784417.post-47384572373843884492013-04-26T09:00:00.001-07:002013-04-28T03:10:56.941-07:00RSA Public Key Encryption Algorithm in Java<div dir="ltr" style="text-align: left;" trbidi="on">
<h2 style="text-align: left;">
Definition: </h2>
<blockquote class="tr_bq">
<blockquote class="tr_bq">
<span style="background-color: #eeeeee; font-family: Arial, Helvetica, sans-serif;"><b style="line-height: 19.200000762939453px;">RSA</b><span style="line-height: 19.200000762939453px;"> is an </span>algorithm<span style="line-height: 19.200000762939453px;"> for </span>public-key cryptography<span style="line-height: 19.200000762939453px;"> that is based on the presumed difficulty of </span>factoring<span style="line-height: 19.200000762939453px;"> </span>large integers<span style="line-height: 19.200000762939453px;">, the </span>factoring problem<span style="line-height: 19.200000762939453px;">.</span></span></blockquote>
</blockquote>
<h2 style="text-align: left;">
Algorithm:</h2>
<blockquote class="tr_bq">
<ul style="background-color: white; font-family: sans-serif; font-size: 13px; line-height: 19.200000762939453px; list-style-image: url(data:image/png; margin: 0.3em 0px 0px 1.6em; padding: 0px;">
<li style="margin-bottom: 0.1em;"><img alt="p" class="tex" src="http://upload.wikimedia.org/math/8/3/8/83878c91171338902e0fe0fb97a8c47a.png" style="border: none; vertical-align: middle;" /> and <img alt="q" class="tex" src="http://upload.wikimedia.org/math/7/6/9/7694f4a66316e53c8cdd9d9954bd611d.png" style="border: none; vertical-align: middle;" />: the primes from the key generation,</li>
</ul>
<ul style="background-color: white; font-family: sans-serif; font-size: 13px; line-height: 19.200000762939453px; list-style-image: url(data:image/png; margin: 0.3em 0px 0px 1.6em; padding: 0px;">
<li style="margin-bottom: 0.1em;"><img alt="d_P = d\text{ (mod }p-1\text{)}" class="tex" src="http://upload.wikimedia.org/math/4/f/5/4f550a8c494b6904be4dacbe3c6a35c1.png" style="border: none; vertical-align: middle;" />,</li>
</ul>
<ul style="background-color: white; font-family: sans-serif; font-size: 13px; line-height: 19.200000762939453px; list-style-image: url(data:image/png; margin: 0.3em 0px 0px 1.6em; padding: 0px;">
<li style="margin-bottom: 0.1em;"><img alt="d_Q = d\text{ (mod }q-1\text{)}" class="tex" src="http://upload.wikimedia.org/math/1/f/3/1f35b6366a2125a536b75744d2b383cc.png" style="border: none; vertical-align: middle;" /> and</li>
</ul>
<ul style="background-color: white; font-family: sans-serif; font-size: 13px; line-height: 19.200000762939453px; list-style-image: url(data:image/png; margin: 0.3em 0px 0px 1.6em; padding: 0px;">
<li style="margin-bottom: 0.1em;"><img alt="q_\text{inv} = q^{-1}\text{ (mod }p\text{)}" class="tex" src="http://upload.wikimedia.org/math/4/b/6/4b6d1c49160796f651ef2845e3aec4b8.png" style="border: none; vertical-align: middle;" />.</li>
</ul>
<ul style="background-color: white; font-family: sans-serif; font-size: 13px; line-height: 19.200000762939453px; list-style-image: url(data:image/png; margin: 0.3em 0px 0px 1.6em; padding: 0px;">
<li style="margin-bottom: 0.1em;"><img alt="m_1 = c^{d_P}\text{ (mod }p\text{)}" class="tex" src="http://upload.wikimedia.org/math/f/2/a/f2a4b511524a02350e2ca752cf1dec51.png" style="border: none; vertical-align: middle;" /></li>
</ul>
<ul style="background-color: white; font-family: sans-serif; font-size: 13px; line-height: 19.200000762939453px; list-style-image: url(data:image/png; margin: 0.3em 0px 0px 1.6em; padding: 0px;">
<li style="margin-bottom: 0.1em;"><img alt="m_2 = c^{d_Q}\text{ (mod }q\text{)}" class="tex" src="http://upload.wikimedia.org/math/9/f/e/9febbc37580671705e3bc6ee38142683.png" style="border: none; vertical-align: middle;" /></li>
</ul>
<ul style="background-color: white; font-family: sans-serif; font-size: 13px; line-height: 19.200000762939453px; list-style-image: url(data:image/png; margin: 0.3em 0px 0px 1.6em; padding: 0px;">
<li style="margin-bottom: 0.1em;"><img alt="h = q_\text{inv}*(m_1-m_2)\text{ (mod }p\text{)}" class="tex" src="http://upload.wikimedia.org/math/2/6/5/265f957f9541d3bdf3ac1bb78f19e86f.png" style="border: none; vertical-align: middle;" /> (if <img alt="m_1 < m_2" class="tex" src="http://upload.wikimedia.org/math/0/1/d/01d357661ef5691c57936bd8b9d8a847.png" style="border: none; vertical-align: middle;" /> then some libraries compute <i>h</i> as <img alt="q_\text{inv} \times (m_1+p-m_2)\text{ (mod }p\text{)}" class="tex" src="http://upload.wikimedia.org/math/7/a/b/7abf6623ac0d55cdc390e68b00696c77.png" style="border: none; vertical-align: middle;" />)</li>
</ul>
<ul style="background-color: white; font-family: sans-serif; font-size: 13px; line-height: 19.200000762939453px; list-style-image: url(data:image/png; margin: 0.3em 0px 0px 1.6em; padding: 0px;">
<li style="margin-bottom: 0.1em;"><img alt="m = m_2 + (h*q)\," class="tex" src="http://upload.wikimedia.org/math/b/a/2/ba25235941ae1e2c07a449464e5976e1.png" style="border: none; vertical-align: middle;" /><span style="background-color: transparent;"> </span></li>
</ul>
</blockquote>
<i>Definition and Algorithm taken from <a href="http://en.wikipedia.org/wiki/RSA_(algorithm)" rel="nofollow" target="_blank">Wikipedia</a>.</i><br />
<i><br /></i>
I have write a Java program implementing the RSA Public key cipher algorithm. You can use the code in <i style="font-weight: bold;">case 'k' </i>,<i style="font-weight: bold;"> case 'c' </i>, <b><i>case 'p'</i></b> for key generation, encryption and decryption respectively.<br />
<br />
<div>
<pre consolas="" style="background-color: #999999; border: 1px solid black; color: #333333;">package encryption.rsa;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.math.BigInteger;
public class RSA {
public static void main(String[] args) throws IOException {
char selection = '3';
BufferedReader console = new BufferedReader(new InputStreamReader(System.in));
BigInteger p;
BigInteger q;
BigInteger n;
BigInteger phiN;
BigInteger e = new BigInteger("0");
BigInteger d = new BigInteger("0");
BigInteger plain;
BigInteger cipher;
while (selection != 'e') {
switch (selection) {
case 'k':
System.out.print("Enter the Value of p: ");
p = new BigInteger(console.readLine());
System.out.print("Enter the Value of q: ");
q = new BigInteger(console.readLine());
if (!p.isProbablePrime(4) || !q.isProbablePrime(4)) {
System.out.print("p or q is a composite number!\n\n");
break;
}
n = p.multiply(q);
phiN = p.subtract(new BigInteger("1")).multiply(q.subtract(new BigInteger("1")));
for (int i = 2; i < (phiN.intValue() / 2); i++) {
if (phiN.remainder(new BigInteger(String.valueOf(i))).intValue() != 0) {
e = new BigInteger(String.valueOf(i));
d = e.modInverse(phiN);
break;
}
}
System.out.print("\n\nPublic Key: {" + e.toString() + ", " + n.toString() + "}\n");
System.out.print("Private Key: {" + d.toString() + ", " + n.toString() + "}\n");
break;
case 'c':
System.out.print("\n\nEnter the value to Encrypt: ");
plain = new BigInteger(console.readLine());
System.out.print("Enter the value of e: ");
e = new BigInteger(console.readLine());
System.out.print("Enter the value of n: ");
n = new BigInteger(console.readLine());
cipher = plain.modPow(e, n);
System.out.print("\nThe Ciphered Value of " + plain.toString()
+ " is " + cipher.toString() + '.');
break;
case 'p':
System.out.print("\n\nEnter the value to Decrypt: ");
cipher = new BigInteger(console.readLine());
System.out.print("Enter the value of d: ");
d = new BigInteger(console.readLine());
System.out.print("Enter the value of n: ");
n = new BigInteger(console.readLine());
plain = cipher.modPow(d, n);
System.out.print("\nThe Deciphered Value of " + cipher.toString()
+ " is " + plain.toString() + '.');
break;
}
System.out.println("\n\n\nPlease Select From the Following:");
System.out.println("Generate [k]ey pair.");
System.out.println("En[c]rypt.");
System.out.println("Decry[p]t");
System.out.println("[E]xit");
selection = console.readLine().charAt(0);
}
}
}
</pre>
</div>
</div>
Anonymoushttp://www.blogger.com/profile/06031158750101269976noreply@blogger.com0