• PUDN用户
    了解作者
  • Java
    开发工具
  • 2KB
    文件大小
  • rar
    文件格式
  • 0
    收藏次数
  • 1 积分
    下载积分
  • 43
    下载次数
  • 2005-10-16 14:58
    上传日期
一个基于java平台的DES加密解密算法,key保存在文件中,KeyManager.java是该加密解密算法的使用范例
javaCipher.rar
  • www.pudn.com.txt
    218B
  • MyCipher.java
    4.8KB
  • KeyManager.java
    2.3KB
内容介绍
package tywy; import java.io.Serializable; import java.security.InvalidKeyException; import java.security.NoSuchAlgorithmException; import javax.crypto.BadPaddingException; import javax.crypto.Cipher; import javax.crypto.IllegalBlockSizeException; import javax.crypto.KeyGenerator; import javax.crypto.NoSuchPaddingException; import javax.crypto.SecretKey; /* * Created on 2005-5-14 * * TODO To change the template for this generated file go to * Window - Preferences - Java - Code Style - Code Templates */ /** * @author Administrator * * TODO To change the template for this generated type comment go to * Window - Preferences - Java - Code Style - Code Templates */ public class MyCipher implements Serializable { /** * Comment for <code>serialVersionUID</code> */ private static final long serialVersionUID = 1L; private SecretKey desKey = null; MyCipher() { try { KeyGenerator keygen = KeyGenerator.getInstance("DES"); desKey = keygen.generateKey(); } catch (NoSuchAlgorithmException e) { // TODO Auto-generated catch block e.printStackTrace(); } } public SecretKey getSecretKey() { return desKey; } private String bytes2hex(byte[] bytes) { StringBuffer buf = new StringBuffer(bytes.length * 2); for (int pos = 0; pos < bytes.length; pos++) { int n = bytes[pos] & 0xFF; buf.append(halfByte2hex((n >> 4) & 0xF)); buf.append(halfByte2hex(n & 0xF)); } String rv = buf.toString(); return rv; } private char halfByte2hex(int n) { //ensure: 0 <= n <= 15 return (char) (n > 9 ? 'A' + n - 10 : '0' + n); } private int hex2halfByte(char c) { switch (c) { case '0': return 0; case '1': return 1; case '2': return 2; case '3': return 3; case '4': return 4; case '5': return 5; case '6': return 6; case '7': return 7; case '8': return 8; case '9': return 9; case 'A': return 10; case 'B': return 11; case 'C': return 12; case 'D': return 13; case 'E': return 14; case 'F': return 15; default: System.out.println("char out of range when decrypt"); } return -1; } private byte[] hex2bytes(String str) { byte[] bt = new byte[str.length() / 2]; StringBuffer buf = new StringBuffer(str.length()); for (int i = 0; i < str.length(); i++) { buf.append(str.charAt(i)); } for (int i = 0; i < str.length() / 2; i++) { int gao = -1, di = -1; gao = hex2halfByte(buf.charAt(2 * i)); di = hex2halfByte(buf.charAt(2 * i + 1)); bt[i] = (byte) ((gao << 4 | di) & 0xff); } return bt; } public String enCryptString(String clearMessage) { String cipherMessage = null; Cipher desCipher = null; try { desCipher = Cipher.getInstance("DES"); } catch (NoSuchAlgorithmException e2) { // TODO Auto-generated catch block e2.printStackTrace(); } catch (NoSuchPaddingException e2) { // TODO Auto-generated catch block e2.printStackTrace(); } try { //初始化cipher用来加密 desCipher.init(Cipher.ENCRYPT_MODE, desKey); } catch (InvalidKeyException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } try { //加密 byte[] clearText = clearMessage.getBytes(); byte[] cipherText = desCipher.doFinal(clearText); cipherMessage = bytes2hex(cipherText); } catch (IllegalStateException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IllegalBlockSizeException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (BadPaddingException e) { // TODO Auto-generated catch block e.printStackTrace(); } return cipherMessage; } public String decryptString(String cipherMessage) { String clearMessage = null; Cipher desCipher = null; try { desCipher = Cipher.getInstance("DES"); } catch (NoSuchAlgorithmException e2) { // TODO Auto-generated catch block e2.printStackTrace(); } catch (NoSuchPaddingException e2) { // TODO Auto-generated catch block e2.printStackTrace(); } try { //初始化cipher用来解密 desCipher.init(Cipher.DECRYPT_MODE, desKey); } catch (InvalidKeyException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } try { byte[] cipherText = hex2bytes(cipherMessage); byte[] clearText = desCipher.doFinal(cipherText); clearMessage = new String(clearText); } catch (IllegalStateException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IllegalBlockSizeException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (BadPaddingException e) { // TODO Auto-generated catch block e.printStackTrace(); } return clearMessage; } public static void main(String[] args) { } }
评论
    相关推荐
    • 加密解密.zip
      通过Python简单实现md5加密,aes加密解密,加密文本text必须为16的倍数
    • 加密解密源程序
      加密解密程序。C++编写 Encryption and decryption program
    • 加密解密套件
      包括很多得加密/解密控件,如des,3des,md5等等约10多个。我用了很好用,demo可以到http://www.cityinthesky.co.uk/ 下载
    • keyczar 加密 解密
      加密 解密 python aes des md5
    • iOS RSA加密解密
      RSA基本原理 公钥(public key): 用于加密数据. 用于公开, 一般存放在数据提供方, 例如iOS客户端. ... demo 中实现了公钥加密和私钥解密,对于服务器给了公钥,直接公钥加密后上传就ok,楼主亲测,不需要生成证书.
    • 加密解密程序
      加密解密程序。C++编写 Encryption and decryption program
    • DES加密解密
      java 实现的DES加解密算法,支持3DES加解密,密文以十六进制,二进制显示.rar
    • rsa加密解密
      RSA加密解密,html+css+js编写,如果有不懂的话可以私我。很基础
    • AES加密解密算法
      实现的AES加密解密算法,高级加密标准(AES,Advanced Encryption Standard)为最常见的对称加密算法(微信小程序加密传输就是用这个加密算法的)。对称加密算法也就是加密和解密用相同的密钥
    • 加密解密C语言
      大学生软件设计报告 南京邮电大学 加密解密 C语言