微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

我在哪里可以找到Vigenere密码的Java源代码?

在我的应用程序中,我想实现一些加密.因此我需要Vigenere密码的代码.有谁知道我在哪里可以找到 Java的源代码

解决方法

这是Vigenere密码类,你可以使用它,只需调用加密和解密函数
代码来自 Rosetta Code.
public class VigenereCipher {
    public static void main(String[] args) {
        String key = "VIGENERECIPHER";
        String ori = "Beware the Jabberwock,my son! The jaws that bite,the claws that catch!";
        String enc = encrypt(ori,key);
        System.out.println(enc);
        System.out.println(decrypt(enc,key));
    }

    static String encrypt(String text,final String key) {
        String res = "";
        text = text.toupperCase();
        for (int i = 0,j = 0; i < text.length(); i++) {
            char c = text.charat(i);
            if (c < 'A' || c > 'Z') continue;
            res += (char)((c + key.charat(j) - 2 * 'A') % 26 + 'A');
            j = ++j % key.length();
        }
        return res;
    }

    static String decrypt(String text,j = 0; i < text.length(); i++) {
            char c = text.charat(i);
            if (c < 'A' || c > 'Z') continue;
            res += (char)((c - key.charat(j) + 26) % 26 + 'A');
            j = ++j % key.length();
        }
        return res;
    }
}

原文地址:https://www.jb51.cc/java/127302.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐