现在的位置: 首页 > 综合 > 正文

3DES – Java编程: 使用3Des(Triple-DES)

2013年12月01日 ⁄ 综合 ⁄ 共 583字 ⁄ 字号 评论关闭

3DES

默认为ECB加密, JAVA中该算法名为 DESede,每次加密8字节

try
{
    String password = "123456780000111122223333";
    byte[] b = password.getBytes();

    // 必须是>=24字节的数组,否则返回InvalidKeyException
    DESedeKeySpec dks = new DESedeKeySpec(b);
    SecretKeyFactory keyFactory = SecretKeyFactory.getInstance( "DESede" );
    SecretKey key = keyFactory.generateSecret( dks );
    
    Cipher cipher = Cipher.getInstance("DESede");
    cipher.init(Cipher.ENCRYPT_MODE, key);
    
    String plain = "shaofa00";
    byte [] input = plain.getBytes();
    byte [] output = cipher.update(plain.getBytes());
    
    System.out.println("haha");
}
catch(Exception e)
{
    e.printStackTrace();
}

加密后的数据
50 F1 30 74 9B 20 32 60
-102 -17 14 -41 38 -88 90 -19

抱歉!评论已关闭.