社区
Java SE
帖子详情
帮我编一个简单的加密程序?
ymjoy
2002-07-01 02:26:32
可不可以帮我编一个简单的加密程序?
要求:键入a输出z,输入b输出y依次类推。
...全文
35
4
打赏
收藏
帮我编一个简单的加密程序?
可不可以帮我编一个简单的加密程序? 要求:键入a输出z,输入b输出y依次类推。
复制链接
扫一扫
分享
转发到动态
举报
写回复
配置赞助广告
用AI写文章
4 条
回复
切换为时间正序
请发表友善的回复…
发表回复
打赏红包
newman0708
2003-04-22
打赏
举报
回复
不是都 一样吗?
import java.security.*;
import java.security.cert.X509Certificate;
import java.io.*;
import javax.crypto.*;
import javax.crypto.spec.*;
import java.lang.*;
import com.newman.io.FileReadWrite;
/**
* PBE算法
*
* <p>Title: newman的类库</p>
* <p>Description: 没有最好只有更好</p>
* <p>Copyright: Copyright (c) 2002</p>
* <p>Company: newman0708@eastday.com</p>
* @author newman0708
* @version 1.0
*/
public class PBE{
private String m_Password;
private final String SECRETKEYFACTORY= "PBEWithMD5AndDES";
private byte[] m_Salt = {
(byte)0xc7, (byte)0x73, (byte)0x21, (byte)0x8c,
(byte)0x7e, (byte)0xc8, (byte)0xee, (byte)0x99
};
private PBEKeySpec m_pbeKeySpec=null;
private SecretKeyFactory m_keyFac=null;
private SecretKey m_pbeKey=null;
private PBEParameterSpec m_pbeParamSpec=null;
private Cipher m_pbeCipher=null;
private int m_Count = 2;//???
public PBE(String password){
this.setPassword(password);
init();
}
public void setPassword(String password){
this.m_Password =password;
}
private void init(){
try {
this.m_pbeKeySpec = new PBEKeySpec(this.m_Password.toCharArray());
this.m_keyFac = SecretKeyFactory.getInstance(this.SECRETKEYFACTORY);
this.m_pbeKey = this.m_keyFac.generateSecret(this.m_pbeKeySpec);
// 生成pbe算法所需的参数对象,两个参数详见 RSA的 PKCS #5 标准
this.m_pbeParamSpec = new PBEParameterSpec(this.m_Salt, this.m_Count);
// 生成一个加密器
this.m_pbeCipher= Cipher.getInstance(this.SECRETKEYFACTORY);
}
catch (Exception ex) {
}
}
public byte[] encode(byte[] info){
if(info==null)
return null;
try{
// 初始化加密器
this.m_pbeCipher.init(Cipher.ENCRYPT_MODE,this.m_pbeKey,this.m_pbeParamSpec);
// 明文
byte[] cleartext = info;
// 加密
byte[] ciphertext = this.m_pbeCipher.doFinal(cleartext);
System.out.println("*********************");
System.out.println("加密后");
for (int i = 0 ;i<ciphertext.length ;i++){
System.out.println(ciphertext[i]);
}
System.out.println("*********************");
//返回密文
return ciphertext;
}
catch (Exception e){
System.out.println(e);
return null;
}
}
public byte[] decode(byte[] info){
if(info==null)
return null;
try{
// 初始化加密器
this.m_pbeCipher.init(Cipher.DECRYPT_MODE,this.m_pbeKey, this.m_pbeParamSpec);
// 密文
byte[] ciphertext = info;
System.out.println("*********************");
System.out.println("解密前");
for (int i = 0 ;i<ciphertext.length ;i++){
System.out.println(ciphertext[i]);
}
System.out.println("*********************");
// 解密
byte[] cleartext = this.m_pbeCipher.doFinal(ciphertext);
//返回明文
return cleartext;
}
catch (Exception e){
System.out.println(e);
return null;
}
}
public InputStream getInputStream(InputStream is) throws IOException {
try {
this.m_pbeCipher.init(Cipher.DECRYPT_MODE,this.m_pbeKey, this.m_pbeParamSpec);
CipherInputStream cis = new CipherInputStream(is, this.m_pbeCipher);
return cis;
}
catch (Exception ex) {
return null;
}
}
public OutputStream getOutputStream(OutputStream os) throws IOException {
try {
this.m_pbeCipher.init(Cipher.ENCRYPT_MODE,this.m_pbeKey,this.m_pbeParamSpec);
CipherOutputStream cos = new CipherOutputStream(os, this.m_pbeCipher);
return cos;
}
catch (Exception ex) {
System.err.println("error: getOutputStream");
return null;
}
}
public void writeFile(OutputStream os,String content){
try {
OutputStream os2=this.getOutputStream(os);
byte[] tom=content.getBytes();
System.out.println("byte("+tom.length +"): "+new String(tom));
os2.write(content.getBytes());
}
catch (Exception ex) {
}
}
public void PRINT(InputStream inputStream){
try {
int nEnd=128;
byte[] byteTom=new byte[nEnd];
int nCount=0;
//StringBuffer bufftext=new StringBuffer();
InputStream is=this.getInputStream(inputStream);
System.out.println("following is from is");
while((nCount=is.read(byteTom))!=-1){
String s=new String (byteTom);
System.out.println(s);
}
is.close ();
}
catch (Exception ex) {
System.err.println("ERROR: "+ex.toString());
}
}
public static void main(String[] args) throws Exception{
/*
PBE PBE1 = new PBE("newman0708");
byte[] en_string,de_string;
String content="Description: author nch@peoplemail.com.cn";//41
String content2="Description: 没有最好只有更好";//29
en_string = PBE1.encode(content2.getBytes());
de_string = PBE1.decode(en_string);
System.out.println("en_string: "+new String (en_string));
System.out.println("de_string: "+new String (de_string));
*/
PBE PBE1 = new PBE("newman0708");
String content="Description: author nch@peoplemail.com.cn";//41
String content2="Description: 没有最好只有更好";//29
FileOutputStream bw =new FileOutputStream("f://foo.txt");
PBE1.writeFile(bw,content);
BufferedInputStream br =new BufferedInputStream(new FileInputStream("f://foo.txt"));
PBE1.PRINT(br);
}
}
/*
*********************
加密后
113
-89
-55
-60
-122
-94
76
-122
36
114
-121
3
6
38
-22
-61
10
-110
23
115
-45
-81
-62
-75
111
-73
93
60
-23
80
100
-128
*********************
*********************
解密前
113
-89
-55
-60
-122
-94
76
-122
36
114
-121
3
6
38
-22
-61
10
-110
23
115
-45
-81
-62
-75
111
-73
93
60
-23
80
100
-128
*********************
en_string:
de_string: Description: 没有最好只有更好
*/
chongling
2002-07-01
打赏
举报
回复
核心的部分你不是已经写好了吗
================================================================
CSDN 论坛助手 Ver 1.0 B0402提供下载。 改进了很多,功能完备!
★ 浏览帖子速度极快![建议系统使用ie5.5以上]。 ★ 多种帖子实现界面。
★ 保存帖子到本地[html格式]★ 监视您关注帖子的回复更新。
★ 可以直接发贴、回复帖子★ 采用XML接口,可以一次性显示4页帖子,同时支持自定义每次显示帖子数量。可以浏览历史记录!
★ 支持在线检测程序升级情况,可及时获得程序更新的信息。
★★ 签名 ●
可以在您的每个帖子的后面自动加上一个自己设计的签名哟。
Http://www.ChinaOK.net/csdn/csdn.zip
Http://www.ChinaOK.net/csdn/csdn.rar
Http://www.ChinaOK.net/csdn/csdn.exe [自解压]
ymjoy
2002-07-01
打赏
举报
回复
老兄具体一点
疾风2002
2002-07-01
打赏
举报
回复
char chr='a',
chr_jm;
/*对字符进行加密*/
chr_jm='z'-(chr-'a');
System.out.println("把"+chr+"加密为"+chr_jm);
MD5
加密
、DES加解密工具、GBK&punycode转换小工具
本小软件是小
编
为方便日常
程序
开发工中对字符串的
加密
需要而开发制作的,不用上网找在线
加密
或架设个
程序
来获取
加密
字符串。 本工具简易开发,需要的朋友可以下载来备用,功能不算强大,只是方便使用。希望能
帮
到...
计算机 bat文件夹
加密
,bat文件
加密
介绍【图解】
懂电脑的人一定知道bat文件是什么,但对电脑一知半解或者不怎么懂电脑的,一定不知道它是什么了,...批处理文件,在DOS和Windows(任意)系统中,.bat文件是可执行文件,由一系列命令构成,其中可以包含对其他
程序
的调...
python
编
写
一个
简单
的
程序
,python写
一个
简单
的
程序
大家好,小
编
来为大家解答以下问题,python
编
写
一个
简单
的
程序
,python写
一个
简单
的
程序
,现在让我们一起来看看吧!今天要和大家分享8个好玩的脚本例子,初学者可以照着代码自己写写,在快速掌握Python语法的时候,...
访问
一个
网站需要那几个协议?
作为
一个
软件开发者,你一定会对网络应用如何工作有
一个
完整的层次化的认知,同样这里也包括这些应用所用到的技术:像浏览器,HTTP,HTML,网络服务器,需求处理等等。 本文将更深入的研究当你输入
一个
网址的时候,...
换一种方式做
加密
换一种方式做
加密
——著名
程序
员刘涛涛谈扭曲
加密
变换技术什么是扭曲
加密
变换呢?... 现在
加密
变换适用于VC6、VS2005、VS2008等这么几个平台,下面我给大家讲一下在这个软件的开发过程中的一些思考,以及我的一
Java SE
62,614
社区成员
307,327
社区内容
发帖
与我相关
我的任务
Java SE
Java 2 Standard Edition
复制链接
扫一扫
分享
社区描述
Java 2 Standard Edition
社区管理员
加入社区
获取链接或二维码
近7日
近30日
至今
加载中
查看更多榜单
社区公告
暂无公告
试试用AI创作助手写篇文章吧
+ 用AI写文章