获取WinSCP保存的密码

有时候大家会忘记WinSCP中保存的密码

下面用java提取WinSCP保存的密码

 

 

package com.sglyz.simple;

import java.util.*;

/**
 * @author sg
 */
public class DecodeWinSCP {

  public static final int PWALG_SIMPLE_FLAG = 0xFF;

  public static final char PWALG_SIMPLE_MAGIC = 0xA3;

  public static List<Character> fPassword = new ArrayList<>();

  public static void main(String[] args) {
    String hostName = "192.168.17.4";
    String userName = "root";
    String pwdStr =
      "A35C4A5F0460782E3333286D656E726D6A64726D6B72686D6E6F68696A6D2AB3A5FE7044DCAE7B1BFA2896052D79D9C276B6";
    Map<String, Object> result = getPassWord(hostName, userName, pwdStr);
    System.out.println(result);
  }

  /**
   * 获取密码
   *
   * @param hostName  用户名
   * @param userName  主机名
   * @param iniPwdStr WinSCP密码加密后的字符串
   * @return
   */
  public static Map<String, Object> getPassWord(
    String hostName,
    String userName,
    String iniPwdStr
  ) {
    for (int i = 0; i < iniPwdStr.length(); ++i) {
      fPassword.add((char) Integer.parseInt("" + iniPwdStr.charAt(i), 16));
    }
    Map<String, Object> map = new LinkedHashMap<>();
    String pwd = decryptPassword(fPassword, userName + hostName);
    map.put("主机名", hostName);
    map.put("用户名", userName);
    map.put("密码", pwd);
    return map;
  }

  /**
   * 解密
   *
   * @param password
   * @param unicodeKey
   * @return
   */
  private static String decryptPassword(
    List<Character> password,
    String unicodeKey
  ) {
    String key = unicodeKey;
    String result = "";
    char length, flag;

    flag = simpleDecryptNextChar(password);
    if (flag == PWALG_SIMPLE_FLAG) {
      /* Dummy = */
      simpleDecryptNextChar(password);
      length = simpleDecryptNextChar(password);
    } else length = flag;
    int newStart = ((int) simpleDecryptNextChar(password) * 2);
    removeItems(password, 0, newStart);

    for (int index = 0; index < length; ++index) result +=
      simpleDecryptNextChar(password);

    if (flag == PWALG_SIMPLE_FLAG) {
      if (!result.substring(0, key.length()).equals(key)) result =
        ""; else result = result.substring(key.length());
    }

    return result;
  }

  /**
   * unsigned char simpleDecryptNextChar(RawByteString &Str)
   * {
   * if (Str.Length() > 0)
   * {
   * unsigned char Result = (unsigned char)
   * ~((((PWALG_SIMPLE_STRING.Pos(Str.c_str()[0])-1) << 4) +
   * ((PWALG_SIMPLE_STRING.Pos(Str.c_str()[1])-1) << 0)) ^ PWALG_SIMPLE_MAGIC);
   * Str.Delete(1, 2);
   * return Result;
   * }
   * else return 0x00;
   * }
   *
   * @param str
   * @return
   */
  private static char simpleDecryptNextChar(List<Character> str) {
    if (str.size() > 0) {
      char result = unsignedChar(
        ~(
          (
            unsignedChar(str.get(0) << 4) + str.get(1) // Remove bitshift overflow bits.
          ) ^
          PWALG_SIMPLE_MAGIC
        )
      );

      removeItems(str, 0, 2);
      return result;
    } else return 0x00;
  }

  /**
   * Cut off anything over 255.
   *
   * @param v
   * @return
   */
  private static char unsignedChar(int v) {
    return (char) (v & 0xFF);
  }

  /**
   * Remove items from list
   */
  private static void removeItems(List<Character> lst, int start, int end) {
    for (int i = 0; i < end - start; ++i) {
      lst.remove(start);
    }
  }
}

 

 

  • 2
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值