devin_lxs 2015-11-19 02:38 采纳率: 33.3%
浏览 1384

android des加密后别人如何打开?

我的代码是,我已经加密了,生成的文件发给别人解不开,为什么呢?发现每次的key都是不一样的,怎么让我们两个的key保持一致?
我的代码:
——————————————————————
public class Locker {
Key key;
public Locker(String str) {
getKey(str);//生成密匙
}
/**
* 根据参数生成KEY
*/
public void getKey(String strKey) {
try {
KeyGenerator _generator = KeyGenerator.getInstance("DES");
_generator.init(new SecureRandom(strKey.getBytes()));
this.key = _generator.generateKey();
_generator = null;
} catch (Exception e) {
throw new RuntimeException("Error initializing SqlMap class. Cause: " + e);
}
}

  /** 
  * 文件file进行加密并保存目标文件destFile中 
  * 
  * @param file   要加密的文件 如mnt/sdcard/PateokeyNormal.txt 
  * @param destFile 加密后存放的文件名 如mnt/sdcard/Pateokey.txt 
  */ 
  public void encrypt(String file, String destFile) throws Exception { 
    Cipher cipher = Cipher.getInstance("DES"); 
    // cipher.init(Cipher.ENCRYPT_MODE, getKey()); 
    cipher.init(Cipher.ENCRYPT_MODE, this.key); 
    InputStream is = new FileInputStream(file); 
    OutputStream out = new FileOutputStream(destFile); 
    CipherInputStream cis = new CipherInputStream(is, cipher); 
    byte[] buffer = new byte[1024]; 
    int r; 
    while ((r = cis.read(buffer)) > 0) { 
        out.write(buffer, 0, r); 
    } 
    System.out.println("KEY加密="+key);
    cis.close(); 
    is.close(); 
    out.close(); 
  } 

}
别人的代码:
——————————————————————
public class Locker {
Key key;
public Locker(String str) {
getKey(str);//生成密匙
}
/**
* 根据参数生成KEY
*/
public void getKey(String strKey) {
try {
KeyGenerator _generator = KeyGenerator.getInstance("DES");
_generator.init(new SecureRandom(strKey.getBytes()));
this.key = _generator.generateKey();
_generator = null;
} catch (Exception e) {
throw new RuntimeException("Error initializing SqlMap class. Cause: " + e);
}
}

  /** 
  * 文件采用DES算法解密文件 
  * 
  * @param file 已加密的文件 如auth/Pateokey.txt 
  *  
  */ 
  public String decrypt(String file) throws Exception {
      System.out.println("KEY解密="+key);
        Cipher cipher = Cipher.getInstance("DES"); 
        cipher.init(Cipher.DECRYPT_MODE, this.key); 

        InputStream is = new FileInputStream(file); 
        String PateoMessage = "";
        OutputStream out =System.out; 

        //  start
            ByteArrayOutputStream baos = new ByteArrayOutputStream();  
              CipherOutputStream cos = new CipherOutputStream(baos, cipher); 
        //  end
    //    CipherOutputStream cos = new CipherOutputStream(out, cipher); 


        byte[] buffer = new byte[1024]; 
        int r; 
        while ((r = is.read(buffer)) >= 0) { 
            System.out.println("正在解密");
             cos.write(buffer, 0, r);              
        } 

        cos.close(); 
        //out.close(); 
        is.close();
        PateoMessage=baos.toString();
        baos.close();
       System.out.println("正在解密PateoMessage="+PateoMessage);
        return PateoMessage;
  } 

}

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
    • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
    • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
    • ¥50 成都蓉城足球俱乐部小程序抢票
    • ¥15 yolov7训练自己的数据集
    • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)
    • ¥15 电力市场出清matlab yalmip kkt 双层优化问题
    • ¥30 ros小车路径规划实现不了,如何解决?(操作系统-ubuntu)
    • ¥20 matlab yalmip kkt 双层优化问题
    • ¥15 如何在3D高斯飞溅的渲染的场景中获得一个可控的旋转物体