String simple encryption implemented in Java

Source: Internet
Author: User
// Simple string encryption implemented in Java: // use a string key to encrypt the original string // please kindly advise
// JDK 5.0 is compiled
Import javax. Swing .*;
Import java. AWT .*;
Import java. AWT. event .*;
Import java. Io .*;
Import javax. Swing. Border .*;

Class Mima {
Char key;
Public static void main (string [] ARGs) throws exception {
New Mima ();
}
Public Mima (){
Mygui Tw = new mygui ("file encryption dialog box ");
Tw. setvisible (true );
}
// Encryption
Public String jiami (string S, string key ){
String STR = "";
Int ch;
If (key. Length () = 0 ){
Return S;
}
Else if (! S. Equals (null )){
For (INT I = 0, j = 0; I <S. Length (); I ++, J ++ ){
If (j> key. Length ()-1 ){
J = J % key. Length ();
}
Ch = S. codepointat (I) + key. codepointat (j );
If (CH> 65535 ){
Ch = CH % 65535; // ch-33 = (CH-33) % 95;
}
STR + = (char) ch;
}
}
Return STR;

}
// Decrypt
Public String jiemi (string S, string key ){
String STR = "";
Int ch;
If (key. Length () = 0 ){
Return S;
}
Else if (! S. Equals (key )){
For (INT I = 0, j = 0; I <S. Length (); I ++, J ++ ){
If (j> key. Length ()-1 ){
J = J % key. Length ();
}
Ch = (S. codepointat (I) + 65535-key. codepointat (j ));
If (CH> 65535 ){
Ch = CH % 65535; // ch-33 = (CH-33) % 95;
}
STR + = (char) ch;
}
}
Return STR;
}
Class mygui extends jframe
{
Jbutton b1 = new jbutton ("encrypt ");
Jbutton b2 = new jbutton ("decrypt ");
Panel pan1 = new Panel ();
Panel pan2 = new Panel ();
Panel pan3 = new Panel ();
Panel pan4 = new Panel ();
Panel pan5 = new Panel ();
//
Label label1 = new label ("sourse file ");
Label label2 = new label ("OBJ file ");
Label label3 = new label ("key :");


Textarea TF1 = new textarea ("text area 1", 4,30, textarea. scrollbars_vertical_only );
Textarea TF2 = new textarea ("text area 2", 4,30, textarea. scrollbars_vertical_only );
Textfield tf3 = new textfield ("key ");
Toolkit thekit = gettoolkit ();
Public mygui ()
{
Init ();
}
Public mygui (String title)
{
Super (title );
Init ();
}
Private void Init ()
{
Setlayout (New borderlayout ());
Pan1.setlayout (New gridlayout (1, 2 ));
//
Label1.setbackground (new color (220,220,220 ));
Label2.setbackground (new color (220,220,220 ));
Label3.setbackground (new color (220,220,220 ));
//
Tf3.setechochar ('*');
//

Dimension wndsize = thekit. getscreensize ();
Setbounds (wndsize. width/4, wndsize. Height/4,
Wndsize. width/2, wndsize. Height/4 );
Setdefaclocloseoperation (windowconstants. dispose_on_close );
// Lookandfeel
Setundecorated (true );
Getrootpane (). setwindowdecorationstyle (jrootpane. Frame );
// Encryption
B1.settooltiptext ("encrypted ");
B1.setmnemonic ('E ');
B1.addactionlistener (New actionlistener (){
Public void actionreceivmed (actionevent e ){
Tf2.settext (jiami (tf1.gettext (), tf3.gettext ()));
// Tf3.settext ("");
}
});
B1.addmouselistener (New mouseadapter (){
Public void mouseclicked (mouseevent e ){
Tf2.settext (jiami (tf1.gettext (), tf3.gettext ()));
// Tf3.settext ("");
}
});
// Decrypt
B2.settooltiptext ("decryption ");
B2.setmnemonic ('D ');
B2.addactionlistener (New actionlistener (){
Public void actionreceivmed (actionevent e ){
Tf2.settext (jiemi (tf1.gettext (), tf3.gettext ()));
// Tf3.settext ("");
}
});
B2.addmouselistener (New mouseadapter (){
Public void mouseclicked (mouseevent e ){
Tf2.settext (jiemi (tf1.gettext (), tf3.gettext ()));
// Tf3.settext ("");
}
});
//
Pan1.add (pan2 );
Pan1.add (pan3 );
Pan2.add (label1 );
Pan3.add (label2 );
Pan4.add (TF1 );
Pan4.add (TF2 );
Pan5.add (B1 );
Pan5.add (B2 );
Pan5.add (label3 );
Pan5.add (tf3 );
//.
Add ("North", pan1 );
Add ("South", pan5 );
Add ("center", pan4 );
}
}
}

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.