3des java 库_java 3DES 加密

public class DESCode {

private String algorithm = "DESede/CBC/PKCS7Padding";//加密方法/运算模式/填充模式

private String charset = "UTF-8";//编码

private Cipher encCipher;//加密cipher

private Cipher decCipher;//解密cipher

private String encStrKey =null;//密钥

private String encSourceSpliter="$";

private String hashSourceSpliter="_";

private MessageDigest digest;//计算摘要对象

private KeyGenerator keyGene;

public String getKey()

{

return this.keyGene.getKey();

}

public DESCode(KeyGenerator keyGene)throws NoSuchAlgorithmException,

NoSuchPaddingException, InvalidKeyException,

InvalidAlgorithmParameterException, DecoderException {

Security

.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider());//添加jce支持(sun有其默认实现)

this.keyGene = keyGene;

digest=MessageDigest.getInstance("MD5");

this.encStrKey = keyGene.getEncKeyStr();

init();

}

private void init() throws NoSuchAlgorithmException,

NoSuchPaddingException, InvalidKeyException,

InvalidAlgorithmParameterException, DecoderException {

encCipherInit();

decCipherInit();

}

private void encCipherInit() throws NoSuchAlgorithmException,

NoSuchPaddingException, InvalidKeyException,

InvalidAlgorithmParameterException, DecoderException {

DESedeKeySpec encKey = new DESedeKeySpec(Hex.decodeHex(this.encStrKey

.toCharArray()));

SecretKey secrekey = new SecretKeySpec(encKey.getKey(), algorithm);

this.encCipher = Cipher.getInstance(this.algorithm);

IvParameterSpec iv = new IvParameterSpec(new byte[] { 1, 2, 3, 4, 5, 6,

7, 8 });//iv向量(加密解密双方要一致)

encCipher.init(Cipher.ENCRYPT_MODE, secrekey, iv);//初始化cipher对象

}

private void decCipherInit() throws InvalidKeyException, DecoderException,

NoSuchAlgorithmException, NoSuchPaddingException,

InvalidAlgorithmParameterException {

DESedeKeySpec encKey = new DESedeKeySpec(Hex.decodeHex(this.encStrKey

.toCharArray()));

SecretKey secrekey = new SecretKeySpec(encKey.getKey(), algorithm);

this.decCipher = Cipher.getInstance(this.algorithm);

IvParameterSpec iv = new IvParameterSpec(new byte[] { 1, 2, 3, 4, 5, 6,

7, 8 });

decCipher.init(Cipher.DECRYPT_MODE, secrekey, iv);//初始化cipher对象

}

public String encrypt(String sourceMsg)//加密数据

throws UnsupportedEncodingException, IllegalBlockSizeException,

BadPaddingException, InvalidKeyException, NoSuchAlgorithmException, NoSuchPaddingException, InvalidAlgorithmParameterException, DecoderException {

if(!this.encStrKey.equals(this.keyGene.getEncKeyStr()))

{

this.encStrKey = keyGene.getEncKeyStr();

init();

}

byte[] msg = sourceMsg.getBytes(this.charset);

byte[] value = this.encCipher.doFinal(msg);

return new String(Base64.encodeBase64(value));

}

public String decrypt(String msgStr) throws IllegalBlockSizeException,

BadPaddingException, UnsupportedEncodingException, InvalidKeyException, NoSuchAlgorithmException, NoSuchPaddingException, InvalidAlgorithmParameterException, DecoderException {//解密数据

if(!this.encStrKey.equals(this.keyGene.getEncKeyStr()))

{

this.encStrKey = keyGene.getEncKeyStr();

init();

}

byte[] temp = Base64.decodeBase64(msgStr);

byte[] value = this.decCipher.doFinal(temp);

String result = new String(value, this.charset);

return URLDecoder.decode(result, this.charset);

}

public String md5EnCode(String src,boolean replace)

{

if(replace)

{

src=src.replace(this.encSourceSpliter, this.hashSourceSpliter);

}

return Hex.encodeHexString(digest.digest(src.getBytes()));

}

public String urlEncode(String... str) throws UnsupportedEncodingException {

StringBuilder sb = new StringBuilder();

for (String s : str) {

sb.append(URLEncoder.encode(s, this.charset));

sb.append(this.encSourceSpliter);

}

int len = sb.length();

if (len > 0) {

sb.delete(len-1, len );

}

return sb.toString();

}

public static void main(String[] args) throws Exception {

DESCode descode = new DESCode(new KeyGenerator());

MessageDigest dig = MessageDigest.getInstance("MD5");

String msg1 = "ybcola$"

+ Hex.encodeHexString(dig.digest("pppppppp".getBytes()))

+ "$www.yltch.net";

String msg2 = descode.urlEncode("ybcola", Hex.encodeHexString(dig

.digest("pppppppp".getBytes())), "www.yltch.net");

String msg3 = descode.urlEncode("弟弟", "1000", "7", "根据推广逻辑,增加的奖励",

"dz_02156356548", "www.yltch.net");

System.out.println(msg1);

System.out.println(msg2);

System.out.println(msg3);

System.out.println("-------encode------");

msg1= descode.encrypt(msg1);

msg2= descode.encrypt(msg2);

msg3= descode.encrypt(msg3);

System.out.println(msg1);

System.out.println(msg2);

System.out.println(msg3);

System.out.println("------decode--------");

// String msg4 =

// "hOWzn/a38v2idQegaPnBUqJL3jsF9uh8OBSQ+LKoCeXGNkXX2ceCIn14g6FbFF4StrBoOfBH6UPRoHvO/Ftwp3qL4FHf/1+3aCU3K0esqqQ=";

System.out.println(descode.decrypt(msg1));

System.out.println(descode.decrypt(msg2));

System.out.println(descode.decrypt(msg3));

}

}

public class KeyGenerator {

private String key="hongsoft";

private String encKeyStr="9F8C243AEE347183B39DD81B20941E86BC11529B034C8842";

public String getEncKeyStr() {

return encKeyStr;

}

public void setEncKeyStr(String encKeyStr) {

this.encKeyStr = encKeyStr;

}

public String getKey() {

return key;

}

public void setKey(String key) {

this.key = key;

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
#ifdef __cplusplus extern "C" { #endif /** * \brief DES context structure */ typedef struct { unsigned long esk[32]; /*!< DES encryption subkeys */ unsigned long dsk[32]; /*!< DES decryption subkeys */ } des_context; /** * \brief Triple-DES context structure */ typedef struct { unsigned long esk[96]; /*!< Triple-DES encryption subkeys */ unsigned long dsk[96]; /*!< Triple-DES decryption subkeys */ } des3_context; /** * \brief DES key schedule (56-bit) * * \param ctx DES context to be initialized * \param key 8-byte secret key */ void my_des_set_key( des_context *ctx, unsigned char key[8] ); /** * \brief DES block encryption (ECB mode) * * \param ctx DES context * \param input plaintext block * \param output ciphertext block */ void des_encrypt( des_context *ctx, unsigned char input[8], unsigned char output[8] ); /** * \brief DES block decryption (ECB mode) * * \param ctx DES context * \param input ciphertext block * \param output plaintext block */ void des_decrypt( des_context *ctx, unsigned char input[8], unsigned char output[8] ); /** * \brief DES-CBC buffer encryption * * \param ctx DES context * \param iv initialization vector (modified after use) * \param input buffer holding the plaintext * \param output buffer holding the ciphertext * \param len length of the data to be encrypted */ void my_des_cbc_encrypt( des_context *ctx, unsigned char iv[8], unsigned char *input, unsigned char *output, int len ); /** * \brief DES-CBC buffer decryption * * \param ctx DES context * \param iv initialization vector (modified after use) * \param input buffer holding the ciphertext * \param output buffer holding the plaintext * \param len length of the data to be decrypted */ void my_des_cbc_decrypt( des_context *ctx, unsigned char iv[8], unsigned char *input, unsigned char *output, int len ); /** * \brief Triple-DES key schedule (112-bit) * * \param ctx 3DES context to be initialized * \param key 16-byte secret key */ void des3_set_2keys( des3_context *ctx, unsigned char key[16] ); /** * \brief Triple-DES key schedule (168-bit) * * \param ctx 3DES context to be initialized * \param key 24-byte secret key */ void des3_set_3keys( des3_context *ctx, unsigned char key[24] ); /** * \brief Triple-DES block encryption (ECB mode) * * \param ctx 3DES context * \param input plaintext block * \param output ciphertext block */ void des3_encrypt( des3_context *ctx, unsigned char input[8], unsigned char output[8] ); /** * \brief Triple-DES block decryption (ECB mode) * * \param ctx 3DES context * \param input ciphertext block * \param output plaintext block */ void des3_decrypt( des3_context *ctx, unsigned char input[8], unsigned char output[8] ); /** * \brief 3DES-CBC buffer encryption * * \param ctx 3DES context * \param iv initialization vector (modified after use) * \param input buffer holding the plaintext * \param output buffer holding the ciphertext * \param len length of the data to be encrypted */ void des3_cbc_encrypt( des3_context *ctx, unsigned char iv[8], unsigned char *input, unsigned char *output, int len ); /** * \brief 3DES-CBC buffer decryption * * \param ctx 3DES context * \param iv initialization vector (modified after use) * \param input buffer holding the ciphertext * \param output buffer holding the plaintext * \param len length of the data to be decrypted */ void des3_cbc_decrypt( des3_context *ctx, unsigned char iv[8], unsigned char *input, unsigned char *output, int len ); /* * \brief Checkup routine * * \return 0 if successful, or 1 if the test failed */ int des_self_test( void ); #ifdef __cplusplus } #endif #endif /* des.h */

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值