JAVA代码翻译成C#

在与JAVA对接时,他们用的开源的Md5Crypt.md5Crypt加的密,以下为源码。
麻烦那位大神写个C#版本的。

public class Md5Crypt {

    /** The Identifier of the Apache variant. */

    static final String APR1_PREFIX = "$apr1$";



    /** The number of bytes of the final hash. */

    private static final int BLOCKSIZE = 16;



    /** The Identifier of this crypt() variant. */

    static final String MD5_PREFIX = "$1$";



    /** The number of rounds of the big loop. */

    private static final int ROUNDS = 1000;





public static String md5Crypt(final byte[] keyBytes, final String salt, final String prefix) {

        final int keyLen = keyBytes.length;



        // Extract the real salt from the given string which can be a complete hash string.

        String saltString;

        if (salt == null) {

            saltString = B64.getRandomSalt(8);

        } else {

            final Pattern p = Pattern.compile("^" + prefix.replace("$", "\\$") + "([\\.\\/a-zA-Z0-9]{1,8}).*");

            final Matcher m = p.matcher(salt);

            if (!m.find()) {

                throw new IllegalArgumentException("Invalid salt value: " + salt);

            }

            saltString = m.group(1);

        }

        final byte[] saltBytes = saltString.getBytes(Charsets.UTF_8);



        final MessageDigest ctx = DigestUtils.getMd5Digest();



        /*

         * The password first, since that is what is most unknown

         */

        ctx.update(keyBytes);



        /*

         * Then our magic string

         */

        ctx.update(prefix.getBytes(Charsets.UTF_8));



        /*

         * Then the raw salt

         */

        ctx.update(saltBytes);



        /*

         * Then just as many characters of the MD5(pw,salt,pw)

         */

        MessageDigest ctx1 = DigestUtils.getMd5Digest();

        ctx1.update(keyBytes);

        ctx1.update(saltBytes);

        ctx1.update(keyBytes);

        byte[] finalb = ctx1.digest();

        int ii = keyLen;

        while (ii > 0) {

            ctx.update(finalb, 0, ii > 16 ? 16 : ii);

            ii -= 16;

        }



        /*

         * Don't leave anything around in vm they could use.

         */

        Arrays.fill(finalb, (byte) 0);



        /*

         * Then something really weird...

         */

        ii = keyLen;

        final int j = 0;

        while (ii > 0) {

            if ((ii & 1) == 1) {

                ctx.update(finalb[j]);

            } else {

                ctx.update(keyBytes[j]);

            }

            ii >>= 1;

        }



        /*

         * Now make the output string

         */

        final StringBuilder passwd = new StringBuilder(prefix + saltString + "$");

        finalb = ctx.digest();



        /*

         * and now, just to make sure things don't run too fast On a 60 Mhz Pentium this takes 34 msec, so you would

         * need 30 seconds to build a 1000 entry dictionary...

         */

        for (int i = 0; i < ROUNDS; i++) {

            ctx1 = DigestUtils.getMd5Digest();

            if ((i & 1) != 0) {

                ctx1.update(keyBytes);

            } else {

                ctx1.update(finalb, 0, BLOCKSIZE);

            }



            if (i % 3 != 0) {

                ctx1.update(saltBytes);

            }



            if (i % 7 != 0) {

                ctx1.update(keyBytes);

            }



            if ((i & 1) != 0) {

                ctx1.update(finalb, 0, BLOCKSIZE);

            } else {

                ctx1.update(keyBytes);

            }

            finalb = ctx1.digest();

        }



        // The following was nearly identical to the Sha2Crypt code.

        // Again, the buflen is not really needed.

        // int buflen = MD5_PREFIX.length() - 1 + salt_string.length() + 1 + BLOCKSIZE + 1;

        B64.b64from24bit(finalb[0], finalb[6], finalb[12], 4, passwd);

        B64.b64from24bit(finalb[1], finalb[7], finalb[13], 4, passwd);

        B64.b64from24bit(finalb[2], finalb[8], finalb[14], 4, passwd);

        B64.b64from24bit(finalb[3], finalb[9], finalb[15], 4, passwd);

        B64.b64from24bit(finalb[4], finalb[10], finalb[5], 4, passwd);

        B64.b64from24bit((byte) 0, (byte) 0, finalb[11], 2, passwd);



        /*

         * Don't leave anything around in vm they could use.

         */

        // Is there a better way to do this with the JVM?

        ctx.reset();

        ctx1.reset();

        Arrays.fill(keyBytes, (byte) 0);

        Arrays.fill(saltBytes, (byte) 0);

        Arrays.fill(finalb, (byte) 0);



        return passwd.toString();

    }

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
一个很强大的工具, 能将c#代码片段、文件甚至工程直接转换成java代码,并能彻底解决外部引用的DLL问题,最强的是支持c#工程的直接转换,生成的Java代码质量也很不错。软件已破解,去除了未注册版最多只能转换1000行的限制,亲测可用!压缩包内含帮助文档,不过由于软件的使用很简单,帮助文档基本可以忽略。(小提示:如无法运行,请确认是否安装了.NET framework) 下面是一些英文介绍: C# to Java Converter features include: 1. Folder conversion: Entire folders of C# code files are converted to Java code files.(文件夹转换) 2. Code snippet and file conversion: Our snippet conversion accuracy is outstanding and does not require you to insert entire methods or classes. Heuristics are used to convert code fragments wit h missing declarations. (代码片段和文件转换) 3. Full support for the latest .NET code syntax: Generics and other newer C# features are converted where possible to the Java equivalent.(全面支持最新版的.NET语法) 4. Conversion options: You can specify numerous conversion options via our Options dialog. Code formatting options, custom type & member replacements, custom string replacements, and miscellaneous options. (可以指定转换规则) 5. File comparison feature: After converting a project or folder, you can compare original and converted files side-by-side. Comparing converted code to original code.(原代码与转换后代码的比较) 6. Running from the command line: C# to Java Converter can be launched directly for a specific project, folder, or file conversion from the command line. Command line.(命令行执行) 其他一些特点: 1. Converts all versions of C# code (2002, 2003, 2005, 2008, and 2010) (可以转换所有版本的C#代码) 2. Evaluates all referenced assemblies and .NET projects in order to resolve external references more completely.(能彻底解决外部引用的dll类库) 3. Converts C# ref parameters using Java generics 4. Superb conversions of all types of arrays 5. Handles the numerous coding alternatives and ambiguities of C# code 6. Flawless conversion of all aspects of inheritance and interfaces 7. Allows custom replacement of strings in the final converted code 8. Accurate even with poorly formatted C# code
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

m0_72429728

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值