一次性密码 (OTP) C# 代码到 Java 代码转换

发布于 2024-11-14 20:29:10 字数 2110 浏览 3 评论 0原文

去年,我用 C# 编写了一个一次性密码 (OTP) 生成器。现在我需要在 Java 中使用 OTP 生成器,但我在 Java 中找不到等效的函数。

这是我去年写的代码:(我知道这个 OTP 的安全性很低,但我不需要防弹的)

SHA1CryptoServiceProvider hash = new SHA1CryptoServiceProvider(); //first hash with sha1
byte[] hashPass = hash.ComputeHash(Encoding.ASCII.GetBytes(pass)); //pass is entered by user
HMACMD5 hma = new HMACMD5(hashPass); // use the hashed value as a key to hmac
OTPass = hma.ComputeHash(Encoding.ASCII.GetBytes(email + Counter(email)));// generate OTPass, Counter(email) is the counter of the user taken from database
increaseCounter(email); // updating the counter
this.SetLog(this.GetLog() + Environment.NewLine + "OTPass Generated: " + BitConverter.ToString(OTPass)); // OTP

这是我尝试将 C# 转换成的 Java 代码:(这只是 SHA1 部分,我找不到如何用 Java 编写 HMAC-MD5)

import java.io.*;
import java.security.*;

public class otp {

/**
 * @param args
 * @throws IOException 
 */
public static void main(String[] args) throws IOException {

    System.out.println("Please enter your username:");
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    String username = br.readLine();
    System.out.println("Please enter your password:");
    String password = br.readLine();

    try {
         MessageDigest md = MessageDigest.getInstance("SHA1");

         String input = password;
         md.update(input.getBytes()); 
         byte[] output = md.digest();
         System.out.println();
         System.out.println("SHA1(\""+input+"\") =");
         System.out.println("   "+bytesToHex(output));


      } catch (Exception e) {
         System.out.println("Exception: "+e);
      }
   }
   public static String bytesToHex(byte[] b) {
      char hexDigit[] = {'0', '1', '2', '3', '4', '5', '6', '7',
                         '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};
      StringBuffer buf = new StringBuffer();
      for (int j=0; j<b.length; j++) {
         buf.append(hexDigit[(b[j] >> 4) & 0x0f]);
         buf.append(hexDigit[b[j] & 0x0f]);
      }
      return buf.toString();        
}
}

感谢帮助

I wrote an One-Time-Password (OTP) generator in C# last year. Now I need to use an OTP generator in Java but I couldn't find the equivalent functions in Java.

Here is the code I wrote last year: (I know this OTP's security is low but I don't need a bullet-proof one)

SHA1CryptoServiceProvider hash = new SHA1CryptoServiceProvider(); //first hash with sha1
byte[] hashPass = hash.ComputeHash(Encoding.ASCII.GetBytes(pass)); //pass is entered by user
HMACMD5 hma = new HMACMD5(hashPass); // use the hashed value as a key to hmac
OTPass = hma.ComputeHash(Encoding.ASCII.GetBytes(email + Counter(email)));// generate OTPass, Counter(email) is the counter of the user taken from database
increaseCounter(email); // updating the counter
this.SetLog(this.GetLog() + Environment.NewLine + "OTPass Generated: " + BitConverter.ToString(OTPass)); // OTP

Here is the Java code I tried to convert C# into: (This is just the SHA1 part, I couldn't find how to write HMAC-MD5 in Java)

import java.io.*;
import java.security.*;

public class otp {

/**
 * @param args
 * @throws IOException 
 */
public static void main(String[] args) throws IOException {

    System.out.println("Please enter your username:");
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    String username = br.readLine();
    System.out.println("Please enter your password:");
    String password = br.readLine();

    try {
         MessageDigest md = MessageDigest.getInstance("SHA1");

         String input = password;
         md.update(input.getBytes()); 
         byte[] output = md.digest();
         System.out.println();
         System.out.println("SHA1(\""+input+"\") =");
         System.out.println("   "+bytesToHex(output));


      } catch (Exception e) {
         System.out.println("Exception: "+e);
      }
   }
   public static String bytesToHex(byte[] b) {
      char hexDigit[] = {'0', '1', '2', '3', '4', '5', '6', '7',
                         '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};
      StringBuffer buf = new StringBuffer();
      for (int j=0; j<b.length; j++) {
         buf.append(hexDigit[(b[j] >> 4) & 0x0f]);
         buf.append(hexDigit[b[j] & 0x0f]);
      }
      return buf.toString();        
}
}

Thanks for help

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

笨死的猪 2024-11-21 20:29:10

我一直使用 BouncyCastle

你可以看看其中的一些页面:

BouncyCastle HMac

BouncyCastle 规范

或者坚持使用 Java 6:

Mac hmacMd5 = Mac.getInstance("HMACMD5");

I have always used BouncyCastle

You can have a look at a few of these pages:

BouncyCastle HMac

BouncyCastle Specs

Or to stick with Java 6:

Mac hmacMd5 = Mac.getInstance("HMACMD5");
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文