`
leon1509
  • 浏览: 529553 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

Groovy DES加解密

阅读更多
import java.security.*
import javax.crypto.*
import javax.crypto.spec.*

class DESCodec {
    static encode = { String target ->
        def cipher = getCipher(Cipher.ENCRYPT_MODE)
        return cipher.doFinal(target.bytes).encodeBase64()
    }
    
    static decode = { String target ->
        def cipher = getCipher(Cipher.DECRYPT_MODE)
        return new String(cipher.doFinal(target.decodeBase64()))
    }
    
    private static getCipher(mode) {
        def keySpec = new DESKeySpec(getPassword())
        def cipher = Cipher.getInstance("DES")
        def keyFactory = SecretKeyFactory.getInstance("DES")
        cipher.init(mode, keyFactory.generateSecret(keySpec))
        return cipher
    }
    
    private static getPassword() { "secret12".getBytes("UTF-8") }
    
    static void main(args) {
        def strmi=encode("asdtiang")
//println decode("asdtiang")
//DESCodec.decode strmi
            println decode("${strmi}")////这个地主一定要这样写,由于理论不深入,具体为什么还不明白,写成strmi 是不正确的。
                                       ///或者写成strmi.toString()也行,不过还没实践过。
        
    }
}
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics