java EJB 加密与解密原理的一个例子
程序员文章站
2022-05-25 23:21:45
加密与解密原理的一个例子 package lockunlock; import java.awt.*; import ...
加密与解密原理的一个例子
package lockunlock;
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
import javax.swing.*;
import java.util.*;
public class lockunlock extends japplet {
private boolean isstandalone = false;
//get a parameter value
public string getparameter(string key, string def) {
return isstandalone ? system.getproperty(key, def) :
(getparameter(key) != null ? getparameter(key) : def);
}
//construct the applet
public lockunlock() {
}
//initialize the applet
public void init() {
try {
jbinit();
}
catch(exception e) {
e.printstacktrace();
}
}
//component initialization
private void jbinit() throws exception {
contentpane = (jpanel) this.getcontentpane();
jlabel1.settext("string");
jlabel1.setbounds(new rectangle(35, 36, 57, 21));
contentpane.setlayout(null);
this.setsize(new dimension(400, 300));
jlabel2.settext("string length");
jlabel2.setbounds(new rectangle(29, 73, 69, 22));
jtextfield1.settext("");
jtextfield1.setbounds(new rectangle(108, 40, 166, 17));
jtextfield2.settext("");
jtextfield2.setbounds(new rectangle(107, 72, 56, 21));
jbutton1.setbounds(new rectangle(30, 236, 137, 27));
jbutton1.settext("exercise 3");
jbutton1.addactionlistener(new lockunlock_jbutton1_actionadapter(this));
jbutton2.setbounds(new rectangle(218, 237, 131, 27));
jbutton2.settext("exercise 4");
jbutton2.addactionlistener(new lockunlock_jbutton2_actionadapter(this));
jtextfield3.settext("");
jtextfield3.setbounds(new rectangle(106, 105, 58, 21));
jlabel3.settext("moshu");
jlabel3.setbounds(new rectangle(36, 106, 86, 18));
contentpane.add(jlabel1, null);
contentpane.add(jbutton2, null);
contentpane.add(jbutton1, null);
contentpane.add(jlabel3, null);
contentpane.add(jtextfield2, null);
contentpane.add(jlabel2, null);
contentpane.add(jtextfield3, null);
contentpane.add(jtextfield1, null);
}
//get applet information
public string getappletinfo() {
return "applet information";
}
//get parameter info
public string[][] getparameterinfo() {
return null;
}
//main method
public static void main(string[] args) {
lockunlock applet = new lockunlock();
applet.isstandalone = true;
jframe frame = new jframe();
//exit_on_close == 3
frame.setdefaultcloseoperation(3);
frame.settitle("applet frame");
frame.getcontentpane().add(applet, borderlayout.center);
applet.init();
applet.start();
frame.setsize(400,320);
dimension d = toolkit.getdefaulttoolkit().getscreensize();
frame.setlocation((d.width - frame.getsize().width) / 2, (d.height - frame.getsize().height) / 2);
frame.setvisible(true);
}
//static initializer for setting look & feel
static {
try {
//uimanager.setlookandfeel(uimanager.getsystemlookandfeelclassname());
//uimanager.setlookandfeel(uimanager.getcrossplatformlookandfeelclassname());
}
catch(exception e) {
}
}
//declare datamember
int index;
//-----------------------------------------------------
jpanel contentpane;
jlabel jlabel1 = new jlabel();
jlabel jlabel2 = new jlabel();
jtextfield jtextfield2 = new jtextfield();
jtextfield jtextfield1 = new jtextfield();
jbutton jbutton1 = new jbutton();
jbutton jbutton2 = new jbutton();
jtextfield jtextfield3 = new jtextfield();
jlabel jlabel3 = new jlabel();
//----------------------n!------------------------------
public int function(int n){
if(n==1)
return 1;
else{
return n*function(n-1);
/*不是return function(n-1);
而是 n*function(n-1);*/
}
}
//-----------用递归法求一个串的全排列-----------------------
public void arrange(string prefix,string suffix,int[] array){
string newprefix,newsuffix;
int numofchars =suffix.length();
if(numofchars==1){
array[index]=integer.parseint(prefix+suffix);
index++;
}
else{
for(int i=1; i<=numofchars;i++){
newsuffix=suffix.substring(1,numofchars);
newprefix=prefix+suffix.charat(0);
arrange(newprefix,newsuffix,array);
suffix=newsuffix+suffix.charat(0);
}
}
}
//----------arrange from the min to the max------------------
/*public void rankforarrange(int[] array){
int bottom=array.length-1 ;
int temp;
for(int i=bottom;i>0;i--){
for(int j=0;j<i;j++){
if(array[j]>array[j+1]){
temp =array[j];
array[j] =array[j+1];
array[j+1]=temp;
}
}
}
}
*/
//-------------------find the aim number----------------------
public int findaim(int aim,int[] array){
boolean isfound=false;
int location=0;
int length=array.length ;
for(int i=0;i<length;i++){
if(array[i]==aim){
location=i;
isfound =true;
}
}
if(isfound)
return location;
else
system.out.println("not found");
return location;
/*在if里return 不行吗?*/
}
//------------------creat string-------------------------------
public string creatstring(int length){
stringbuffer bufstring=new stringbuffer();
for(int i=1;i<=length;i++){
bufstring.append(i) ;
}
return bufstring.tostring();
}
//-----------output result--------------------
public void outputresult1(){
index = 0; //clear to 0
string aimstring, prefix;
aimstring = jtextfield1.gettext();
int length = aimstring.length();
string strlength = string.valueof(length);
int aim = integer.parseint(aimstring);
/* 方法.parseint才是转换为int类型
而不是.getinteger*/
int[] eacharrange = new int[this.function(length)];
jtextfield2.settext(strlength);
prefix = ""; //make an empty string
if (aimstring.length() > 2 &&
aimstring.length() < 9 && aimstring != "") {
arrange(prefix, aimstring, eacharrange);
//rankforarrange(eacharrange);
arrays.sort(eacharrange);
string result = string.valueof(findaim(aim, eacharrange));
jtextfield3.settext(result);
}
else {
system.out.println("your string is too short");
}
}
//----------out put result 2---------------------
public void outputrestlt2(){
index=0;//let index come back to 0
string strlength, strmoshu,
aimstring, prefix,suffix;
int length, moshu,limit;
strlength = jtextfield2.gettext();
strmoshu = jtextfield3.gettext();
length = integer.parseint(strlength);
moshu = integer.parseint(strmoshu);
limit = function(length);
int[] eacharrange = new int[this.function(length)];
if (length > 2&&length<9&&
strlength!=""&&strmoshu!=""
&&moshu<limit) {
prefix = "";
suffix =creatstring(length);
arrange(prefix, suffix, eacharrange);
arrays.sort(eacharrange);
string strresult=string.valueof(eacharrange[moshu]);
jtextfield1.settext(strresult);
}
else
system.out.println("input ouf moshu, try again") ;
}
void jbutton1_actionperformed(actionevent e) {
this.outputresult1();
}
void jbutton2_actionperformed(actionevent e) {
this.outputrestlt2();
}
//-----------------------------------------------------------
}
class lockunlock_jbutton1_actionadapter implements java.awt.event.actionlistener {
lockunlock adaptee;
lockunlock_jbutton1_actionadapter(lockunlock adaptee) {
this.adaptee = adaptee;
}
public void actionperformed(actionevent e) {
adaptee.jbutton1_actionperformed(e);
}
}
class lockunlock_jbutton2_actionadapter implements java.awt.event.actionlistener {
lockunlock adaptee;
lockunlock_jbutton2_actionadapter(lockunlock adaptee) {
this.adaptee = adaptee;
}
public void actionperformed(actionevent e) {
adaptee.jbutton2_actionperformed(e);
}
}
package lockunlock;
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
import javax.swing.*;
import java.util.*;
public class lockunlock extends japplet {
private boolean isstandalone = false;
//get a parameter value
public string getparameter(string key, string def) {
return isstandalone ? system.getproperty(key, def) :
(getparameter(key) != null ? getparameter(key) : def);
}
//construct the applet
public lockunlock() {
}
//initialize the applet
public void init() {
try {
jbinit();
}
catch(exception e) {
e.printstacktrace();
}
}
//component initialization
private void jbinit() throws exception {
contentpane = (jpanel) this.getcontentpane();
jlabel1.settext("string");
jlabel1.setbounds(new rectangle(35, 36, 57, 21));
contentpane.setlayout(null);
this.setsize(new dimension(400, 300));
jlabel2.settext("string length");
jlabel2.setbounds(new rectangle(29, 73, 69, 22));
jtextfield1.settext("");
jtextfield1.setbounds(new rectangle(108, 40, 166, 17));
jtextfield2.settext("");
jtextfield2.setbounds(new rectangle(107, 72, 56, 21));
jbutton1.setbounds(new rectangle(30, 236, 137, 27));
jbutton1.settext("exercise 3");
jbutton1.addactionlistener(new lockunlock_jbutton1_actionadapter(this));
jbutton2.setbounds(new rectangle(218, 237, 131, 27));
jbutton2.settext("exercise 4");
jbutton2.addactionlistener(new lockunlock_jbutton2_actionadapter(this));
jtextfield3.settext("");
jtextfield3.setbounds(new rectangle(106, 105, 58, 21));
jlabel3.settext("moshu");
jlabel3.setbounds(new rectangle(36, 106, 86, 18));
contentpane.add(jlabel1, null);
contentpane.add(jbutton2, null);
contentpane.add(jbutton1, null);
contentpane.add(jlabel3, null);
contentpane.add(jtextfield2, null);
contentpane.add(jlabel2, null);
contentpane.add(jtextfield3, null);
contentpane.add(jtextfield1, null);
}
//get applet information
public string getappletinfo() {
return "applet information";
}
//get parameter info
public string[][] getparameterinfo() {
return null;
}
//main method
public static void main(string[] args) {
lockunlock applet = new lockunlock();
applet.isstandalone = true;
jframe frame = new jframe();
//exit_on_close == 3
frame.setdefaultcloseoperation(3);
frame.settitle("applet frame");
frame.getcontentpane().add(applet, borderlayout.center);
applet.init();
applet.start();
frame.setsize(400,320);
dimension d = toolkit.getdefaulttoolkit().getscreensize();
frame.setlocation((d.width - frame.getsize().width) / 2, (d.height - frame.getsize().height) / 2);
frame.setvisible(true);
}
//static initializer for setting look & feel
static {
try {
//uimanager.setlookandfeel(uimanager.getsystemlookandfeelclassname());
//uimanager.setlookandfeel(uimanager.getcrossplatformlookandfeelclassname());
}
catch(exception e) {
}
}
//declare datamember
int index;
//-----------------------------------------------------
jpanel contentpane;
jlabel jlabel1 = new jlabel();
jlabel jlabel2 = new jlabel();
jtextfield jtextfield2 = new jtextfield();
jtextfield jtextfield1 = new jtextfield();
jbutton jbutton1 = new jbutton();
jbutton jbutton2 = new jbutton();
jtextfield jtextfield3 = new jtextfield();
jlabel jlabel3 = new jlabel();
//----------------------n!------------------------------
public int function(int n){
if(n==1)
return 1;
else{
return n*function(n-1);
/*不是return function(n-1);
而是 n*function(n-1);*/
}
}
//-----------用递归法求一个串的全排列-----------------------
public void arrange(string prefix,string suffix,int[] array){
string newprefix,newsuffix;
int numofchars =suffix.length();
if(numofchars==1){
array[index]=integer.parseint(prefix+suffix);
index++;
}
else{
for(int i=1; i<=numofchars;i++){
newsuffix=suffix.substring(1,numofchars);
newprefix=prefix+suffix.charat(0);
arrange(newprefix,newsuffix,array);
suffix=newsuffix+suffix.charat(0);
}
}
}
//----------arrange from the min to the max------------------
/*public void rankforarrange(int[] array){
int bottom=array.length-1 ;
int temp;
for(int i=bottom;i>0;i--){
for(int j=0;j<i;j++){
if(array[j]>array[j+1]){
temp =array[j];
array[j] =array[j+1];
array[j+1]=temp;
}
}
}
}
*/
//-------------------find the aim number----------------------
public int findaim(int aim,int[] array){
boolean isfound=false;
int location=0;
int length=array.length ;
for(int i=0;i<length;i++){
if(array[i]==aim){
location=i;
isfound =true;
}
}
if(isfound)
return location;
else
system.out.println("not found");
return location;
/*在if里return 不行吗?*/
}
//------------------creat string-------------------------------
public string creatstring(int length){
stringbuffer bufstring=new stringbuffer();
for(int i=1;i<=length;i++){
bufstring.append(i) ;
}
return bufstring.tostring();
}
//-----------output result--------------------
public void outputresult1(){
index = 0; //clear to 0
string aimstring, prefix;
aimstring = jtextfield1.gettext();
int length = aimstring.length();
string strlength = string.valueof(length);
int aim = integer.parseint(aimstring);
/* 方法.parseint才是转换为int类型
而不是.getinteger*/
int[] eacharrange = new int[this.function(length)];
jtextfield2.settext(strlength);
prefix = ""; //make an empty string
if (aimstring.length() > 2 &&
aimstring.length() < 9 && aimstring != "") {
arrange(prefix, aimstring, eacharrange);
//rankforarrange(eacharrange);
arrays.sort(eacharrange);
string result = string.valueof(findaim(aim, eacharrange));
jtextfield3.settext(result);
}
else {
system.out.println("your string is too short");
}
}
//----------out put result 2---------------------
public void outputrestlt2(){
index=0;//let index come back to 0
string strlength, strmoshu,
aimstring, prefix,suffix;
int length, moshu,limit;
strlength = jtextfield2.gettext();
strmoshu = jtextfield3.gettext();
length = integer.parseint(strlength);
moshu = integer.parseint(strmoshu);
limit = function(length);
int[] eacharrange = new int[this.function(length)];
if (length > 2&&length<9&&
strlength!=""&&strmoshu!=""
&&moshu<limit) {
prefix = "";
suffix =creatstring(length);
arrange(prefix, suffix, eacharrange);
arrays.sort(eacharrange);
string strresult=string.valueof(eacharrange[moshu]);
jtextfield1.settext(strresult);
}
else
system.out.println("input ouf moshu, try again") ;
}
void jbutton1_actionperformed(actionevent e) {
this.outputresult1();
}
void jbutton2_actionperformed(actionevent e) {
this.outputrestlt2();
}
//-----------------------------------------------------------
}
class lockunlock_jbutton1_actionadapter implements java.awt.event.actionlistener {
lockunlock adaptee;
lockunlock_jbutton1_actionadapter(lockunlock adaptee) {
this.adaptee = adaptee;
}
public void actionperformed(actionevent e) {
adaptee.jbutton1_actionperformed(e);
}
}
class lockunlock_jbutton2_actionadapter implements java.awt.event.actionlistener {
lockunlock adaptee;
lockunlock_jbutton2_actionadapter(lockunlock adaptee) {
this.adaptee = adaptee;
}
public void actionperformed(actionevent e) {
adaptee.jbutton2_actionperformed(e);
}
}