zoukankan      html  css  js  c++  java
  • 凯撒密码(Java)

    事实上就是把每个字母偏移一下而已,并且字符不限于a-zA-z,可以是别的,那就很显而易见了,代码如下:
    定义一个Caesar密码类,成员变量只有密钥,也就是偏移量key

    代码如下:

    public class CaesarCrypto {
    
        private int key;
        
        public CaesarCrypto(int key) {
            // TODO Auto-generated constructor stub
            this.key = key;
        }
        
        public String getCipher(String plain) {
            char[] cipherChars = plain.toCharArray();
            for (int i = 0; i < cipherChars.length; i++) {
                cipherChars[i] = (char) (cipherChars[i] + this.key);
            }
            return new String(cipherChars);
        }
        
        public String getPlain(String cipher) {
            char[] cipherChars = cipher.toCharArray();
            for (int i = 0; i < cipherChars.length; i++) {
                cipherChars[i] = (char) (cipherChars[i] - this.key);
            }
            return new String(cipherChars);
        }
        
        public static void main(String[] args) {
            String text = "Java is the best language in the world!";
            System.out.println("明文:" + text);
            CaesarCrypto cc = new CaesarCrypto(4);
            String cipher = cc.getCipher(text);
            System.out.println("密文:" + cipher);
            System.out.println("解密" + cc.getPlain(cipher));
            
        }
    
    }

    结果如下:

    明文:Java is the best language in the world!
    密文:Neze$mw$xli$fiwx$perkyeki$mr$xli${svph%
    解密Java is the best language in the world!
  • 相关阅读:
    PCA本质和SVD
    特征工程(转载)
    python入门基础代码
    长尾理论
    金融行业数据分析
    [rancher-net]
    rancher中使用ingress-lbs做负载均衡
    python 高级语言特性
    docker从初识到深入
    关于容器技术的发展以及虚拟化技术的总结
  • 原文地址:https://www.cnblogs.com/zhaoke271828/p/13949951.html
Copyright © 2011-2022 走看看