美文网首页
在Linux下运行引入了外部jar包的java程序

在Linux下运行引入了外部jar包的java程序

作者: zlchen | 来源:发表于2020-12-01 19:17 被阅读0次
    编译:

    javac -cp commons-codec-1.15.jar CryptAES.java

    或者 javac -classpath commons-codec-1.15.jar CryptAES.java

    运行:

    java -cp $CLASSPATH:commons-codec-1.15.jar CryptAES

    或者 java -classpath $CLASSPATH:commons-codec-1.15.jar CryptAES

    [root@iZj6c0dk32hfzgpetzxlcrZ ~]# cd java
    [root@iZj6c0dk32hfzgpetzxlcrZ java]# ls
    commons-codec-1.15.jar  commons-codec-1.1.jar  CryptAES.class  CryptAES.java  Demo.class  Demo.java  Jpro2_1.class  Jpro2_1.java
    [root@iZj6c0dk32hfzgpetzxlcrZ java]# javac -cp commons-codec-1.15.jar CryptAES.java
    [root@iZj6c0dk32hfzgpetzxlcrZ java]# java -classpath $CLASSPATH:commons-codec-1.15.jar CryptAES
    fhTD0NNIzv4jUEhJuC1htFFXJ/4S/rL6tDCJPiNvJ8mVLHWOD0HWweuxHynxoZf9
    this is a string will be AES_Encrypt
    [root@iZj6c0dk32hfzgpetzxlcrZ java]#
    

    示例代码:

    import java.security.Key; 
    import javax.crypto.Cipher; 
    import javax.crypto.spec.SecretKeySpec; 
     
    import org.apache.commons.codec.binary.Base64;
    
      
    public class CryptAES { 
      
        private static final String AESTYPE ="AES/ECB/PKCS5Padding"; 
     
        public static String AES_Encrypt(String keyStr, String plainText) { 
            byte[] encrypt = null; 
            try{ 
                Key key = generateKey(keyStr); 
                Cipher cipher = Cipher.getInstance(AESTYPE); 
                cipher.init(Cipher.ENCRYPT_MODE, key); 
                encrypt = cipher.doFinal(plainText.getBytes());     
            }catch(Exception e){ 
                e.printStackTrace(); 
            }
            return new String(Base64.encodeBase64(encrypt)); 
        } 
     
        public static String AES_Decrypt(String keyStr, String encryptData) {
            byte[] decrypt = null; 
            try{ 
                Key key = generateKey(keyStr); 
                Cipher cipher = Cipher.getInstance(AESTYPE); 
                cipher.init(Cipher.DECRYPT_MODE, key); 
                decrypt = cipher.doFinal(Base64.decodeBase64(encryptData)); 
            }catch(Exception e){ 
                e.printStackTrace(); 
            } 
            return new String(decrypt).trim(); 
        } 
     
        private static Key generateKey(String key)throws Exception{ 
            try{            
                SecretKeySpec keySpec = new SecretKeySpec(key.getBytes(), "AES"); 
                return keySpec; 
            }catch(Exception e){ 
                e.printStackTrace(); 
                throw e; 
            } 
     
        } 
     
        public static void main(String[] args) { 
             
            String keyStr = "UITN25LMUQC436IM";  
     
            String plainText = "this is a string will be AES_Encrypt";
             
            String encText = AES_Encrypt(keyStr, plainText);
            String decString = AES_Decrypt(keyStr, encText); 
             //  fhTD0NNIzv4jUEhJuC1htFFXJ/4S/rL6tDCJPiNvJ8mVLHWOD0HWweuxHynxoZf9
            System.out.println(encText); 
            //  this is a string will be AES_Encrypt
            System.out.println(decString); 
     
        } 
    }
    

    相关文章

      网友评论

          本文标题:在Linux下运行引入了外部jar包的java程序

          本文链接:https://www.haomeiwen.com/subject/tlzswktx.html