如何使用自定义的密码(随意给出一个字符串)对文件或流加密.

javapro 2003-11-22 08:32:35
看到java的加密应用中,都是生成一个SecurityRandom,然后用该对象生产密钥,再进行加密,加密解密都要用到这个密钥"对象",但我碰到的问题是用户在两地交换文件,并希望能加密文件,且能够更改密码,类似于在a的修改以一个字符串的形式修改某个参数后,在b地只要把相应的参数改成一样的直就行就行,而不希望把一个java对象序列化或存成字节后传输.请问有什么方法可以实现?
...全文
36 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
superreset 2003-12-23
  • 打赏
  • 举报
回复
高人
kangta 2003-12-23
  • 打赏
  • 举报
回复
顶 学习
noratong 2003-11-22
  • 打赏
  • 举报
回复
你的问题中有个错误概念,"不希望把一个java对象序列化或存成字节后传输.请问有什么方法可以实现???"java里面的对象只能通过序列化后才能通过网络传输.
beyondtkl 2003-11-22
  • 打赏
  • 举报
回复
不是有大名鼎鼎的des算法么

使用密钥对文件进行加密 然后使用相同的密钥解密

或者用rsa算法
cheify 2003-11-22
  • 打赏
  • 举报
回复
JDK文档上的

In order to use Password-Based Encryption (PBE) as defined in PKCS #5, we have to specify a salt and an iteration count. The same salt and iteration count that are used for encryption must be used for decryption:

PBEKeySpec pbeKeySpec;
PBEParameterSpec pbeParamSpec;
SecretKeyFactory keyFac;

// Salt
byte[] salt = {
(byte)0xc7, (byte)0x73, (byte)0x21, (byte)0x8c,
(byte)0x7e, (byte)0xc8, (byte)0xee, (byte)0x99
};

// Iteration count
int count = 20;

// Create PBE parameter set
pbeParamSpec = new PBEParameterSpec(salt, count);

// Prompt user for encryption password.
// Collect user password as char array (using the
// "readPasswd" method from above), and convert
// it into a SecretKey object, using a PBE key
// factory.
System.out.print("Enter encryption password: ");
System.out.flush();
pbeKeySpec = new PBEKeySpec(readPasswd(System.in));
keyFac = SecretKeyFactory.getInstance("PBEWithMD5AndDES");
SecretKey pbeKey = keyFac.generateSecret(pbeKeySpec);

// Create PBE Cipher
Cipher pbeCipher = Cipher.getInstance("PBEWithMD5AndDES");

// Initialize PBE Cipher with key and parameters
pbeCipher.init(Cipher.ENCRYPT_MODE, pbeKey, pbeParamSpec);

// Our cleartext
byte[] cleartext = "This is another example".getBytes();

// Encrypt the cleartext
byte[] ciphertext = pbeCipher.doFinal(cleartext);
javapro 2003-11-22
  • 打赏
  • 举报
回复
哪个高手能够解答?请告诉小弟,感激不尽

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧