Java实现DESede加密

package JiaMi;
import sun.misc.BASE64Encoder;
import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESedeKeySpec;
/**
 * @author mianma
 * @create 2023-06-08 16:39:45
 */
public class DESede {
    public static void main(String[] args) throws Exception{
        DESedeKeySpec dks = new DESedeKeySpec("123456781234567812345678".getBytes());  
        SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DESede"); 
        SecretKey secretKey = keyFactory.generateSecret(dks);  /
        Cipher cipher = Cipher.getInstance("DESede");
        cipher.init(Cipher.ENCRYPT_MODE,secretKey);  
        byte[] res = cipher.doFinal("a12345678".getBytes()); 
        System.out.println(new BASE64Encoder().encodeBuffer(res));
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Spring Security本身并不提供DESede加密算法实现,但你可以使用Java的标准库来实现。下面是一个示例代码,演示了如何在Spring Security中使用DESede进行加密。 首先,你需要创建一个自定义的PasswordEncoder类来实现加密和解密方法。你可以使用`javax.crypto`包中的`Cipher`类来执行加密和解密操作。下面是一个简单的示例: ```java import org.springframework.security.crypto.password.PasswordEncoder; import javax.crypto.Cipher; import javax.crypto.SecretKey; import javax.crypto.SecretKeyFactory; import javax.crypto.spec.DESedeKeySpec; import java.security.spec.KeySpec; import java.util.Base64; public class MyPasswordEncoder implements PasswordEncoder { private static final String ENCRYPTION_ALGORITHM = "DESede"; private static final String SECRET_KEY = "your_secret_key"; // 自定义的密钥 @Override public String encode(CharSequence rawPassword) { try { Cipher cipher = getCipher(Cipher.ENCRYPT_MODE); byte[] encryptedBytes = cipher.doFinal(rawPassword.toString().getBytes()); return Base64.getEncoder().encodeToString(encryptedBytes); } catch (Exception e) { throw new RuntimeException("Error occurred while encoding password", e); } } @Override public boolean matches(CharSequence rawPassword, String encodedPassword) { try { Cipher cipher = getCipher(Cipher.DECRYPT_MODE); byte[] decryptedBytes = cipher.doFinal(Base64.getDecoder().decode(encodedPassword)); String decryptedPassword = new String(decryptedBytes); return rawPassword.toString().equals(decryptedPassword); } catch (Exception e) { throw new RuntimeException("Error occurred while decoding password", e); } } private Cipher getCipher(int cipherMode) throws Exception { KeySpec keySpec = new DESedeKeySpec(SECRET_KEY.getBytes()); SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(ENCRYPTION_ALGORITHM); SecretKey key = keyFactory.generateSecret(keySpec); Cipher cipher = Cipher.getInstance(ENCRYPTION_ALGORITHM); cipher.init(cipherMode, key); return cipher; } } ``` 然后在Spring Security的配置类中,将自定义的PasswordEncoder实例注入到AuthenticationManagerBuilder中: ```java import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Configuration; import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder; import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter; @Configuration public class SecurityConfig extends WebSecurityConfigurerAdapter { @Autowired private MyPasswordEncoder passwordEncoder; @Override protected void configure(AuthenticationManagerBuilder auth) throws Exception { auth.inMemoryAuthentication() .passwordEncoder(passwordEncoder) .withUser("user") .password("your_encrypted_password") // 加密后的密码 .roles("USER"); } // 其他配置... } ``` 这样,你就可以在Spring Security中使用自定义的DESede加密算法加密和验证密码了。请注意,为了安全起见,你应该将密钥存储在安全的地方,并且不要直接将明文密码存储在代码中。此示例仅用于演示目的,实际应用中请根据具体需求进行安全处理。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值