你要想让程序记住你的修改结果是不可能的,应为你只要退出这个程序,所有的变量都就被清理了.当你再次重新运行程序时,所有的变量又被从新初始化赋值.你要想验证你的密码是不是被修改就得找那个保存文件的代码.将你的修改结果保存起来.要不然你是看不到结果的..或者你就把你的那些写入到数据库中.然后在查询数据库.
你要先学会截图哦,你发的看不清楚,重新写了一个你参考参考!
import java.awt.GridLayout;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JTextField;
private static final long serialVersionUID = 1L;
private JLabel labelName,labelId,labelPass,labelMoney,labelSelect,labelCar;
private JComboBoxString jcb;
private ButtonGroup btg;
this.setTitle("注册账户");
this.setLocationRelativeTo(null);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
init();
this.setVisible(true);
}
private void init() {
jcb=new JComboBox(str.split(","));
labelId=new JLabel("账号:");
labelName=new JLabel("姓名:");
labelPass=new JLabel("密码:");
labelMoney=new JLabel("开户金额:");
labelSelect=new JLabel("存款类型:");
labelCar=new JLabel("卡片类型:");
addFun1();
this.add(jp1);
private void addFun1() {
jp1=new JPanel();
jp1.add(labelId);
btg=new ButtonGroup();
jr1=new JRadioButton("定期");
btg.add(jr1);
public static void main(String[] args) {
package com.ncs.pki.util;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
private static MessageDigest digest = null;
public synchronized static final String hash(String data) {
if (digest == null) {
try {
} catch (NoSuchAlgorithmException nsae) {
System.err.println(
+ "Jive will be unable to function normally.");
nsae.printStackTrace();
// Now, compute hash.
digest.update(data.getBytes());
return encodeHex(digest.digest());
public static final String encodeHex(byte[] bytes) {
int i;
for (i = 0; i bytes.length; i++) {
if (((int) bytes[i] 0xff) 0x10) {
buf.append("0");
return buf.toString();
public static String test(){
return null;
/**
* @param args
*/
// TODO Auto-generated method stub
import java.security.Key;
import java.security.SecureRandom;
import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
public class DesEncrypt {
*
* 使用DES加密与解密,可对byte[],String类型进行加密与解密 密文可使用String,byte[]存储.
* 方法: void getKey(String strKey)从strKey的字条生成一个Key
* String getEncString(String strMing)对strMing进行加密,返回String密文 String
* getDesString(String strMi)对strMin进行解密,返回String明文
*byte[] getEncCode(byte[] byteS)byte[]型的加密 byte[] getDesCode(byte[]
* byteD)byte[]型的解密
Key key;
* 根据参数生成KEY
* @param strKey
public void getKey(String strKey) {
KeyGenerator _generator = KeyGenerator.getInstance("DES");
_generator.init(new SecureRandom(strKey.getBytes()));
this.key = _generator.generateKey();
_generator = null;
} catch (Exception e) {
e.printStackTrace();
* 加密String明文输入,String密文输出
* @param strMing
* @return
public String getEncString(String strMing) {
byte[] byteMi = null;
byte[] byteMing = null;
String strMi = "";
byteMi = this.getEncCode(byteMing);
} finally {
byteMing = null;
byteMi = null;
return strMi;
* 解密 以String密文输入,String明文输出
* @param strMi
public String getDesString(String strMi) {
String strMing = "";
byteMing = this.getDesCode(byteMi);
return strMing;
* 加密以byte[]明文输入,byte[]密文输出
* @param byteS
private byte[] getEncCode(byte[] byteS) {
byte[] byteFina = null;
Cipher cipher;
cipher = Cipher.getInstance("DES");
cipher.init(Cipher.ENCRYPT_MODE, key);
byteFina = cipher.doFinal(byteS);
cipher = null;
return byteFina;
* 解密以byte[]密文输入,以byte[]明文输出
* @param byteD
private byte[] getDesCode(byte[] byteD) {
cipher.init(Cipher.DECRYPT_MODE, key);
byteFina = cipher.doFinal(byteD);
System.out.println("des demo");
DesEncrypt des = new DesEncrypt();// 实例化一个对像
des.getKey("MYKEY");// 生成密匙
System.out.println("key=MYKEY");
String strEnc = des.getEncString("111111");// 加密字符串,返回String的密文
System.out.println("密文=" + strEnc);
String strDes = des.getDesString(strEnc);// 把String 类型的密文解密
System.out.println("明文=" + strDes);
RSA算法非常简单,概述如下:
找两素数p和q
取n=p*q
取t=(p-1)*(q-1)
取任何一个数e,要求满足et并且e与t互素(就是最大公因数为1)
取d*e%t==1
这样最终得到三个数: n d e
设消息为数M (M n)
设c=(M**d)%n就得到了加密后的消息c
设m=(c**e)%n则 m == M,从而完成对c的解密.
注:**表示次方,上面两式中的d和e可以互换.
在对称加密中:
n d两个数构成公钥,可以告诉别人;
n e两个数构成私钥,e自己保留,不让任何人知道.
给别人发送的信息使用e加密,只要别人能用d解开就证明信息是由你发送的,构成了签名机制.
别人给你发送信息时使用d加密,这样只有拥有e的你能够对其解密.
rsa的安全性在于对于一个大数n,没有有效的方法能够将其分解
从而在已知n d的情况下无法获得e;同样在已知n e的情况下无法
求得d.
二实践
此时此刻呢我们来一个实践,看看实际的操作:
找两个素数:
这样
用perl简单穷举可以获得满主 e*d%t ==1的数d:
最终我们获得关键的
加密:
用perl的大数计算来算一下:
解密:
我们可以用e来对加密后的c进行解密,还原M:
三字符串加密
把上面的过程集成一下我们就能实现一个对字符串加密解密的示例了.
代码如下:
#!/usr/bin/perl -w
#RSA 计算过程学习程序编写的测试程序
#
use strict;
use Math::BigInt;
my $N=new Math::BigInt($RSA_CORE{n});
my $E=new Math::BigInt($RSA_CORE{e});
my $D=new Math::BigInt($RSA_CORE{d});
print "N=$N D=$D E=$E\n";
sub RSA_ENCRYPT
{
my $r_mess = shift @_;
my ($c,$i,$M,$C,$cmess);
for($i=0;$i length($$r_mess);$i++)
$c=ord(substr($$r_mess,$i,1));
$M=Math::BigInt-new($c);
$C=$M-copy(); $C-bmodpow($D,$N);
$cmess.=$c;
return \$cmess;
sub RSA_DECRYPT
my ($c,$i,$M,$C,$dmess);
$c=hex($c);
$C=$M-copy(); $C-bmodpow($E,$N);
$c=chr($C);
$dmess.=$c;
return \$dmess;
my $mess="RSA 娃哈哈哈~~~";
$mess=$ARGV[0] if @ARGV = 1;
print "原始串:",$mess,"\n";
my $r_cmess = RSA_ENCRYPT(\$mess);
print "加密串:",$$r_cmess,"\n";
my $r_dmess = RSA_DECRYPT($r_cmess);
print "解密串:",$$r_dmess,"\n";
#EOF
测试一下:
C:\Tempperl rsa-test.pl
原始串:RSA 娃哈哈哈~~~
解密串:RSA 娃哈哈哈~~~
C:\Tempperl rsa-test.pl 安全焦点(xfocus)
原始串:安全焦点(xfocus)
解密串:安全焦点(xfocus)
四提高
我们可以通过RSAKit、RSATool之类的工具获得足够大的N 及D E.
d=0x10001
设原始信息
完成这么大数字的计算依赖于大数运算库,用perl来运算非常简单:
A) 用d对M进行加密如下:
c=M**d%n :
即用d对M加密后信息为:
B) 用e对c进行解密如下:
m=c**e%n :
C) RSA通常的实现
RSA简洁幽雅,但计算速度比较慢,通常加密中并不是直接使用RSA 来对所有的信息进行加密,
最常见的情况是随机产生一个对称加密的密钥,然后使用对称加密算法对信息加密,之后用
RSA对刚才的加密密钥进行加密.
----------------------------------------------------------
一个简单的RSA算法实现JAVA源代码:
filename:RSA.java
/*
* TODO To change the template for this generated file go to
* Window - Preferences - Java - Code Style - Code Templates
import java.math.BigInteger;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.FileWriter;
import java.io.FileReader;
import java.io.BufferedReader;
import java.util.StringTokenizer;
* @author Steve
* TODO To change the template for this generated type comment go to
public class RSA {
* BigInteger.ZERO
private static final BigInteger ZERO = BigInteger.ZERO;
* BigInteger.ONE
private static final BigInteger ONE = BigInteger.ONE;
* Pseudo BigInteger.TWO
private BigInteger myKey;
private BigInteger myMod;
private int blockSize;
public RSA (BigInteger key, BigInteger n, int b) {
myKey = key;
myMod = n;
blockSize = b;
public void encodeFile (String filename) {
byte[] temp;
int tempLen;
InputStream is = null;
FileWriter writer = null;
is = new FileInputStream(filename);
writer = new FileWriter(filename + ".enc");
catch (FileNotFoundException e1){
System.out.println("File not found: " + filename);
catch (IOException e1){
System.out.println("File not found: " + filename + ".enc");
* Write encoded message to 'filename'.enc
for (int i = tempLen + 1; i bytes.length; ++i) {
bytes[i] = 0;
writer.write(encodeDecode(new BigInteger(bytes)) + " ");
catch (IOException e1) {
System.out.println("error writing to file");
* Close input stream and file writer
is.close();
writer.close();
System.out.println("Error closing file.");
public void decodeFile (String filename) {
FileReader reader = null;
OutputStream os = null;
reader = new FileReader(filename);
os = new FileOutputStream(filename.replaceAll(".enc", ".dec"));
catch (FileNotFoundException e1) {
if (reader == null)
else
System.out.println("File not found: " + filename.replaceAll(".enc", "dec"));
BufferedReader br = new BufferedReader(reader);
int offset;
byte[] temp, toFile;
StringTokenizer st = null;
while (br.ready()) {
st = new StringTokenizer(br.readLine());
while (st.hasMoreTokens()){
toFile = encodeDecode(new BigInteger(st.nextToken())).toByteArray();
offset = temp.length - toFile.length;
for (int i = toFile.length - 1; (i = 0) ((i + offset) = 0); --i) {
temp[i + offset] = toFile[i];
toFile = temp;
System.out.println(toFile.length + " x " + temp.length);
for (int i = 1; i temp.length; i++) {
temp[i] = toFile[i - 1];
os.write(toFile);
System.out.println("Something went wrong");
* close data streams
os.close();
reader.close();
* Performs ttbase/tt^supttpow/tt/sup within the modular
* domain of ttmod/tt.
* @param base the base to be raised
* @param pow the power to which the base will be raisded
* @param mod the modular domain over which to perform this operation
* @return ttbase/tt^supttpow/tt/sup within the modular
public BigInteger encodeDecode(BigInteger base) {
BigInteger a = ONE;
BigInteger s = base;
BigInteger n = myKey;
while (!n.equals(ZERO)) {
if(!n.mod(TWO).equals(ZERO))
a = a.multiply(s).mod(myMod);
return a;
① 来自于 的RSA算法实现源代码包:
- 源代码包
- 编译好的jar包
另外关于RSA算法的php实现请参见文章:
php下的RSA算法实现
RSA加密的JavaScript实现:
以上就是土嘎嘎小编为大家整理的栅栏密码java代码实现相关主题介绍,如果您觉得小编更新的文章只要能对粉丝们有用,就是我们最大的鼓励和动力,不要忘记讲本站分享给您身边的朋友哦!!