一个用java写的加密解密程序


import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESKeySpec;

//import com.modernmedia.mw.tag.Messages;

import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;

public class Crypt {
// --------------------------------------------------------------------------------------------
// 获得密钥
public SecretKey getKey(String s) throws Exception {
//s ="g8TlgLEc6oqZxdwGe6pDiKB8Y";
System.out.println("s=="+s);
char[] ss = s.toCharArray();
String sss="";
    for(int i = 0;i<ss.length;i=i+2)
    {
    sss = sss + ss[i];
    }
SecretKeyFactory kf = SecretKeyFactory.getInstance("DES");
DESKeySpec ks = new DESKeySpec(sss.substring(0,8).getBytes());
SecretKey kd = kf.generateSecret(ks);
return kd;
}

// --------------------------------------------------------------------------------------------------
// 返回加密后的字符串
// key是用于生成密钥的字符串,input是要加密的字符串
public String getEncryptedString(String key, String input) {
String base64 = "";
try {
Cipher cipher = Cipher.getInstance("DES/ECB/PKCS5Padding");
cipher.init(Cipher.ENCRYPT_MODE, getKey(key));
System.out.print("getKey(key)==="+getKey(key)+"key=="+key);
byte[] inputBytes = input.getBytes("UTF8");
byte[] outputBytes = cipher.doFinal(inputBytes);
BASE64Encoder encoder = new BASE64Encoder();
base64 = encoder.encode(outputBytes);
} catch (Exception e) {
base64 = e.getMessage();
}
return base64;
}

// --------------------------------------------------------------------------------------------------
// 返回解密后的字符串
// key是用于生成密钥的字符串,input是要解密的字符串
public String getDecryptedString(String key, String input) {
String result = null;
try {
Cipher cipher = Cipher.getInstance("DES/ECB/PKCS5Padding");
cipher.init(Cipher.DECRYPT_MODE, getKey(key));
BASE64Decoder decoder = new BASE64Decoder();
byte[] raw = decoder.decodeBuffer(input);
byte[] stringBytes = cipher.doFinal(raw);
result = new String(stringBytes, "UTF8");
} catch (Exception e) {
result = e.getMessage();
}
return result;
}

public static void main(String[] args){
Crypt mycrypt = new Crypt();
try {
//SecretKey skey = mycrypt.getKey("g8TlgLEc6oqZxdwGe6pDiKB8Y");
String ss = mycrypt.getEncryptedString("6678912345678906", "胖子");
System.out.println("ss=="+ss);
String ss2 = mycrypt.getDecryptedString("6678912345678906",ss);
System.out.println("ss2=="+ss2);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

//String ss = Messages.getString("SendToMemberTag.5");
//System.out.print(ss);
 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,以下是一个基础的Java文件加密解密程序示例: ```java import java.io.*; import java.security.*; import javax.crypto.*; import javax.crypto.spec.*; public class FileEncryptorDecryptor { private Cipher ecipher; private Cipher dcipher; // 构造方法,传入密钥 public FileEncryptorDecryptor(SecretKey key) throws Exception { ecipher = Cipher.getInstance("AES"); dcipher = Cipher.getInstance("AES"); ecipher.init(Cipher.ENCRYPT_MODE, key); dcipher.init(Cipher.DECRYPT_MODE, key); } // 加密文件方法 public void encrypt(File inputFile, File outputFile) throws Exception { FileInputStream inputStream = new FileInputStream(inputFile); FileOutputStream outputStream = new FileOutputStream(outputFile); byte[] buffer = new byte[4096]; int bytesRead; while ((bytesRead = inputStream.read(buffer)) != -1) { byte[] encryptedBytes = ecipher.update(buffer, 0, bytesRead); outputStream.write(encryptedBytes); } byte[] encryptedBytes = ecipher.doFinal(); outputStream.write(encryptedBytes); inputStream.close(); outputStream.close(); } // 解密文件方法 public void decrypt(File inputFile, File outputFile) throws Exception { FileInputStream inputStream = new FileInputStream(inputFile); FileOutputStream outputStream = new FileOutputStream(outputFile); byte[] buffer = new byte[4096]; int bytesRead; while ((bytesRead = inputStream.read(buffer)) != -1) { byte[] decryptedBytes = dcipher.update(buffer, 0, bytesRead); outputStream.write(decryptedBytes); } byte[] decryptedBytes = dcipher.doFinal(); outputStream.write(decryptedBytes); inputStream.close(); outputStream.close(); } public static void main(String[] args) throws Exception { // 生成密钥 KeyGenerator keyGenerator = KeyGenerator.getInstance("AES"); keyGenerator.init(128); SecretKey key = keyGenerator.generateKey(); // 加密文件 FileEncryptorDecryptor encryptor = new FileEncryptorDecryptor(key); encryptor.encrypt(new File("input.txt"), new File("encrypted.bin")); // 解密文件 FileEncryptorDecryptor decryptor = new FileEncryptorDecryptor(key); decryptor.decrypt(new File("encrypted.bin"), new File("decrypted.txt")); } } ``` 这里使用了AES对称加密算法,可以根据需要选择不同的算法。需要注意的是,这里的密钥生成方式是随机生成的,实际应用需要使用更为安全的密钥生成方式。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值