Java 在 Selenium Webdriver 中加密和解密密码

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25975301/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 01:37:07  来源:igfitidea点击:

Encrypt and Decrypt passwords in Selenium Webdriver

javaseleniumencryptionselenium-webdriver

提问by user3834797

I am working on a simple test scenario which checks the successful login into facebook, with my own credentials.I want the password to be encrypted and login with the same. How can this be done? Some code snippet would be helpful.Would decryption also be required? I am looking for somewhat the same capability like QTP has. My code snippet is as below

我正在研究一个简单的测试场景,它使用我自己的凭据检查成功登录到 facebook 的情况。我希望密码被加密并使用相同的密码登录。如何才能做到这一点?一些代码片段会有所帮助。还需要解密吗?我正在寻找与 QTP 相同的功能。我的代码片段如下

    WebDriver driver = new FirefoxDriver();
    driver.get("https://www.facebook.com/");
    driver.findElement(By.id("email")).sendKeys("[email protected]");
    driver.findElement(By.id("pass")).sendKEys("encrypted password)//would want the encrypted pwd        

Pl help

请帮助

回答by Zach

I'm not a security expert but i think all you need to ensure is that the field is a password field using some attribute to check like type="Password" and if also when running HTTPS (Encryption)

我不是安全专家,但我认为您需要确保该字段是使用某些属性检查的密码字段,例如 type="Password" 以及是否在运行 HTTPS(加密)时

Hope this helps

希望这可以帮助

回答by Toni Ramchandani

This answer may be late but please try Apache's Base64 Class please try the below code for the same

这个答案可能会迟到,但请尝试 Apache 的 Base64 类,请尝试以下相同的代码

WebElement pwd = driver.findElement(By.id("j_password"));

byte[] encodedBytes = Base64.encodeBase64("Password".getBytes());
System.out.println("encodedBytes "+ new String(encodedBytes));

byte[] decodedBytes = Base64.decodeBase64(encodedBytes);
System.out.println("decodedBytes "+ new String(decodedBytes));

pwd.sendKeys(new String(encodedBytes));

Hope this will work ....

希望这会奏效......

回答by MaFiA

If you are looking for encryption and decryption mechanism in Java similar to what qtp provides, that is not possible unfortunately. In Java you can use 'Base64' class or some other mechanism too, to encrypt and decrypt the password, but anyone having access to your code can easily figure out original value for password. As a matter of fact, by using Base64 you only avoid storing the password in it's original form. If it's a question whether anyone can pull the original password, then answer is yes, the person having access to the code can easily do that. In qtp it's different, tool itself generates the encryption which only inbuilt libraries has access to and while decryption, only tool can decrypt that. Any person can not figure out original password. Thats a very strong feature qtp provides, when it comes to automation. I agree too. but un-fortunately it's not available in selenium.

如果您正在寻找类似于 qtp 提供的 Java 加密和解密机制,那么不幸的是,这是不可能的。在 Java 中,您也可以使用“Base64”类或其他一些机制来加密和解密密码,但是任何有权访问您的代码的人都可以轻松找出密码的原始值。事实上,通过使用 Base64,您只能避免以原始形式存储密码。如果是任何人都可以提取原始密码的问题,那么答案是肯定的,可以访问代码的人可以轻松做到这一点。在 qtp 中,它是不同的,工具本身会生成只有内置库才能访问的加密,而在解密时,只有工具才能对其进行解密。任何人都无法弄清楚原始密码。在自动化方面,这是 qtp 提供的一个非常强大的功能。我也同意。但不幸的是,它在硒中不可用。